diff --git a/backend/src/main.rs b/backend/src/main.rs index 3862ac6..6232937 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -75,6 +75,8 @@ async fn add_game( games.push(game.clone()); + games.sort_unstable_by(|g1, g2| g1.title.cmp(&g2.title)); + let users = user_list.lock().await; save_state(&games, &users); @@ -113,6 +115,10 @@ async fn add_opinion( existing.would_play = req.would_play; } else { user.person.opinion.push(opinion); + + user.person + .opinion + .sort_unstable_by(|o1, o2| o1.title.cmp(&o2.title)); } result = Some(user.person.clone()); } diff --git a/backend/src/store.rs b/backend/src/store.rs index 563eefe..90098a4 100644 --- a/backend/src/store.rs +++ b/backend/src/store.rs @@ -17,6 +17,12 @@ impl std::ops::Deref for User { } } +impl std::ops::DerefMut for User { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.person + } +} + #[derive(Serialize, Deserialize)] pub struct PersistentState { pub games: Vec, @@ -26,10 +32,17 @@ pub struct PersistentState { pub const STATE_FILE: &str = "state.json"; pub fn save_state(games: &[Game], users: &[User]) { - let state = PersistentState { - games: games.to_vec(), - users: users.to_vec(), - }; + let mut games = games.to_vec(); + games.sort_unstable_by(|g1, g2| g1.title.cmp(&g2.title)); + + let mut users = users.to_vec(); + users.sort_unstable_by(|u1, u2| u1.name.cmp(&u2.name)); + + users.iter_mut().for_each(|u| { + u.opinion.sort_unstable_by(|o1, o2| o1.title.cmp(&o2.title)); + }); + + let state = PersistentState { games, users }; if let Ok(file) = File::create(STATE_FILE) { let _ = serde_json::to_writer_pretty(file, &state); }