/
create_user.go
46 lines (39 loc) · 1.05 KB
/
create_user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package controller
import (
"github.com/gin-gonic/gin"
"github.com/luankkobs/go-crud/src/configuration/logger"
"github.com/luankkobs/go-crud/src/configuration/validation"
"github.com/luankkobs/go-crud/src/controller/model/request"
"github.com/luankkobs/go-crud/src/model"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"net/http"
)
func CreateUser(c *gin.Context) {
logger.Info("Init CreateUser controller",
zap.String("journey", "createUser"),
)
var userRequest request.UserRequest
if err := c.ShouldBindJSON(&userRequest); err != nil {
logger.Error("Error trying to validate user info", err,
zap.String("journey", "createUser"))
errRest := validation.ValidateUserError(err)
c.JSON(errRest.Code, errRest)
return
}
domain := model.NewUserDomain(
userRequest.Email,
userRequest.Password,
userRequest.Name,
userRequest.Age,
)
if err := domain.CreateUser(); err != nil {
c.JSON(err.Code, err)
}
logger.Info("User created successfully",
zapcore.Field{
Key: "journey",
String: "createUser",
})
c.String(http.StatusOK, "")
}