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:
@@ -16,10 +16,10 @@ import (
|
||||
// Service defines business logic for user operations
|
||||
type Service interface {
|
||||
// Main CRUD
|
||||
Register(ctx context.Context, form *CreateUserForm) (*User, error)
|
||||
Update(ctx context.Context, id string, form *UpdateUserForm) (*User, error)
|
||||
Register(ctx context.Context, form *CreateUserForm) (*UserResponse, error)
|
||||
Update(ctx context.Context, id string, form *UpdateUserForm) (*UserResponse, 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)
|
||||
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
|
||||
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
|
||||
existingUser, _ := s.repository.GetByEmail(ctx, form.Email)
|
||||
if existingUser != nil {
|
||||
@@ -112,11 +112,11 @@ func (s *userService) Register(ctx context.Context, form *CreateUserForm) (*User
|
||||
"role": user.Role,
|
||||
})
|
||||
|
||||
return user, nil
|
||||
return user.ToResponse(), nil
|
||||
}
|
||||
|
||||
// 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
|
||||
user, err := s.repository.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
@@ -184,7 +184,7 @@ func (s *userService) Update(ctx context.Context, id string, form *UpdateUserFor
|
||||
"user_id": id,
|
||||
})
|
||||
|
||||
return user, nil
|
||||
return user.ToResponse(), nil
|
||||
}
|
||||
|
||||
// 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
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return user, nil
|
||||
return user.ToResponse(), nil
|
||||
}
|
||||
|
||||
// Search searches users with search and filters
|
||||
|
||||
Reference in New Issue
Block a user