moving to using a filter function for getting the item

This commit is contained in:
Chris Cochrun 2024-10-09 14:47:07 -05:00
parent f3c3e98e16
commit 0bfa2c6089
6 changed files with 16 additions and 18 deletions

View file

@ -112,11 +112,6 @@ impl Model<Song> {
}
})
}
pub fn get_item(&self, index: i32) -> Option<&Song> {
self.items.iter().find(|s| s.id == index)
}
}
impl Song {
@ -287,7 +282,7 @@ You saved my soul"
pub fn test_db_and_model() {
let mut song_model: Model<Song> = Model::default();
song_model.load_from_db();
if let Some(song) = song_model.get_item(7) {
if let Some(song) = song_model.get_item(|s| s.id == 7) {
let test_song = test_song();
assert_eq!(&test_song, song);
} else {
@ -315,7 +310,7 @@ You saved my soul"
match song_model.update_item(song, 2) {
Ok(()) => assert_eq!(
&cloned_song,
song_model.get_item(2).unwrap()
song_model.get_item(|s| s.id == 7).unwrap()
),
Err(e) => assert!(false, "{e}"),
}