aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoris2023-04-17 20:56:53 +0200
committerJoris2023-04-17 20:56:53 +0200
commit3932daa26360d6e03807381d0b8ffa2d0e704847 (patch)
tree099f92c176ba783bccc5c307927e1effe7c2cd9c /src
parent8dcb9c6fff82f9e353e0f92b417dd803199f4257 (diff)
Validate income date from body
Diffstat (limited to 'src')
-rw-r--r--src/controller/incomes.rs6
-rw-r--r--src/db/incomes.rs9
-rw-r--r--src/model/income.rs7
-rw-r--r--src/validation/income.rs13
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<i64> {
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<String, String>) -> Option<Create> {
+ let month = parse::<u32>(form, "month")?;
+ let year = parse::<i32>(form, "year")?;
+
Some(Create {
user_id: parse::<i64>(form, "user_id")?,
amount: parse::<i64>(form, "amount")?,
- month: parse::<u32>(form, "month")?,
- year: parse::<i32>(form, "year")?,
+ date: NaiveDate::from_ymd_opt(year, month, 1)?,
})
}
pub fn update(form: &HashMap<String, String>) -> Option<Update> {
+ let month = parse::<u32>(form, "month")?;
+ let year = parse::<i32>(form, "year")?;
+
Some(Update {
user_id: parse::<i64>(form, "user_id")?,
amount: parse::<i64>(form, "amount")?,
- month: parse::<u32>(form, "month")?,
- year: parse::<i32>(form, "year")?,
+ date: NaiveDate::from_ymd_opt(year, month, 1)?,
})
}