aboutsummaryrefslogtreecommitdiff
path: root/src/validation/income.rs
blob: 0579f4a6104d2479f155007d088decad774a281a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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")?,
        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")?,
        date: NaiveDate::from_ymd_opt(year, month, 1)?,
    })
}