Skip to content

Refactor: Eliminate duplicate code and optimize database queries (#1) - #12

Merged
jakubpraszkowski merged 1 commit into
jakubpraszkowski:mainfrom
Krzykoz:main
Feb 18, 2026
Merged

Refactor: Eliminate duplicate code and optimize database queries (#1)#12
jakubpraszkowski merged 1 commit into
jakubpraszkowski:mainfrom
Krzykoz:main

Conversation

@Krzykoz

@Krzykoz Krzykoz commented Feb 18, 2026

Copy link
Copy Markdown
Contributor

This pull request introduces comprehensive improvements to validation, exception handling, code structure, and test coverage across the codebase. The most significant changes include adding validation annotations to DTOs and enforcing them in controllers, refactoring exception handling for consistency, centralizing entity-to-DTO mapping logic, optimizing user existence checks, and introducing unit tests for DTO validation.

Validation Enhancements:

  • Added Jakarta validation annotations (e.g., @NotBlank, @Email, @Pattern) to DTOs such as AuthenticationRequest, RegisterRequest, UserRequest, and UrlRequest to enforce input constraints. [1] [2] [3] [4]
  • Updated controller methods to use @Valid on request bodies, ensuring validation is triggered automatically for incoming API requests. [1] [2] [3] [4] [5]

Exception Handling Improvements:

  • Introduced a new abstract BaseException class to standardize custom exceptions.
  • Refactored existing exceptions (UrlNotFoundException, UserAlreadyExistsException, UserNotFoundException) to extend from BaseException for consistent error handling. [1] [2] [3]

Code Structure and Reusability:

  • Created a utility class EntityMapper to centralize and reuse logic for converting entities to DTOs, reducing code duplication in services.
  • Updated UserService and UrlService to use EntityMapper for mapping entities to response DTOs, removing redundant mapping methods. [1] [2] [3] [4] [5] [6]

Repository and Service Optimization:

  • Added a new repository method existsByEmailOrUsername to optimize user existence checks, replacing redundant queries in AuthenticationService. [1] [2]
  • Removed unnecessary findById method from UrlRepository to clean up unused code.

Testing and Validation Coverage:

  • Introduced DtoValidationTest to thoroughly test validation constraints on all DTOs, ensuring correctness and robustness of validation logic.

* Initial plan

* Refactor: Fix duplicate DB queries, add validation, centralize mapping

Co-authored-by: Krzykoz <72080194+Krzykoz@users.noreply.github.com>

* Add validation and mapper unit tests

Co-authored-by: Krzykoz <72080194+Krzykoz@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Krzykoz <72080194+Krzykoz@users.noreply.github.com>
@jakubpraszkowski
jakubpraszkowski merged commit 9a8218c into jakubpraszkowski:main Feb 18, 2026
2 checks 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.

3 participants