-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The provided code changes implement user activation functionality wit…
…hin a Kotlin Spring application using R2DBC for database interactions. Let's break down the modifications: **1. `UserActivationDao.kt`:** * The `activateUser` function is renamed to `activate` for conciseness. * A commented-out code block suggests a previous implementation directly handling success/failure checks based on the database update result. This logic is moved to the service layer. **2. `SignupService.kt`:** * The `signup` function is streamlined, directly returning the result of the signup process. * New functions `activateRequest` and `activate` are introduced. `activateRequest` takes the activation key and `ServerWebExchange` to perform the activation. `activate` encapsulates the database interaction and error handling. It calls the `activate` method from the DAO, checks the result, and throws exceptions if activation fails. The constant `ONE_ROW_UPDATED` is added to check if exactly one row was updated during activation, ensuring only the intended user is activated. * A commented-out code block outlines a previous, more complex activation approach likely involving direct database queries and checks within the controller. This is replaced by the cleaner service layer approach. **3. `DaoTests.kt`:** * The test code is updated to use the renamed `activate` function. The assertion checking the number of updated rows is removed as this validation is now in the `SignupService`. **4. `UserController.kt`:** * A new `activate` endpoint is added, mapped to `GET /activate`. It takes the activation key as a request parameter and delegates the activation process to the `activateRequest` function in the service. **Key improvements:** * **Centralized logic:** Moving the activation logic to the `SignupService` improves code organization and maintainability. * **Error handling:** The `activate` function in the service provides robust error handling and clear exception messages. * **Simplified controller:** The `UserController` is now more concise, focusing on routing requests and handling responses. * **Testability:** The refactored code is easier to test, with clear separation of concerns between the controller, service, and DAO layers. This refined implementation provides a more structured, maintainable, and testable approach to user activation, leveraging the power of Spring's service layer and clear error handling. The change also illustrates the idea of keeping controllers lean, delegating business logic to services.
- Loading branch information
Showing
4 changed files
with
75 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters