Skip to content

Commit 54ba2bb

Browse files
committed
controllers/keyword: Extract GetResponse struct
1 parent 1dc896c commit 54ba2bb

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/controllers/keyword.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use crate::util::errors::AppResult;
66
use crate::views::EncodableKeyword;
77
use axum::Json;
88
use axum::extract::{FromRequestParts, Path, Query};
9-
use axum_extra::json;
10-
use axum_extra::response::ErasedJson;
119
use diesel::prelude::*;
1210
use http::request::Parts;
1311

@@ -72,6 +70,11 @@ pub async fn list_keywords(
7270
Ok(Json(ListResponse { keywords, meta }))
7371
}
7472

73+
#[derive(Debug, Serialize, utoipa::ToSchema)]
74+
pub struct GetResponse {
75+
pub keyword: EncodableKeyword,
76+
}
77+
7578
/// Get keyword metadata.
7679
#[utoipa::path(
7780
get,
@@ -80,11 +83,14 @@ pub async fn list_keywords(
8083
("keyword" = String, Path, description = "The keyword to find"),
8184
),
8285
tag = "keywords",
83-
responses((status = 200, description = "Successful Response")),
86+
responses((status = 200, description = "Successful Response", body = inline(GetResponse))),
8487
)]
85-
pub async fn find_keyword(Path(name): Path<String>, state: AppState) -> AppResult<ErasedJson> {
88+
pub async fn find_keyword(
89+
Path(name): Path<String>,
90+
state: AppState,
91+
) -> AppResult<Json<GetResponse>> {
8692
let mut conn = state.db_read().await?;
8793
let kw = Keyword::find_by_keyword(&mut conn, &name).await?;
88-
89-
Ok(json!({ "keyword": EncodableKeyword::from(kw) }))
94+
let keyword = EncodableKeyword::from(kw);
95+
Ok(Json(GetResponse { keyword }))
9096
}

src/snapshots/crates_io__openapi__tests__openapi_snapshot.snap

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,21 @@ expression: response.json()
14251425
],
14261426
"responses": {
14271427
"200": {
1428+
"content": {
1429+
"application/json": {
1430+
"schema": {
1431+
"properties": {
1432+
"keyword": {
1433+
"$ref": "#/components/schemas/Keyword"
1434+
}
1435+
},
1436+
"required": [
1437+
"keyword"
1438+
],
1439+
"type": "object"
1440+
}
1441+
}
1442+
},
14281443
"description": "Successful Response"
14291444
}
14301445
},

0 commit comments

Comments
 (0)