Skip to content

refactor: add generic controller and query#24

Merged
zetsux merged 3 commits intomainfrom
task/add-generic-controller-and-query
Oct 24, 2025
Merged

refactor: add generic controller and query#24
zetsux merged 3 commits intomainfrom
task/add-generic-controller-and-query

Conversation

@zetsux
Copy link
Owner

@zetsux zetsux commented Oct 24, 2025

Description

This pull request refactors the user controller logic for CRUD operations by introducing generic handler functions to reduce code duplication and standardize error handling. It also updates the user picture change workflow to simplify the service interface, and moves the generic query logic to a more appropriate package. Some test and DTO adjustments are made to match these changes.

Controller Refactoring and Handler Abstraction:

  • Introduced generic handler functions (HandleCreate, HandleGetAll, HandleGetByID, HandleUpdate, HandleDelete) in api/v1/controller/generic.go to standardize and simplify CRUD operations in controllers, replacing repetitive code in user.go. [1] [2] [3]

User Picture Change Workflow Simplification:

  • Changed the UserChangePictureRequest DTO to include an ID field, so service methods no longer need a separate user ID argument.
  • Updated the UserService interface and implementation: ChangePicture now takes only a single DTO argument (with the ID inside), not a separate userID. [1] [2]
  • Updated all related tests and mocks to match the new signature and DTO structure for ChangePicture. [1] [2] [3]

Query Layer Refactor:

  • Moved the generic query and pagination logic from support/base/query.go to infrastructure/query/generic.go and updated usages to import from the new location. [1] [2] [3]

Dependency and Import Cleanup:

  • Made github.com/go-playground/validator/v10 a direct dependency in go.mod to ensure proper validation support.
  • Removed unneeded imports (like reflect) from user.go after controller refactoring.

Change

Check all options that are relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

  • I have performed a self-review of my code
  • I have made sure that my fix is effective / my feature works
  • All other features still works locally with my changes
  • I have made API documentation and example response in Postman

@zetsux zetsux self-assigned this Oct 24, 2025
@zetsux zetsux requested a review from Copilot October 24, 2025 08:04
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request refactors the user controller to reduce code duplication by introducing generic handler functions for CRUD operations, simplifies the user picture change workflow by embedding the user ID in the request DTO, and reorganizes the generic query logic into a more appropriate package location.

  • Introduced generic handler functions (HandleCreate, HandleGetAll, HandleUpdate, HandleDelete) to standardize CRUD operations across controllers
  • Simplified the ChangePicture service method signature by including the ID field directly in UserChangePictureRequest DTO
  • Moved generic pagination query logic from support/base/query.go to infrastructure/query/generic.go

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
api/v1/controller/generic.go New file containing generic handler functions for CRUD operations
api/v1/controller/user.go Refactored to use generic handlers, reducing boilerplate code
core/helper/dto/user.go Added ID field to UserChangePictureRequest DTO
core/service/user.go Updated ChangePicture method signature to accept only the DTO parameter
core/service/tests/picture_test.go Updated test to match new ChangePicture signature
api/v1/controller/tests/user_test.go Updated mock and test expectations for ChangePicture
infrastructure/query/generic.go Relocated from support/base/query.go with package name change
infrastructure/query/user.go Updated import reference for GetWithPagination function
go.mod Made validator/v10 a direct dependency

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@zetsux zetsux merged commit 47ab263 into main Oct 24, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants