140 lines
3.7 KiB
Go
140 lines
3.7 KiB
Go
package services
|
|
|
|
import (
|
|
"net/http"
|
|
"netina/commands"
|
|
"netina/database"
|
|
"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 {
|
|
usr := c.Get("user").(*models.JWTClaims)
|
|
if usr == nil {
|
|
return echo.ErrUnauthorized
|
|
}
|
|
|
|
u , err := FindUser(usr.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
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 , u.Name); 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 {
|
|
usr := c.Get("user").(*models.JWTClaims)
|
|
if usr == nil {
|
|
return echo.ErrUnauthorized
|
|
}
|
|
|
|
u , err := FindUser(usr.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
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 , u.Name)
|
|
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)
|
|
}
|
|
|
|
|
|
|
|
func FindUser(id uint)(*models.User , error) {
|
|
db := database.Db()
|
|
|
|
var user models.User
|
|
if err := db.Where("is_removed = ?" , false).Where("user_id = ?" , id).First(&user).Error; err != nil {
|
|
return nil , err
|
|
}
|
|
|
|
return &user , nil
|
|
} |