Skip to content

Commit f90f360

Browse files
committed
feat(tag-details): add the endpoint logic
1 parent 32de879 commit f90f360

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/endpoints/tags.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub async fn index(data: web::Data<AppData>) -> Result<impl Responder, ApiError>
2424
pub async fn detailed_index(data: web::Data<AppData>) -> Result<impl Responder, ApiError> {
2525
let mut pool = data.db.acquire().await.or(Err(ApiError::DbAcquireError))?;
2626

27-
let tags = Tag::get_tags(&mut pool).await?;
27+
let tags = Tag::get_detailed_tags(&mut pool).await?;
2828

2929
Ok(web::Json(ApiResponse {
3030
error: "".to_string(),

src/types/models/tag.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ pub struct FetchedTag {
1010
pub name: String,
1111
}
1212

13-
pub struct Tag;
13+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
14+
pub struct Tag {
15+
pub id: i32,
16+
pub name: String,
17+
pub display_name: String,
18+
}
1419

1520
impl Tag {
1621
pub async fn get_tags(pool: &mut PgConnection) -> Result<Vec<String>, ApiError> {
@@ -28,6 +33,37 @@ impl Tag {
2833
Ok(tags.into_iter().map(|x| x.name).collect::<Vec<String>>())
2934
}
3035

36+
pub async fn get_detailed_tags(conn: &mut PgConnection) -> Result<Vec<Tag>, ApiError> {
37+
struct QueryResult {
38+
id: i32,
39+
name: String,
40+
display_name: Option<String>,
41+
}
42+
let tags = sqlx::query_as!(
43+
QueryResult,
44+
"SELECT
45+
id,
46+
name,
47+
display_name
48+
FROM mod_tags"
49+
)
50+
.fetch_all(&mut *conn)
51+
.await
52+
.map_err(|err| {
53+
log::error!("Failed to fetch detailed tags: {}", err);
54+
ApiError::DbError
55+
})?;
56+
57+
Ok(tags
58+
.into_iter()
59+
.map(|i| Tag {
60+
id: i.id,
61+
name: i.name.clone(),
62+
display_name: i.display_name.unwrap_or(i.name),
63+
})
64+
.collect())
65+
}
66+
3167
pub async fn get_tag_ids(
3268
tags: Vec<String>,
3369
pool: &mut PgConnection,

0 commit comments

Comments
 (0)