implement get_series for JsonDb

This commit is contained in:
Akbar Rahman 2023-12-21 16:56:33 +00:00
parent eebbef6782
commit bd8266b795
Signed by: alvierahman90
GPG Key ID: 6217899F07CA2BDF
2 changed files with 23 additions and 2 deletions

View File

@ -29,8 +29,8 @@ pub trait Storage {
fn add_series_point(&mut self, category_id: i32, series_id: i32, series_point: NewSeriesPoint) -> Result<i32, Error>; fn add_series_point(&mut self, category_id: i32, series_id: i32, series_point: NewSeriesPoint) -> Result<i32, Error>;
//fn add_user(&mut self, id: i32, user: NewUser) -> Result<User, Error>; //fn add_user(&mut self, id: i32, user: NewUser) -> Result<User, Error>;
fn get_category(&self, id: i32) -> Option<Category>; fn get_category(&self, id: i32) -> Option<Category>;
//fn get_series(&self, id: i32) -> Result<Series, Error>; fn get_series(&self, category_id: i32, series_id: i32) -> Option<Series>;
//fn get_user(&self, id: i32) -> Result<User, Error>; //fn get_user(&self, id: i32) -> Option<User>;
//fn update_category(&mut self, id: i32, changeset: CategoryChangeset) -> Result<(), Error>; //fn update_category(&mut self, id: i32, changeset: CategoryChangeset) -> Result<(), Error>;
//fn update_series(&mut self, id: i32, changeset: SeriesChangeset) -> Result<(), Error>; //fn update_series(&mut self, id: i32, changeset: SeriesChangeset) -> Result<(), Error>;
//fn update_user(&mut self, id: i32, changeset: UserChangeset) -> Result<(), Error>; //fn update_user(&mut self, id: i32, changeset: UserChangeset) -> Result<(), Error>;

View File

@ -133,4 +133,25 @@ impl Storage for JsonDb {
None None
} }
fn get_series(&self, category_id: i32, series_id: i32) -> Option<Series> {
let data = self.load().ok()?;
let mut cat: Option<Category> = None;
for cat_opt in data {
if cat_opt.id == category_id {
cat = Some(cat_opt);
}
}
let cat = cat.unwrap();
for series_opt in cat.series {
if series_opt.id == series_id {
return Some(series_opt)
}
}
None
}
} }