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;
use crate::models::*;
use std::io;
use serde_json;
use std::io;
#[derive(Debug)]
pub enum Error {
@ -26,7 +26,12 @@ impl From<serde_json::Error> for Error {
pub trait Storage {
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_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 get_category(&self, id: i32) -> Option<Category>;
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 {
match value {
Some(value) => JsonDb { value },
None => JsonDb { value: String::from("[]") },
None => JsonDb {
value: String::from("[]"),
},
}
}
@ -26,7 +28,7 @@ impl JsonDb {
Ok(d) => {
self.value = d;
Ok(())
},
}
Err(e) => Err(Error::from(e)),
}
}
@ -65,7 +67,6 @@ impl Storage for JsonDb {
}
let cat = cat.unwrap();
for series in cat.series.iter().clone() {
if series.id > max_id {
max_id = series.id;
@ -79,7 +80,12 @@ impl Storage for JsonDb {
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 cat: Option<&mut Category> = None;
let mut series: Option<&mut Series> = None;
@ -148,7 +154,7 @@ impl Storage for JsonDb {
for series_opt in cat.series {
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 models;
mod utils;
use wasm_bindgen::prelude::*;

View File

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

View File

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

View File

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

View File

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

View File

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