user service
parent
b58103f8ac
commit
d1181f31a2
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue