diff --git a/Cargo.lock b/Cargo.lock index 85bc596..b62764c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1032,7 +1032,7 @@ dependencies = [ [[package]] name = "geode-index" -version = "0.31.1" +version = "0.32.0" dependencies = [ "actix-cors", "actix-web", diff --git a/migrations/20241113145919_add_display_name_to_tags.down.sql b/migrations/20241113145919_add_display_name_to_tags.down.sql new file mode 100644 index 0000000..532f55a --- /dev/null +++ b/migrations/20241113145919_add_display_name_to_tags.down.sql @@ -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'; diff --git a/migrations/20241113145919_add_display_name_to_tags.up.sql b/migrations/20241113145919_add_display_name_to_tags.up.sql new file mode 100644 index 0000000..dc8f130 --- /dev/null +++ b/migrations/20241113145919_add_display_name_to_tags.up.sql @@ -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'; diff --git a/src/endpoints/tags.rs b/src/endpoints/tags.rs index 4132b84..8bc59e1 100644 --- a/src/endpoints/tags.rs +++ b/src/endpoints/tags.rs @@ -19,3 +19,15 @@ pub async fn index(data: web::Data) -> Result payload: tags, })) } + +#[get("/v1/detailed-tags")] +pub async fn detailed_index(data: web::Data) -> Result { + 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, + })) +} diff --git a/src/main.rs b/src/main.rs index bdeec61..0b746bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) }) 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,