BLOG-125 Get post by sementic ID #134

Merged
squid merged 10 commits from BLOG-125_semantic_post_id into main 2025-10-12 18:28:58 +08:00
Owner

Description

Backend

  • String and interger can be pass as id to GET /post/{id}

  • For the posts existed, the default semantic_id for them will be _id. (e.g. _1, _2)

  • Semantic ID should follow the rules:

    1. It shouldn't be an integer

    2. It should match the pattern: ^[0-9a-zA-Z_\-]+$


    Semantic ID Result Note
    12 X against with i
    -3 X against with i
    3.14 X against with ii
    hello world X against with ii
    EMPTY X against with ii
    12_34-56 O

Frontend

  • The href of post preview card becomes the semantic ID.

Package Changes

regex = "1.12.1"

Screenshots

截圖 2025-10-12 下午6.23.12.png

Reference

Resolves #125.

Checklist

  • A milestone is set
  • The related issuse has been linked to this branch
### Description #### Backend - String and interger can be pass as `id` to `GET` `/post/{id}` - For the posts existed, the default `semantic_id` for them will be `_id`. (e.g. `_1`, `_2`) - Semantic ID should follow the rules: 1. It shouldn't be an integer 1. It should match the pattern: `^[0-9a-zA-Z_\-]+$` <br> |Semantic ID|Result|Note| |-|-|-| |12|X|against with `i`| |-3|X|against with `i`| |3.14|X|against with `ii`| |hello world|X|against with `ii`| |*EMPTY*|X|against with `ii`| |12_34-56|O|| #### Frontend - The href of post preview card becomes the semantic ID. ### Package Changes ```toml regex = "1.12.1" ``` ### Screenshots ![截圖 2025-10-12 下午6.23.12.png](/attachments/67de1cd7-f584-40ad-9bbd-27f8bf6f1894) ### Reference Resolves #125. ### Checklist - [x] A milestone is set - [x] The related issuse has been linked to this branch
squid added this to the 0.4 milestone 2025-10-12 17:28:37 +08:00
squid added 5 commits 2025-10-12 17:28:38 +08:00
feat: add example environment configuration file
All checks were successful
Frontend CI / build (push) Successful in 1m20s
bb9a6bbb12
feat: add validation for semantic ID in post and update error handling
All checks were successful
Frontend CI / build (push) Successful in 1m23s
8f91d815bc
fix: update comment and SQL statement to set semantic_id with _id prefix
Some checks failed
Frontend CI / build (push) Successful in 1m21s
Auto Comment On PR / add_improve_comment (pull_request) Successful in 17s
PR Title Check / pr-title-check (pull_request) Failing after 16s
9d6098c353
Collaborator

/improve

/improve
Collaborator

PR Code Suggestions

CategorySuggestion                                                                                                                                    Impact
Possible issue
Correct HTTP status codes for errors

The API handlers return 500 Internal Server Error for PostError::NotFound and
PostError::InvalidSemanticId. This is incorrect and misleading for client-side
errors. PostError::NotFound should return 404 Not Found, and
PostError::InvalidSemanticId should return 400 Bad Request. Ensure consistent and
appropriate HTTP status codes across all handlers.

backend/feature/post/src/framework/web/create_label_handler.rs [37-40]

-PostError::NotFound | PostError::InvalidSemanticId => {
-    capture_anyhow(&anyhow!(e));
-    HttpResponse::InternalServerError().finish()
-}
+PostError::NotFound => HttpResponse::NotFound().finish(),
+PostError::InvalidSemanticId => HttpResponse::BadRequest().finish(),
Suggestion importance[1-10]: 10

__

Why: Returning 500 Internal Server Error for PostError::NotFound and PostError::InvalidSemanticId is incorrect. 404 Not Found and 400 Bad Request are the appropriate HTTP status codes, respectively, for these client-side errors, which is crucial for correct API behavior.

High
General
Align struct name with trait

The struct GetPostIdBySemanticIdUseCaseImpl is named inconsistently with its trait
GetPostBySemanticIdUseCase and its actual functionality, which is to retrieve the
full post, not just its ID. Rename the struct to GetPostBySemanticIdUseCaseImpl to
accurately reflect its purpose.

backend/feature/post/src/application/use_case/get_post_by_sementic_id_use_case.rs [18]

-pub struct GetPostIdBySemanticIdUseCaseImpl {
+pub struct GetPostBySemanticIdUseCaseImpl {
Suggestion importance[1-10]: 8

__

Why: The struct GetPostIdBySemanticIdUseCaseImpl is inconsistently named with its trait GetPostBySemanticIdUseCase and its actual functionality, which retrieves the full post, not just its ID. Renaming it to GetPostBySemanticIdUseCaseImpl improves clarity and consistency.

Medium
Correct typo in module name

The module name get_post_by_sementic_id_use_case contains a typo. It should be
get_post_by_semantic_id_use_case for correct spelling and consistency. This also
requires renaming the corresponding file
backend/feature/post/src/application/use_case/get_post_by_sementic_id_use_case.rs.

backend/feature/post/src/application/use_case.rs [6]

-pub mod get_post_by_sementic_id_use_case;
+pub mod get_post_by_semantic_id_use_case;
Suggestion importance[1-10]: 7

__

Why: The suggestion correctly identifies a typo in the module name get_post_by_sementic_id_use_case, which should be get_post_by_semantic_id_use_case. This improves code consistency and readability, and the suggestion correctly notes the need to rename the corresponding file.

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>Possible issue</td> <td> <details><summary>Correct HTTP status codes for errors</summary> ___ **The API handlers return <code>500 Internal Server Error</code> for <code>PostError::NotFound</code> and <br><code>PostError::InvalidSemanticId</code>. This is incorrect and misleading for client-side <br>errors. <code>PostError::NotFound</code> should return <code>404 Not Found</code>, and <br><code>PostError::InvalidSemanticId</code> should return <code>400 Bad Request</code>. Ensure consistent and <br>appropriate HTTP status codes across all handlers.** [backend/feature/post/src/framework/web/create_label_handler.rs [37-40]](https://git.squidspirit.com/squid/blog/src/branch/BLOG-125_semantic_post_id/backend/feature/post/src/framework/web/create_label_handler.rs#L37-L40) ```diff -PostError::NotFound | PostError::InvalidSemanticId => { - capture_anyhow(&anyhow!(e)); - HttpResponse::InternalServerError().finish() -} +PostError::NotFound => HttpResponse::NotFound().finish(), +PostError::InvalidSemanticId => HttpResponse::BadRequest().finish(), ``` <details><summary>Suggestion importance[1-10]: 10</summary> __ Why: Returning `500 Internal Server Error` for `PostError::NotFound` and `PostError::InvalidSemanticId` is incorrect. `404 Not Found` and `400 Bad Request` are the appropriate HTTP status codes, respectively, for these client-side errors, which is crucial for correct API behavior. </details></details></td><td align=center>High </td></tr><tr><td rowspan=2>General</td> <td> <details><summary>Align struct name with trait</summary> ___ **The struct <code>GetPostIdBySemanticIdUseCaseImpl</code> is named inconsistently with its trait <br><code>GetPostBySemanticIdUseCase</code> and its actual functionality, which is to retrieve the <br>full post, not just its ID. Rename the struct to <code>GetPostBySemanticIdUseCaseImpl</code> to <br>accurately reflect its purpose.** [backend/feature/post/src/application/use_case/get_post_by_sementic_id_use_case.rs [18]](https://git.squidspirit.com/squid/blog/src/branch/BLOG-125_semantic_post_id/backend/feature/post/src/application/use_case/get_post_by_sementic_id_use_case.rs#L18-L18) ```diff -pub struct GetPostIdBySemanticIdUseCaseImpl { +pub struct GetPostBySemanticIdUseCaseImpl { ``` <details><summary>Suggestion importance[1-10]: 8</summary> __ Why: The struct `GetPostIdBySemanticIdUseCaseImpl` is inconsistently named with its trait `GetPostBySemanticIdUseCase` and its actual functionality, which retrieves the full post, not just its ID. Renaming it to `GetPostBySemanticIdUseCaseImpl` improves clarity and consistency. </details></details></td><td align=center>Medium </td></tr><tr><td> <details><summary>Correct typo in module name</summary> ___ **The module name <code>get_post_by_sementic_id_use_case</code> contains a typo. It should be <br><code>get_post_by_semantic_id_use_case</code> for correct spelling and consistency. This also <br>requires renaming the corresponding file <br><code>backend/feature/post/src/application/use_case/get_post_by_sementic_id_use_case.rs</code>.** [backend/feature/post/src/application/use_case.rs [6]](https://git.squidspirit.com/squid/blog/src/branch/BLOG-125_semantic_post_id/backend/feature/post/src/application/use_case.rs#L6-L6) ```diff -pub mod get_post_by_sementic_id_use_case; +pub mod get_post_by_semantic_id_use_case; ``` <details><summary>Suggestion importance[1-10]: 7</summary> __ Why: The suggestion correctly identifies a typo in the module name `get_post_by_sementic_id_use_case`, which should be `get_post_by_semantic_id_use_case`. This improves code consistency and readability, and the suggestion correctly notes the need to rename the corresponding file. </details></details></td><td align=center>Medium </td></tr></tr></tbody></table>
squid added 1 commit 2025-10-12 17:38:42 +08:00
fix: correct spelling of 'semantic' in use case references
Some checks failed
Frontend CI / build (push) Successful in 1m21s
PR Title Check / pr-title-check (pull_request) Failing after 17s
c0fe4585ba
Author
Owner

The module name get_post_by_sementic_id_use_case contains a typo. It should be
get_post_by_semantic_id_use_case for correct spelling and consistency. This also
requires renaming the corresponding file
backend/feature/post/src/application/use_case/get_post_by_sementic_id_use_case.rs.

backend/feature/post/src/application/use_case.rs [6]

-pub mod get_post_by_sementic_id_use_case;
+pub mod get_post_by_semantic_id_use_case;

Addressed in c0fe4585ba.

> The module name <code>get_post_by_sementic_id_use_case</code> contains a typo. It should be <br><code>get_post_by_semantic_id_use_case</code> for correct spelling and consistency. This also <br>requires renaming the corresponding file <br><code>backend/feature/post/src/application/use_case/get_post_by_sementic_id_use_case.rs</code>. > > [backend/feature/post/src/application/use_case.rs [6]](https://git.squidspirit.com/squid/blog/src/branch/BLOG-125_semantic_post_id/backend/feature/post/src/application/use_case.rs#L6-L6) > > ```diff > -pub mod get_post_by_sementic_id_use_case; > +pub mod get_post_by_semantic_id_use_case; > ``` Addressed in c0fe4585ba881bf28314bd03df60f47af6276b54.
squid added 1 commit 2025-10-12 17:43:31 +08:00
fix: correct struct name from GetPostIdBySemanticIdUseCaseImpl to GetPostBySemanticIdUseCaseImpl
Some checks failed
Frontend CI / build (push) Successful in 1m21s
PR Title Check / pr-title-check (pull_request) Failing after 17s
9862850d28
Author
Owner

The struct GetPostIdBySemanticIdUseCaseImpl is named inconsistently with its trait
GetPostBySemanticIdUseCase and its actual functionality, which is to retrieve the
full post, not just its ID. Rename the struct to GetPostBySemanticIdUseCaseImpl to
accurately reflect its purpose.

backend/feature/post/src/application/use_case/get_post_by_sementic_id_use_case.rs [18]

-pub struct GetPostIdBySemanticIdUseCaseImpl {
+pub struct GetPostBySemanticIdUseCaseImpl {

Addressed in 9862850d28.

> The struct <code>GetPostIdBySemanticIdUseCaseImpl</code> is named inconsistently with its trait <br><code>GetPostBySemanticIdUseCase</code> and its actual functionality, which is to retrieve the <br>full post, not just its ID. Rename the struct to <code>GetPostBySemanticIdUseCaseImpl</code> to <br>accurately reflect its purpose. > > [backend/feature/post/src/application/use_case/get_post_by_sementic_id_use_case.rs [18]](https://git.squidspirit.com/squid/blog/src/branch/BLOG-125_semantic_post_id/backend/feature/post/src/application/use_case/get_post_by_sementic_id_use_case.rs#L18-L18) > > ```diff > -pub struct GetPostIdBySemanticIdUseCaseImpl { > +pub struct GetPostBySemanticIdUseCaseImpl { > ``` Addressed in 9862850d283f2ac74b2000eef447f01f9687e12c.
squid added 2 commits 2025-10-12 18:10:11 +08:00
squid changed title from WIP: Get post by sementic ID to Get post by sementic ID 2025-10-12 18:16:21 +08:00
squid changed title from Get post by sementic ID to BLOG-125 Get post by sementic ID 2025-10-12 18:24:14 +08:00
squid added 1 commit 2025-10-12 18:27:05 +08:00
chore: pnpm format
All checks were successful
Frontend CI / build (push) Successful in 1m21s
PR Title Check / pr-title-check (pull_request) Successful in 17s
8b1d643531
squid merged commit 565df7aace into main 2025-10-12 18:28:58 +08:00
squid deleted branch BLOG-125_semantic_post_id 2025-10-12 18:28:59 +08:00
Sign in to join this conversation.
No description provided.