fix: fixing response user domain API

This commit is contained in:
pahmiudahgede 2024-12-11 14:33:32 +07:00
parent 72e1d812ca
commit 5d98599f05
2 changed files with 66 additions and 53 deletions

View File

@ -5,6 +5,17 @@ import (
"regexp" "regexp"
) )
type UserResponseDTO struct {
ID string `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
Email string `json:"email"`
Phone string `json:"phone"`
RoleId string `json:"roleId"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
}
func ValidateEmail(email string) error { func ValidateEmail(email string) error {
if email == "" { if email == "" {
return errors.New("email harus diisi") return errors.New("email harus diisi")

View File

@ -29,38 +29,9 @@ func Register(c *fiber.Ctx) error {
err := services.RegisterUser(userInput.Username, userInput.Name, userInput.Email, userInput.Phone, userInput.Password, userInput.ConfirmPassword, userInput.RoleId) err := services.RegisterUser(userInput.Username, userInput.Name, userInput.Email, userInput.Phone, userInput.Password, userInput.ConfirmPassword, userInput.RoleId)
if err != nil { if err != nil {
if err.Error() == "email is already registered" {
return c.Status(fiber.StatusConflict).JSON(utils.FormatResponse(
fiber.StatusConflict,
"Email is already registered",
nil,
))
}
if err.Error() == "username is already registered" {
return c.Status(fiber.StatusConflict).JSON(utils.FormatResponse(
fiber.StatusConflict,
"Username is already registered",
nil,
))
}
if err.Error() == "phone number is already registered" {
return c.Status(fiber.StatusConflict).JSON(utils.FormatResponse(
fiber.StatusConflict,
"Phone number is already registered",
nil,
))
}
if err.Error() == "password dan confirm password tidak cocok" {
return c.Status(fiber.StatusBadRequest).JSON(utils.FormatResponse(
fiber.StatusBadRequest,
"Password dan confirm password tidak cocok",
nil,
))
}
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse( return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
fiber.StatusInternalServerError, fiber.StatusInternalServerError,
"Failed to create user", err.Error(),
nil, nil,
)) ))
} }
@ -74,15 +45,15 @@ func Register(c *fiber.Ctx) error {
)) ))
} }
userResponse := map[string]interface{}{ userResponse := dto.UserResponseDTO{
"id": user.ID, ID: user.ID,
"username": user.Username, Username: user.Username,
"name": user.Name, Name: user.Name,
"email": user.Email, Email: user.Email,
"phone": user.Phone, Phone: user.Phone,
"roleId": user.RoleID, RoleId: user.RoleID,
"createdAt": user.CreatedAt, CreatedAt: utils.FormatDateToIndonesianFormat(user.CreatedAt),
"updatedAt": user.UpdatedAt, UpdatedAt: utils.FormatDateToIndonesianFormat(user.UpdatedAt),
} }
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse( return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
@ -125,7 +96,6 @@ func Login(c *fiber.Ctx) error {
} }
func GetUserInfo(c *fiber.Ctx) error { func GetUserInfo(c *fiber.Ctx) error {
userID := c.Locals("userID").(string) userID := c.Locals("userID").(string)
user, err := services.GetUserByID(userID) user, err := services.GetUserByID(userID)
@ -137,21 +107,20 @@ func GetUserInfo(c *fiber.Ctx) error {
)) ))
} }
userResponse := map[string]interface{}{ userResponse := dto.UserResponseDTO{
"id": user.ID, ID: user.ID,
"username": user.Username, Username: user.Username,
"nama": user.Name, Name: user.Name,
"nohp": user.Phone, Phone: user.Phone,
"email": user.Email, Email: user.Email,
"statusverifikasi": user.EmailVerified, RoleId: user.RoleID,
"role": user.Role.RoleName, CreatedAt: utils.FormatDateToIndonesianFormat(user.CreatedAt),
"createdAt": user.CreatedAt, UpdatedAt: utils.FormatDateToIndonesianFormat(user.UpdatedAt),
"updatedAt": user.UpdatedAt,
} }
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse( return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
fiber.StatusOK, fiber.StatusOK,
"data user berhasil ditampilkan", "Data user berhasil ditampilkan",
userResponse, userResponse,
)) ))
} }
@ -186,10 +155,30 @@ func UpdateUser(c *fiber.Ctx) error {
)) ))
} }
user, err := repositories.GetUserByID(userID)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
fiber.StatusInternalServerError,
"Failed to fetch user after update",
nil,
))
}
userResponse := dto.UserResponseDTO{
ID: user.ID,
Username: user.Username,
Name: user.Name,
Email: user.Email,
Phone: user.Phone,
RoleId: user.RoleID,
CreatedAt: utils.FormatDateToIndonesianFormat(user.CreatedAt),
UpdatedAt: utils.FormatDateToIndonesianFormat(user.UpdatedAt),
}
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse( return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
fiber.StatusOK, fiber.StatusOK,
"User updated successfully", "User updated successfully",
nil, userResponse,
)) ))
} }
@ -223,9 +212,22 @@ func UpdatePassword(c *fiber.Ctx) error {
)) ))
} }
user, err := repositories.GetUserByID(userID)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
fiber.StatusInternalServerError,
"Failed to fetch user after password update",
nil,
))
}
updatedAtFormatted := utils.FormatDateToIndonesianFormat(user.UpdatedAt)
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse( return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
fiber.StatusOK, fiber.StatusOK,
"Password updated successfully", "Password updated successfully",
nil, map[string]string{
"updatedAt": updatedAtFormatted,
},
)) ))
} }