diff --git a/src/endpoints/tags.rs b/src/endpoints/tags.rs index 914ab8d..8bc59e1 100644 --- a/src/endpoints/tags.rs +++ b/src/endpoints/tags.rs @@ -24,7 +24,7 @@ pub async fn index(data: web::Data) -> Result pub async fn detailed_index(data: web::Data) -> Result { let mut pool = data.db.acquire().await.or(Err(ApiError::DbAcquireError))?; - let tags = Tag::get_tags(&mut pool).await?; + let tags = Tag::get_detailed_tags(&mut pool).await?; Ok(web::Json(ApiResponse { error: "".to_string(), diff --git a/src/types/models/tag.rs b/src/types/models/tag.rs index f6af92c..4f0c179 100644 --- a/src/types/models/tag.rs +++ b/src/types/models/tag.rs @@ -10,7 +10,12 @@ pub struct FetchedTag { pub name: String, } -pub struct Tag; +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub struct Tag { + pub id: i32, + pub name: String, + pub display_name: String, +} impl Tag { pub async fn get_tags(pool: &mut PgConnection) -> Result, ApiError> { @@ -28,6 +33,37 @@ impl Tag { Ok(tags.into_iter().map(|x| x.name).collect::>()) } + pub async fn get_detailed_tags(conn: &mut PgConnection) -> Result, ApiError> { + struct QueryResult { + id: i32, + name: String, + display_name: Option, + } + let tags = sqlx::query_as!( + QueryResult, + "SELECT + id, + name, + display_name + FROM mod_tags" + ) + .fetch_all(&mut *conn) + .await + .map_err(|err| { + log::error!("Failed to fetch detailed tags: {}", err); + ApiError::DbError + })?; + + Ok(tags + .into_iter() + .map(|i| Tag { + id: i.id, + name: i.name.clone(), + display_name: i.display_name.unwrap_or(i.name), + }) + .collect()) + } + pub async fn get_tag_ids( tags: Vec, pool: &mut PgConnection,