|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/labstack/echo" |
| 7 | + "github.com/laironacosta/ms-echo-go/controllers/dto" |
| 8 | + "github.com/laironacosta/ms-echo-go/services" |
| 9 | + "net/http" |
| 10 | +) |
| 11 | + |
| 12 | +type UserControllerInterface interface { |
| 13 | + Create(c echo.Context) error |
| 14 | + GetByEmail(c echo.Context) error |
| 15 | + UpdateByEmail(c echo.Context) error |
| 16 | + DeleteByEmail(c echo.Context) error |
| 17 | +} |
| 18 | + |
| 19 | +type UserController struct { |
| 20 | + userService services.UserServiceInterface |
| 21 | +} |
| 22 | + |
| 23 | +func NewUserController(userService services.UserServiceInterface) UserControllerInterface { |
| 24 | + return &UserController{ |
| 25 | + userService, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func (ctr *UserController) Create(c echo.Context) error { |
| 30 | + u := dto.CreateUserRequest{} |
| 31 | + if err := c.Bind(&u); err != nil { |
| 32 | + return echo.NewHTTPError(http.StatusBadRequest, err.Error()) |
| 33 | + } |
| 34 | + |
| 35 | + if err := ctr.userService.Create(context.Background(), u); err != nil { |
| 36 | + return echo.NewHTTPError(http.StatusBadRequest, err.Error()) |
| 37 | + } |
| 38 | + |
| 39 | + fmt.Printf("Request received: %+v \n", u) |
| 40 | + return c.JSON(http.StatusOK, dto.Response{ |
| 41 | + Message: "created", |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +func (ctr *UserController) GetByEmail(c echo.Context) error { |
| 46 | + e := c.Param("email") |
| 47 | + fmt.Printf("Path param received: %+v \n", e) |
| 48 | + |
| 49 | + u, err := ctr.userService.GetByEmail(context.Background(), e) |
| 50 | + if err != nil { |
| 51 | + return echo.NewHTTPError(http.StatusBadRequest, err.Error()) |
| 52 | + } |
| 53 | + return c.JSON(http.StatusOK, u) |
| 54 | +} |
| 55 | + |
| 56 | +func (ctr *UserController) UpdateByEmail(c echo.Context) error { |
| 57 | + u := dto.UpdateUserRequest{} |
| 58 | + if err := c.Bind(&u); err != nil { |
| 59 | + return echo.NewHTTPError(http.StatusBadRequest, err.Error()) |
| 60 | + } |
| 61 | + |
| 62 | + e := c.Param("email") |
| 63 | + |
| 64 | + fmt.Printf("Request received: %+v \n", u) |
| 65 | + fmt.Printf("Path param received: %+v \n", e) |
| 66 | + |
| 67 | + if err := ctr.userService.UpdateByEmail(context.Background(), u, e); err != nil { |
| 68 | + return echo.NewHTTPError(http.StatusBadRequest, err.Error()) |
| 69 | + } |
| 70 | + return c.JSON(http.StatusOK, dto.Response{ |
| 71 | + Message: "updated", |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +func (ctr *UserController) DeleteByEmail(c echo.Context) error { |
| 76 | + e := c.Param("email") |
| 77 | + |
| 78 | + fmt.Printf("Path param received: %+v \n", e) |
| 79 | + |
| 80 | + if err := ctr.userService.DeleteByEmail(context.Background(), e); err != nil { |
| 81 | + return echo.NewHTTPError(http.StatusBadRequest, err.Error()) |
| 82 | + } |
| 83 | + return c.JSON(http.StatusOK, dto.Response{ |
| 84 | + Message: "deleted", |
| 85 | + }) |
| 86 | +} |
0 commit comments