feat: Ensure consistent ordering of games, users, and user opinions, and add DerefMut for User.
This commit is contained in:
parent
f312da33b5
commit
0a526c0c57
@ -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());
|
||||
}
|
||||
|
||||
@ -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<Game>,
|
||||
@ -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);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user