Skip to content

Commit

Permalink
feat(tag-details): add the endpoint logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Fleeym committed Nov 13, 2024
1 parent 32de879 commit f90f360
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/endpoints/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub async fn index(data: web::Data<AppData>) -> Result<impl Responder, ApiError>
pub async fn detailed_index(data: web::Data<AppData>) -> Result<impl Responder, ApiError> {
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(),
Expand Down
38 changes: 37 additions & 1 deletion src/types/models/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<String>, ApiError> {
Expand All @@ -28,6 +33,37 @@ impl Tag {
Ok(tags.into_iter().map(|x| x.name).collect::<Vec<String>>())
}

pub async fn get_detailed_tags(conn: &mut PgConnection) -> Result<Vec<Tag>, ApiError> {
struct QueryResult {
id: i32,
name: String,
display_name: Option<String>,
}
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<String>,
pool: &mut PgConnection,
Expand Down

0 comments on commit f90f360

Please sign in to comment.