Skip to content

Commit

Permalink
Merge pull request #33 from geode-sdk/tag-extras
Browse files Browse the repository at this point in the history
Add display_name for tags, rename `cheats` to `cheat`
  • Loading branch information
Fleeym authored Nov 13, 2024
2 parents 32d4e63 + f90f360 commit c0985ee
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions migrations/20241113145919_add_display_name_to_tags.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- Add down migration script here

ALTER TABLE mod_tags DROP COLUMN display_name;

UPDATE mod_tags SET name = 'cheats'
WHERE name = 'cheat';
40 changes: 40 additions & 0 deletions migrations/20241113145919_add_display_name_to_tags.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
-- Add up migration script here

ALTER TABLE mod_tags ADD COLUMN display_name TEXT;

UPDATE mod_tags SET display_name = 'Universal'
WHERE name = 'universal';
UPDATE mod_tags SET display_name = 'Gameplay'
WHERE name = 'gameplay';
UPDATE mod_tags SET display_name = 'Editor'
WHERE name = 'editor';
UPDATE mod_tags SET display_name = 'Offline'
WHERE name = 'offline';
UPDATE mod_tags SET display_name = 'Online'
WHERE name = 'online';
UPDATE mod_tags SET display_name = 'Enhancement'
WHERE name = 'enhancement';
UPDATE mod_tags SET display_name = 'Music'
WHERE name = 'music';
UPDATE mod_tags SET display_name = 'Interface'
WHERE name = 'interface';
UPDATE mod_tags SET display_name = 'Bugfix'
WHERE name = 'bugfix';
UPDATE mod_tags SET display_name = 'Utility'
WHERE name = 'utility';
UPDATE mod_tags SET display_name = 'Performance'
WHERE name = 'performance';
UPDATE mod_tags SET display_name = 'Customization'
WHERE name = 'customization';
UPDATE mod_tags SET display_name = 'Content'
WHERE name = 'content';
UPDATE mod_tags SET display_name = 'Developer'
WHERE name = 'developer';
UPDATE mod_tags SET display_name = 'Cheat', name = 'cheat'
WHERE name = 'cheats';
UPDATE mod_tags SET display_name = 'Paid'
WHERE name = 'paid';
UPDATE mod_tags SET display_name = 'Joke'
WHERE name = 'joke';
UPDATE mod_tags SET display_name = 'Modtober 2024'
WHERE name = 'modtober24';
12 changes: 12 additions & 0 deletions src/endpoints/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ pub async fn index(data: web::Data<AppData>) -> Result<impl Responder, ApiError>
payload: tags,
}))
}

#[get("/v1/detailed-tags")]
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_detailed_tags(&mut pool).await?;

Ok(web::Json(ApiResponse {
error: "".to_string(),
payload: tags,
}))
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ async fn main() -> anyhow::Result<()> {
.service(endpoints::developers::get_me)
.service(endpoints::developers::update_developer)
.service(endpoints::tags::index)
.service(endpoints::tags::detailed_index)
.service(endpoints::stats::get_stats)
.service(health)
})
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 c0985ee

Please sign in to comment.