From d1181f31a20944eda1bcb9d26ae5a02cbc3e9535 Mon Sep 17 00:00:00 2001 From: nima Date: Mon, 17 Jun 2024 19:05:19 +0330 Subject: [PATCH] user service --- services/user.go | 106 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 services/user.go diff --git a/services/user.go b/services/user.go new file mode 100644 index 0000000..bc06768 --- /dev/null +++ b/services/user.go @@ -0,0 +1,106 @@ +package services + +import ( + "net/http" + "netina/commands" + "netina/models" + cm "netina/models/commands" + "netina/queries" + User_repository "netina/repositories/user" + "netina/validation" + "strconv" + + "github.com/labstack/echo/v4" +) + +type UserService struct { + CommandRepo User_repository.UserCommandRepository + QueryRepo User_repository.UserQueryRepository +} + +func (s *UserService) CreateUser(c echo.Context) error { + user := new(cm.CreateUserCommand) + if err := c.Bind(user); err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request payload"}) + } + if err := validation.ValidateStruct(user); err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()}) + } + + handler := commands.CreateUserHandler{Repository: s.CommandRepo} + if err := handler.Handle(*user); err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) + } + + return c.JSON(http.StatusCreated, user) +} + +func (s *UserService) GetUser(c echo.Context) error { + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid User ID"}) + } + + handler := queries.GetUserByIdHandler{Repository: s.QueryRepo} + User, err := handler.Handle(uint(id)) + if err != nil { + return c.JSON(http.StatusNotFound, map[string]string{"error": err.Error()}) + } + + return c.JSON(http.StatusOK, User) +} + + +func (s *UserService) GetUserByPhoneNumber(c echo.Context) error { + number := new(models.CreatePhoneNumber) + if err := c.Bind(number); err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid phone number"}) + } + + handler := queries.GetUserByPhoneNumberHandler{Repository: s.QueryRepo} + user , err := handler.Handle(number.PhoneNumber) + if err != nil { + return c.JSON(http.StatusNotFound, map[string]string{"error": err.Error()}) + } + + return c.JSON(http.StatusOK , user) +} + + + +func (s *UserService) UpdateUser(c echo.Context) error { + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid User ID"}) + } + + User := new(cm.UpdateUserCommand) + if err := c.Bind(User); err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request payload"}) + } + if err := validation.ValidateStruct(User); err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()}) + } + + handler := commands.UpdateUserHandler{Repository: s.CommandRepo} + updatedUser, err := handler.Handle(uint(id), *User) + if err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) + } + + return c.JSON(http.StatusOK, updatedUser) +} + +func (s *UserService) RemoveUser(c echo.Context) error { + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid User ID"}) + } + + handler := commands.RemoveUserHandler{Repository: s.CommandRepo} + if err := handler.Handle(uint(id)); err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) + } + + return c.NoContent(http.StatusNoContent) +}