Skip to content

Commit c0985ee

Browse files
authored
Merge pull request #33 from geode-sdk/tag-extras
Add display_name for tags, rename `cheats` to `cheat`
2 parents 32d4e63 + f90f360 commit c0985ee

File tree

6 files changed

+97
-2
lines changed

6 files changed

+97
-2
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- Add down migration script here
2+
3+
ALTER TABLE mod_tags DROP COLUMN display_name;
4+
5+
UPDATE mod_tags SET name = 'cheats'
6+
WHERE name = 'cheat';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
-- Add up migration script here
2+
3+
ALTER TABLE mod_tags ADD COLUMN display_name TEXT;
4+
5+
UPDATE mod_tags SET display_name = 'Universal'
6+
WHERE name = 'universal';
7+
UPDATE mod_tags SET display_name = 'Gameplay'
8+
WHERE name = 'gameplay';
9+
UPDATE mod_tags SET display_name = 'Editor'
10+
WHERE name = 'editor';
11+
UPDATE mod_tags SET display_name = 'Offline'
12+
WHERE name = 'offline';
13+
UPDATE mod_tags SET display_name = 'Online'
14+
WHERE name = 'online';
15+
UPDATE mod_tags SET display_name = 'Enhancement'
16+
WHERE name = 'enhancement';
17+
UPDATE mod_tags SET display_name = 'Music'
18+
WHERE name = 'music';
19+
UPDATE mod_tags SET display_name = 'Interface'
20+
WHERE name = 'interface';
21+
UPDATE mod_tags SET display_name = 'Bugfix'
22+
WHERE name = 'bugfix';
23+
UPDATE mod_tags SET display_name = 'Utility'
24+
WHERE name = 'utility';
25+
UPDATE mod_tags SET display_name = 'Performance'
26+
WHERE name = 'performance';
27+
UPDATE mod_tags SET display_name = 'Customization'
28+
WHERE name = 'customization';
29+
UPDATE mod_tags SET display_name = 'Content'
30+
WHERE name = 'content';
31+
UPDATE mod_tags SET display_name = 'Developer'
32+
WHERE name = 'developer';
33+
UPDATE mod_tags SET display_name = 'Cheat', name = 'cheat'
34+
WHERE name = 'cheats';
35+
UPDATE mod_tags SET display_name = 'Paid'
36+
WHERE name = 'paid';
37+
UPDATE mod_tags SET display_name = 'Joke'
38+
WHERE name = 'joke';
39+
UPDATE mod_tags SET display_name = 'Modtober 2024'
40+
WHERE name = 'modtober24';

src/endpoints/tags.rs

+12
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,15 @@ pub async fn index(data: web::Data<AppData>) -> Result<impl Responder, ApiError>
1919
payload: tags,
2020
}))
2121
}
22+
23+
#[get("/v1/detailed-tags")]
24+
pub async fn detailed_index(data: web::Data<AppData>) -> Result<impl Responder, ApiError> {
25+
let mut pool = data.db.acquire().await.or(Err(ApiError::DbAcquireError))?;
26+
27+
let tags = Tag::get_detailed_tags(&mut pool).await?;
28+
29+
Ok(web::Json(ApiResponse {
30+
error: "".to_string(),
31+
payload: tags,
32+
}))
33+
}

src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ async fn main() -> anyhow::Result<()> {
124124
.service(endpoints::developers::get_me)
125125
.service(endpoints::developers::update_developer)
126126
.service(endpoints::tags::index)
127+
.service(endpoints::tags::detailed_index)
127128
.service(endpoints::stats::get_stats)
128129
.service(health)
129130
})

src/types/models/tag.rs

+37-1
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)