added a find function to model
This commit is contained in:
parent
125289ace1
commit
3e1e46ce2b
5 changed files with 16 additions and 11 deletions
|
@ -45,7 +45,7 @@ mod test {
|
||||||
pub fn test_db_and_model() {
|
pub fn test_db_and_model() {
|
||||||
let mut image_model: Model<Image> = Model::default();
|
let mut image_model: Model<Image> = Model::default();
|
||||||
image_model.load_from_db();
|
image_model.load_from_db();
|
||||||
if let Some(image) = image_model.get_item(|i| i.id == 3) {
|
if let Some(image) = image_model.find(|i| i.id == 3) {
|
||||||
let test_image = test_image("nccq5".into());
|
let test_image = test_image("nccq5".into());
|
||||||
assert_eq!(test_image.title, image.title);
|
assert_eq!(test_image.title, image.title);
|
||||||
} else {
|
} else {
|
||||||
|
@ -61,10 +61,10 @@ mod test {
|
||||||
let new_image = test_image("A newer image".into());
|
let new_image = test_image("A newer image".into());
|
||||||
match result {
|
match result {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
assert_eq!(&image, image_model.get_item(|i| i.id == 0).unwrap());
|
assert_eq!(&image, image_model.find(|i| i.id == 0).unwrap());
|
||||||
assert_ne!(
|
assert_ne!(
|
||||||
&new_image,
|
&new_image,
|
||||||
image_model.get_item(|i| i.id == 0).unwrap()
|
image_model.find(|i| i.id == 0).unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Err(e) => assert!(
|
Err(e) => assert!(
|
||||||
|
|
|
@ -36,11 +36,16 @@ impl<T> Model<T> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_item<P>(&self, f: P) -> Option<&T>
|
pub fn get_item(&self, index: i32) -> Option<&T>
|
||||||
|
{
|
||||||
|
self.items.get(index as usize)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn find<P>(&self, f: P) -> Option<&T>
|
||||||
where
|
where
|
||||||
P: FnMut(&&T) -> bool,
|
P: FnMut(&&T) -> bool,
|
||||||
{
|
{
|
||||||
self.items.iter().filter(f).next()
|
self.items.iter().find(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_item(&mut self, item: T, index: i32) -> Result<()> {
|
pub fn insert_item(&mut self, item: T, index: i32) -> Result<()> {
|
||||||
|
|
|
@ -88,7 +88,7 @@ mod test {
|
||||||
let mut presentation_model: Model<Presentation> =
|
let mut presentation_model: Model<Presentation> =
|
||||||
Model::default();
|
Model::default();
|
||||||
presentation_model.load_from_db();
|
presentation_model.load_from_db();
|
||||||
if let Some(presentation) = presentation_model.get_item(|p| p.id == 10) {
|
if let Some(presentation) = presentation_model.find(|p| p.id == 54) {
|
||||||
let test_presentation = test_presentation();
|
let test_presentation = test_presentation();
|
||||||
assert_eq!(&test_presentation, presentation);
|
assert_eq!(&test_presentation, presentation);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -282,7 +282,7 @@ You saved my soul"
|
||||||
pub fn test_db_and_model() {
|
pub fn test_db_and_model() {
|
||||||
let mut song_model: Model<Song> = Model::default();
|
let mut song_model: Model<Song> = Model::default();
|
||||||
song_model.load_from_db();
|
song_model.load_from_db();
|
||||||
if let Some(song) = song_model.get_item(|s| s.id == 7) {
|
if let Some(song) = song_model.find(|s| s.id == 7) {
|
||||||
let test_song = test_song();
|
let test_song = test_song();
|
||||||
assert_eq!(&test_song, song);
|
assert_eq!(&test_song, song);
|
||||||
} else {
|
} else {
|
||||||
|
@ -310,7 +310,7 @@ You saved my soul"
|
||||||
match song_model.update_item(song, 2) {
|
match song_model.update_item(song, 2) {
|
||||||
Ok(()) => assert_eq!(
|
Ok(()) => assert_eq!(
|
||||||
&cloned_song,
|
&cloned_song,
|
||||||
song_model.get_item(|s| s.id == 7).unwrap()
|
song_model.find(|s| s.id == 7).unwrap()
|
||||||
),
|
),
|
||||||
Err(e) => assert!(false, "{e}"),
|
Err(e) => assert!(false, "{e}"),
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ mod test {
|
||||||
pub fn test_db_and_model() {
|
pub fn test_db_and_model() {
|
||||||
let mut video_model: Model<Video> = Model::default();
|
let mut video_model: Model<Video> = Model::default();
|
||||||
video_model.load_from_db();
|
video_model.load_from_db();
|
||||||
if let Some(video) = video_model.get_item(|v| v.id == 3) {
|
if let Some(video) = video_model.find(|v| v.id == 73) {
|
||||||
let test_video = test_video("Getting started with Tokio. The ultimate starter guide to writing async Rust.".into());
|
let test_video = test_video("Getting started with Tokio. The ultimate starter guide to writing async Rust.".into());
|
||||||
assert_eq!(test_video.title, video.title);
|
assert_eq!(test_video.title, video.title);
|
||||||
} else {
|
} else {
|
||||||
|
@ -64,10 +64,10 @@ mod test {
|
||||||
let new_video = test_video("A newer video".into());
|
let new_video = test_video("A newer video".into());
|
||||||
match result {
|
match result {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
assert_eq!(&video, video_model.get_item(|v| v.id == 0).unwrap());
|
assert_eq!(&video, video_model.find(|v| v.id == 0).unwrap());
|
||||||
assert_ne!(
|
assert_ne!(
|
||||||
&new_video,
|
&new_video,
|
||||||
video_model.get_item(|v| v.id == 0).unwrap()
|
video_model.find(|v| v.id == 0).unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Err(e) => assert!(
|
Err(e) => assert!(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue