From 08ba19e32c8faa19015a04e3181940387e1280ba Mon Sep 17 00:00:00 2001 From: Iris Date: Tue, 24 Oct 2023 12:40:01 +0200 Subject: [PATCH] feat: order achievements categories based on ids --- src/endpoints/achievements/fetch.rs | 5 ++++- src/models.rs | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/endpoints/achievements/fetch.rs b/src/endpoints/achievements/fetch.rs index c1ac5415..4f52da3e 100644 --- a/src/endpoints/achievements/fetch.rs +++ b/src/endpoints/achievements/fetch.rs @@ -52,6 +52,7 @@ pub async fn handler( doc! { "$project": { "_id": 0, + "category_id": "$id", "category_name": "$name", "category_desc": "$desc", "category_img_url": "$img_url", @@ -82,12 +83,13 @@ pub async fn handler( }, doc! { "$group": { - "_id": { "category_name": "$category_name", "category_desc": "$category_desc", "category_img_url": "$category_img_url", "category_type": "$category_type" }, + "_id": { "category_id": "$category_id", "category_name": "$category_name", "category_desc": "$category_desc", "category_img_url": "$category_img_url", "category_type": "$category_type" }, "achievements": { "$push": "$achievements" } } }, doc! { "$project": { + "category_id": "$_id.category_id", "category_name": "$_id.category_name", "category_desc": "$_id.category_desc", "category_img_url": "$_id.category_img_url", @@ -111,6 +113,7 @@ pub async fn handler( _ => continue, } } + achievements.sort_by(|a, b| a.category_id.cmp(&b.category_id)); (StatusCode::OK, Json(achievements)).into_response() } Err(e) => get_error(format!("Error fetching user achievements: {}", e)), diff --git a/src/models.rs b/src/models.rs index 7571acb1..df4d8645 100644 --- a/src/models.rs +++ b/src/models.rs @@ -107,6 +107,7 @@ pub_struct!(Debug, Serialize, Deserialize; AchievementCategoryDocument { }); pub_struct!(Debug, Serialize, Deserialize; UserAchievements { + category_id: u32, category_name: String, category_desc: String, category_img_url: String,