This commit is contained in:
Akbar Rahman 2023-12-22 11:46:57 +00:00
parent bd8266b795
commit 53208b73f9
Signed by: alvierahman90
GPG Key ID: 6217899F07CA2BDF
8 changed files with 33 additions and 22 deletions

View File

@ -1,8 +1,8 @@
pub mod json; pub mod json;
use crate::models::*; use crate::models::*;
use std::io;
use serde_json; use serde_json;
use std::io;
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
@ -18,7 +18,7 @@ impl From<io::Error> for Error {
} }
impl From<serde_json::Error> for Error { impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self{ fn from(e: serde_json::Error) -> Self {
Self::Json(e.to_string()) Self::Json(e.to_string())
} }
} }
@ -26,7 +26,12 @@ impl From<serde_json::Error> for Error {
pub trait Storage { pub trait Storage {
fn add_category(&mut self, category: NewCategory) -> Result<i32, Error>; fn add_category(&mut self, category: NewCategory) -> Result<i32, Error>;
fn add_series(&mut self, category_id: i32, series: NewSeries) -> Result<i32, Error>; fn add_series(&mut self, category_id: i32, series: NewSeries) -> Result<i32, Error>;
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, category_id: i32, series_id: i32) -> Option<Series>; fn get_series(&self, category_id: i32, series_id: i32) -> Option<Series>;

View File

@ -9,7 +9,9 @@ impl JsonDb {
pub fn new(value: Option<String>) -> Self { pub fn new(value: Option<String>) -> Self {
match value { match value {
Some(value) => JsonDb { value }, Some(value) => JsonDb { value },
None => JsonDb { value: String::from("[]") }, None => JsonDb {
value: String::from("[]"),
},
} }
} }
@ -26,7 +28,7 @@ impl JsonDb {
Ok(d) => { Ok(d) => {
self.value = d; self.value = d;
Ok(()) Ok(())
}, }
Err(e) => Err(Error::from(e)), Err(e) => Err(Error::from(e)),
} }
} }
@ -41,7 +43,7 @@ impl Storage for JsonDb {
max_id = cat.id max_id = cat.id
} }
} }
let created = Category::new(max_id+1, category); let created = Category::new(max_id + 1, category);
data.push(created); data.push(created);
self.save(data)?; self.save(data)?;
@ -61,25 +63,29 @@ impl Storage for JsonDb {
} }
if let None = cat { if let None = cat {
return Err(Error::Generic ("Unable to find category".to_owned())); return Err(Error::Generic("Unable to find category".to_owned()));
} }
let cat = cat.unwrap(); let cat = cat.unwrap();
for series in cat.series.iter().clone() { for series in cat.series.iter().clone() {
if series.id > max_id { if series.id > max_id {
max_id = series.id; max_id = series.id;
} }
} }
let created = Series::new(max_id+1, series); let created = Series::new(max_id + 1, series);
cat.series.push(created); cat.series.push(created);
self.save(data)?; self.save(data)?;
Ok(max_id + 1) Ok(max_id + 1)
} }
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> {
let mut max_id = 1; let mut max_id = 1;
let mut cat: Option<&mut Category> = None; let mut cat: Option<&mut Category> = None;
let mut series: Option<&mut Series> = None; let mut series: Option<&mut Series> = None;
@ -93,7 +99,7 @@ impl Storage for JsonDb {
} }
if let None = cat { if let None = cat {
return Err(Error::Generic ("Unable to find category".to_owned())); return Err(Error::Generic("Unable to find category".to_owned()));
} }
let cat = cat.unwrap(); let cat = cat.unwrap();
@ -105,7 +111,7 @@ impl Storage for JsonDb {
} }
if let None = series { if let None = series {
return Err(Error::Generic ("Unable to find series".to_owned())); return Err(Error::Generic("Unable to find series".to_owned()));
} }
let series: &mut Series = series.unwrap(); let series: &mut Series = series.unwrap();
@ -148,7 +154,7 @@ impl Storage for JsonDb {
for series_opt in cat.series { for series_opt in cat.series {
if series_opt.id == series_id { if series_opt.id == series_id {
return Some(series_opt) return Some(series_opt);
} }
} }

View File

@ -1,5 +1,5 @@
mod utils;
pub mod models;
pub mod db; pub mod db;
pub mod models;
mod utils;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;

View File

@ -1,5 +1,5 @@
use crate::models::Series; use crate::models::Series;
use serde::{ Serialize, Deserialize }; use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Category { pub struct Category {

View File

@ -1,6 +1,6 @@
use chrono;
use super::series_point::SeriesPoint; use super::series_point::SeriesPoint;
use serde::{ Serialize, Deserialize }; use chrono;
use serde::{Deserialize, Serialize};
use serde_with; use serde_with;
#[serde_with::serde_as] #[serde_with::serde_as]

View File

@ -1,6 +1,6 @@
use chrono;
use super::series_type::SeriesType; use super::series_type::SeriesType;
use serde::{ Serialize, Deserialize }; use chrono;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct SeriesPoint { pub struct SeriesPoint {

View File

@ -1,4 +1,4 @@
use serde::{ Serialize, Deserialize }; use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub enum SeriesType { pub enum SeriesType {

View File

@ -1,4 +1,4 @@
use serde::{ Serialize, Deserialize }; use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct User { pub struct User {