Refactor User Response Handling in API Endpoints
- Updated user-related API handlers to return user entities directly instead of using the ToResponse method, simplifying response structures. - Modified service methods to return UserResponse types instead of User, enhancing consistency in response formats. - Improved pagination handling in the response package by setting default values for sorting parameters, ensuring more predictable behavior in API responses.
This commit is contained in:
@@ -137,7 +137,7 @@ func (h *Handler) GetProfile(c echo.Context) error {
|
|||||||
return response.NotFound(c, "User not found")
|
return response.NotFound(c, "User not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.Success(c, user.ToResponse(), "Profile retrieved successfully")
|
return response.Success(c, user, "Profile retrieved successfully")
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateProfile updates current user profile
|
// UpdateProfile updates current user profile
|
||||||
@@ -172,7 +172,7 @@ func (h *Handler) UpdateProfile(c echo.Context) error {
|
|||||||
return response.BadRequest(c, err.Error(), "")
|
return response.BadRequest(c, err.Error(), "")
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.Success(c, user.ToResponse(), "Profile updated successfully")
|
return response.Success(c, user, "Profile updated successfully")
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChangePassword changes current user password
|
// ChangePassword changes current user password
|
||||||
@@ -276,7 +276,7 @@ func (h *Handler) CreateUser(c echo.Context) error {
|
|||||||
return response.BadRequest(c, err.Error(), "")
|
return response.BadRequest(c, err.Error(), "")
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.Created(c, user.ToResponse(), "User created successfully")
|
return response.Created(c, user, "User created successfully")
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListUsers lists users with search and filters (admin only)
|
// ListUsers lists users with search and filters (admin only)
|
||||||
@@ -342,7 +342,7 @@ func (h *Handler) GetUserByID(c echo.Context) error {
|
|||||||
return response.NotFound(c, "User not found")
|
return response.NotFound(c, "User not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.Success(c, user.ToResponse(), "User retrieved successfully")
|
return response.Success(c, user, "User retrieved successfully")
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateUser updates a user (admin only)
|
// UpdateUser updates a user (admin only)
|
||||||
@@ -380,7 +380,7 @@ func (h *Handler) UpdateUser(c echo.Context) error {
|
|||||||
return response.BadRequest(c, err.Error(), "")
|
return response.BadRequest(c, err.Error(), "")
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.Success(c, user.ToResponse(), "User updated successfully")
|
return response.Success(c, user, "User updated successfully")
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteUser deletes a user (admin only)
|
// DeleteUser deletes a user (admin only)
|
||||||
|
|||||||
@@ -16,10 +16,10 @@ import (
|
|||||||
// Service defines business logic for user operations
|
// Service defines business logic for user operations
|
||||||
type Service interface {
|
type Service interface {
|
||||||
// Main CRUD
|
// Main CRUD
|
||||||
Register(ctx context.Context, form *CreateUserForm) (*User, error)
|
Register(ctx context.Context, form *CreateUserForm) (*UserResponse, error)
|
||||||
Update(ctx context.Context, id string, form *UpdateUserForm) (*User, error)
|
Update(ctx context.Context, id string, form *UpdateUserForm) (*UserResponse, error)
|
||||||
UpdateStatus(ctx context.Context, id string, form *UpdateUserStatusForm) error
|
UpdateStatus(ctx context.Context, id string, form *UpdateUserStatusForm) error
|
||||||
GetUserByID(ctx context.Context, userID string) (*User, error)
|
GetUserByID(ctx context.Context, userID string) (*UserResponse, error)
|
||||||
Search(ctx context.Context, form *SearchUsersForm, pagination *response.Pagination) (*UserListResponse, error)
|
Search(ctx context.Context, form *SearchUsersForm, pagination *response.Pagination) (*UserListResponse, error)
|
||||||
Delete(ctx context.Context, id string) error
|
Delete(ctx context.Context, id string) error
|
||||||
|
|
||||||
@@ -51,7 +51,7 @@ func NewUserService(repository Repository, logger logger.Logger, authService aut
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Register creates a new user
|
// Register creates a new user
|
||||||
func (s *userService) Register(ctx context.Context, form *CreateUserForm) (*User, error) {
|
func (s *userService) Register(ctx context.Context, form *CreateUserForm) (*UserResponse, error) {
|
||||||
// Check if email already exists
|
// Check if email already exists
|
||||||
existingUser, _ := s.repository.GetByEmail(ctx, form.Email)
|
existingUser, _ := s.repository.GetByEmail(ctx, form.Email)
|
||||||
if existingUser != nil {
|
if existingUser != nil {
|
||||||
@@ -112,11 +112,11 @@ func (s *userService) Register(ctx context.Context, form *CreateUserForm) (*User
|
|||||||
"role": user.Role,
|
"role": user.Role,
|
||||||
})
|
})
|
||||||
|
|
||||||
return user, nil
|
return user.ToResponse(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update updates user information
|
// Update updates user information
|
||||||
func (s *userService) Update(ctx context.Context, id string, form *UpdateUserForm) (*User, error) {
|
func (s *userService) Update(ctx context.Context, id string, form *UpdateUserForm) (*UserResponse, error) {
|
||||||
// Get user
|
// Get user
|
||||||
user, err := s.repository.GetByID(ctx, id)
|
user, err := s.repository.GetByID(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -184,7 +184,7 @@ func (s *userService) Update(ctx context.Context, id string, form *UpdateUserFor
|
|||||||
"user_id": id,
|
"user_id": id,
|
||||||
})
|
})
|
||||||
|
|
||||||
return user, nil
|
return user.ToResponse(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateStatus updates user status
|
// UpdateStatus updates user status
|
||||||
@@ -218,13 +218,13 @@ func (s *userService) UpdateStatus(ctx context.Context, id string, form *UpdateU
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetUserByID retrieves a user by ID
|
// GetUserByID retrieves a user by ID
|
||||||
func (s *userService) GetUserByID(ctx context.Context, id string) (*User, error) {
|
func (s *userService) GetUserByID(ctx context.Context, id string) (*UserResponse, error) {
|
||||||
user, err := s.repository.GetByID(ctx, id)
|
user, err := s.repository.GetByID(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return user, nil
|
return user.ToResponse(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search searches users with search and filters
|
// Search searches users with search and filters
|
||||||
|
|||||||
@@ -191,11 +191,21 @@ func NewPagination(c echo.Context) *Pagination {
|
|||||||
offset = 0
|
offset = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sortBy := c.QueryParam("sort_by")
|
||||||
|
if sortBy == "" {
|
||||||
|
sortBy = "created_at"
|
||||||
|
}
|
||||||
|
|
||||||
|
sortOrder := c.QueryParam("sort_order")
|
||||||
|
if sortOrder == "" {
|
||||||
|
sortOrder = "desc"
|
||||||
|
}
|
||||||
|
|
||||||
return &Pagination{
|
return &Pagination{
|
||||||
Limit: limit,
|
Limit: limit,
|
||||||
Offset: offset,
|
Offset: offset,
|
||||||
SortBy: c.QueryParam("sort_by"),
|
SortBy: sortBy,
|
||||||
SortOrder: c.QueryParam("sort_order"),
|
SortOrder: sortOrder,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user