BLOG-105 Implement CRUD functionality for Labels #107

Merged
squid merged 3 commits from BLOG-105_lable_create_and_update_routes into main 2025-08-02 10:46:00 +08:00
Owner

Description

This PR introduces full CRUD (Create, Read, Update) functionality for post labels, implemented by following the existing Clean Architecture.

Backend

  • New API Endpoints for Label Management:

    • POST /label: Create a new label (authentication required).
    • PUT /label/{id}: Update a label by its ID (authentication required).
    • GET /label: Get all labels.
  • Architectural Implementation:

    • Delivery Layer: Added CreateLabelRequestDto, UpdateLabelRequestDto, and updated PostController with methods to handle label-related operations.
    • Application Layer: Created corresponding use cases (CreateLabelUseCase, UpdateLabelUseCase, GetAllLabelsUseCase) to handle business logic.
    • Gateway/Framework Layer: Implemented LabelRepository and LabelDbService to manage database interactions, including creating, updating, and querying labels.
  • Route Adjustment:

    • The route for fetching all post info has been changed from GET /post/all to GET /post to be more RESTful.

Frontend

  • API Call Update:
    • To match the backend route change, the API path for fetching all posts is updated from /post/all to /post.

Package Changes

No response

Screenshots

No response

Reference

Resolves #105

Checklist

  • A milestone is set
  • The related issuse has been linked to this branch
### Description This PR introduces full CRUD (Create, Read, Update) functionality for post labels, implemented by following the existing Clean Architecture. #### Backend * **New API Endpoints for Label Management:** * `POST /label`: Create a new label (**authentication required**). * `PUT /label/{id}`: Update a label by its ID (**authentication required**). * `GET /label`: Get all labels. * **Architectural Implementation:** * **Delivery Layer**: Added `CreateLabelRequestDto`, `UpdateLabelRequestDto`, and updated `PostController` with methods to handle label-related operations. * **Application Layer**: Created corresponding use cases (`CreateLabelUseCase`, `UpdateLabelUseCase`, `GetAllLabelsUseCase`) to handle business logic. * **Gateway/Framework Layer**: Implemented `LabelRepository` and `LabelDbService` to manage database interactions, including creating, updating, and querying labels. * **Route Adjustment:** * The route for fetching all post info has been changed from `GET /post/all` to `GET /post` to be more RESTful. #### Frontend * **API Call Update:** * To match the backend route change, the API path for fetching all posts is updated from `/post/all` to `/post`. ### Package Changes _No response_ ### Screenshots _No response_ ### Reference Resolves #105 ### Checklist - [x] A milestone is set - [x] The related issuse has been linked to this branch
squid added this to the 0.3 milestone 2025-08-02 10:41:09 +08:00
squid added 2 commits 2025-08-02 10:41:09 +08:00
BLOG-105 fix: update post endpoint to remove '/all' from the path for consistency
All checks were successful
Frontend CI / build (push) Successful in 1m9s
Auto Comment On PR / add_improve_comment (pull_request) Successful in 17s
PR Title Check / pr-title-check (pull_request) Successful in 17s
8f9a32948f
Collaborator

/improve

/improve
Collaborator

PR Code Suggestions

CategorySuggestion                                                                                                                                    Impact
General
Remove redundant database existence check

The update_label method in LabelDbServiceImpl already handles the case where the
label is not found by checking rows_affected and returning PostError::NotFound.
Therefore, the preceding get_label_by_id call in UpdateLabelUseCaseImpl::execute is
redundant. Removing this call will eliminate an unnecessary database query,
improving performance.

backend/feature/post/src/application/use_case/update_label_use_case.rs [27-30]

 async fn execute(&self, label: Label) -> Result<(), PostError> {
-    self.label_repository.get_label_by_id(label.id).await?;
     self.label_repository.update_label(label).await
 }
Suggestion importance[1-10]: 8

__

Why: The update_label method in LabelDbServiceImpl correctly handles the PostError::NotFound case by checking rows_affected. Therefore, the preceding get_label_by_id call in UpdateLabelUseCaseImpl is redundant, and removing it improves performance by eliminating an unnecessary database query.

Medium
## PR Code Suggestions ✨ <!-- --> <table><thead><tr><td><strong>Category</strong></td><td align=left><strong>Suggestion&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </strong></td><td align=center><strong>Impact</strong></td></tr><tbody><tr><td rowspan=1>General</td> <td> <details><summary>Remove redundant database existence check</summary> ___ **The <code>update_label</code> method in <code>LabelDbServiceImpl</code> already handles the case where the <br>label is not found by checking <code>rows_affected</code> and returning <code>PostError::NotFound</code>. <br>Therefore, the preceding <code>get_label_by_id</code> call in <code>UpdateLabelUseCaseImpl::execute</code> is <br>redundant. Removing this call will eliminate an unnecessary database query, <br>improving performance.** [backend/feature/post/src/application/use_case/update_label_use_case.rs [27-30]](https://git.squidspirit.com/squid/blog/src/branch/BLOG-105_lable_create_and_update_routes/backend/feature/post/src/application/use_case/update_label_use_case.rs#L27-L30) ```diff async fn execute(&self, label: Label) -> Result<(), PostError> { - self.label_repository.get_label_by_id(label.id).await?; self.label_repository.update_label(label).await } ``` <details><summary>Suggestion importance[1-10]: 8</summary> __ Why: The `update_label` method in `LabelDbServiceImpl` correctly handles the `PostError::NotFound` case by checking `rows_affected`. Therefore, the preceding `get_label_by_id` call in `UpdateLabelUseCaseImpl` is redundant, and removing it improves performance by eliminating an unnecessary database query. </details></details></td><td align=center>Medium </td></tr></tr></tbody></table>
squid added 1 commit 2025-08-02 10:44:41 +08:00
BLOG-105 fix: remove unnecessary label retrieval before updating label
All checks were successful
PR Title Check / pr-title-check (pull_request) Successful in 13s
Frontend CI / build (push) Successful in 1m9s
234b337519
Author
Owner

The update_label method in LabelDbServiceImpl already handles the case where the
label is not found by checking rows_affected and returning PostError::NotFound.
Therefore, the preceding get_label_by_id call in UpdateLabelUseCaseImpl::execute is
redundant. Removing this call will eliminate an unnecessary database query,
improving performance.

backend/feature/post/src/application/use_case/update_label_use_case.rs [27-30]

 async fn execute(&self, label: Label) -> Result<(), PostError> {
-    self.label_repository.get_label_by_id(label.id).await?;
     self.label_repository.update_label(label).await
 }

Addressed in 234b337519.

> **The <code>update_label</code> method in <code>LabelDbServiceImpl</code> already handles the case where the <br>label is not found by checking <code>rows_affected</code> and returning <code>PostError::NotFound</code>. <br>Therefore, the preceding <code>get_label_by_id</code> call in <code>UpdateLabelUseCaseImpl::execute</code> is <br>redundant. Removing this call will eliminate an unnecessary database query, <br>improving performance.** > > [backend/feature/post/src/application/use_case/update_label_use_case.rs [27-30]](https://git.squidspirit.com/squid/blog/src/branch/BLOG-105_lable_create_and_update_routes/backend/feature/post/src/application/use_case/update_label_use_case.rs#L27-L30) > > ```diff > async fn execute(&self, label: Label) -> Result<(), PostError> { > - self.label_repository.get_label_by_id(label.id).await?; > self.label_repository.update_label(label).await > } > ``` Addressed in https://git.squidspirit.com/squid/blog/commit/234b337519d9b622499c5f5e32313f0bf60ba731.
squid merged commit 71528294ae into main 2025-08-02 10:46:00 +08:00
squid deleted branch BLOG-105_lable_create_and_update_routes 2025-08-02 10:46:01 +08:00
Sign in to join this conversation.
No description provided.