From 3932daa26360d6e03807381d0b8ffa2d0e704847 Mon Sep 17 00:00:00 2001 From: Joris Date: Mon, 17 Apr 2023 20:56:53 +0200 Subject: Validate income date from body --- src/controller/incomes.rs | 6 ++---- src/db/incomes.rs | 9 ++++----- src/model/income.rs | 7 +++---- src/validation/income.rs | 13 +++++++++---- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/controller/incomes.rs b/src/controller/incomes.rs index b7fb3ed..cc66ed6 100644 --- a/src/controller/incomes.rs +++ b/src/controller/incomes.rs @@ -97,8 +97,7 @@ pub async fn create( if !db::incomes::defined_at( &wallet.pool, income.user_id, - income.month, - income.year, + income.date, ) .await .is_empty() @@ -180,8 +179,7 @@ pub async fn update( let existing_incomes = db::incomes::defined_at( &wallet.pool, income.user_id, - income.month, - income.year, + income.date, ) .await; if existing_incomes.into_iter().any(|eid| eid != id) { diff --git a/src/db/incomes.rs b/src/db/incomes.rs index f2eaf1c..2952d62 100644 --- a/src/db/incomes.rs +++ b/src/db/incomes.rs @@ -139,7 +139,7 @@ VALUES "#, ) .bind(i.user_id) - .bind(NaiveDate::from_ymd_opt(i.year, i.month, 1)?) + .bind(i.date) .bind(i.amount) .execute(pool) .await; @@ -156,8 +156,7 @@ VALUES pub async fn defined_at( pool: &SqlitePool, user_id: i64, - month: u32, - year: i32, + date: NaiveDate, ) -> Vec { let query = r#" SELECT @@ -172,7 +171,7 @@ WHERE let res = sqlx::query(&query) .bind(user_id) - .bind(NaiveDate::from_ymd(year, month, 1)) + .bind(date) .map(|row: SqliteRow| row.get("id")) .fetch_all(pool) .await; @@ -202,7 +201,7 @@ WHERE "#, ) .bind(i.user_id) - .bind(NaiveDate::from_ymd(i.year, i.month, 1)) + .bind(i.date) .bind(i.amount) .bind(id) .execute(pool) diff --git a/src/model/income.rs b/src/model/income.rs index 7bc888f..ef97b56 100644 --- a/src/model/income.rs +++ b/src/model/income.rs @@ -1,3 +1,4 @@ +use chrono::NaiveDate; use serde::Serialize; #[derive(Debug, Clone, sqlx::FromRow, Serialize)] @@ -27,14 +28,12 @@ pub struct Form { pub struct Create { pub user_id: i64, pub amount: i64, - pub month: u32, - pub year: i32, + pub date: NaiveDate, } #[derive(Debug)] pub struct Update { pub user_id: i64, pub amount: i64, - pub month: u32, - pub year: i32, + pub date: NaiveDate, } diff --git a/src/validation/income.rs b/src/validation/income.rs index 972e42a..0579f4a 100644 --- a/src/validation/income.rs +++ b/src/validation/income.rs @@ -1,22 +1,27 @@ +use chrono::NaiveDate; use std::collections::HashMap; use crate::model::income::{Create, Update}; use crate::validation::utils::*; pub fn create(form: &HashMap) -> Option { + let month = parse::(form, "month")?; + let year = parse::(form, "year")?; + Some(Create { user_id: parse::(form, "user_id")?, amount: parse::(form, "amount")?, - month: parse::(form, "month")?, - year: parse::(form, "year")?, + date: NaiveDate::from_ymd_opt(year, month, 1)?, }) } pub fn update(form: &HashMap) -> Option { + let month = parse::(form, "month")?; + let year = parse::(form, "year")?; + Some(Update { user_id: parse::(form, "user_id")?, amount: parse::(form, "amount")?, - month: parse::(form, "month")?, - year: parse::(form, "year")?, + date: NaiveDate::from_ymd_opt(year, month, 1)?, }) } -- cgit v1.2.3