Skip to content

feat: add generic repository#29

Merged
zetsux merged 2 commits intomainfrom
task/add-generic-repository
Nov 1, 2025
Merged

feat: add generic repository#29
zetsux merged 2 commits intomainfrom
task/add-generic-repository

Conversation

@zetsux
Copy link
Owner

@zetsux zetsux commented Nov 1, 2025

Description

This pull request refactors the user service and repository layers to simplify user CRUD operations and improve code maintainability. The main changes include removing redundant user existence checks in the service methods and introducing generic repository functions for common database operations. These changes reduce code duplication and make future updates easier.

Service Layer Simplification:

  • Removed unnecessary checks for empty entity.User results in user-related service methods, relying on repository error handling for "not found" cases (core/service/user.go). [1] [2] [3] [4]

Repository Layer Refactoring:

  • Added new generic repository functions (Create, GetByID, Update, Delete) in infrastructure/repository/generic.go to standardize CRUD operations and reduce repetitive code.
  • Updated methods in userRepository to use the new generic functions, streamlining implementations for creating, updating, and deleting users (infrastructure/repository/user.go). [1] [2]

Closes #28

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 Nov 1, 2025
@zetsux zetsux requested a review from Copilot November 1, 2025 05:43
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 repository to use generic helper functions for common database operations, improving code maintainability and reducing duplication. The changes also clean up redundant user existence checks that were previously performed in the service layer.

  • Introduced generic CRUD helper functions (Create, Update, Delete, GetByID) in a new generic.go file
  • Refactored userRepository methods to use the generic helpers instead of duplicated code
  • Removed redundant reflect.DeepEqual checks in the service layer since GetUserByPrimaryKey already handles not-found cases

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
infrastructure/repository/generic.go Added generic helper functions for common CRUD operations with proper transaction handling
infrastructure/repository/user.go Refactored to use generic helpers and fixed receiver naming inconsistency from rp to ur
core/service/user.go Removed redundant empty entity checks that are already handled by the repository layer
Comments suppressed due to low confidence (1)

infrastructure/repository/user.go:21

  • Receiver name 'rp' is inconsistent with the 'ur' naming convention used in other methods (CreateNewUser, GetUserByPrimaryKey, UpdateUser, DeleteUserByID). Consider renaming this receiver to 'ur' for consistency.
func (rp *userRepository) DB() *gorm.DB {

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@zetsux zetsux force-pushed the task/add-generic-repository branch from 23a29fc to 38738fc Compare November 1, 2025 05:57
@zetsux zetsux merged commit 89cf4ec into main Nov 1, 2025
1 check passed
@zetsux zetsux deleted the task/add-generic-repository branch November 1, 2025 05:59
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.

Create generic repository

2 participants