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:
n.nakhostin
2025-09-06 15:59:34 +03:30
parent 38844939b5
commit 05e7b50ec4
3 changed files with 26 additions and 16 deletions
+9 -9
View File
@@ -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