aboutsummaryrefslogtreecommitdiff
path: root/src/controller/categories.rs
blob: b1a3664eab7ac56e940e3acee018bcfca68aea9b (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use hyper::{Body, Response};
use std::collections::HashMap;
use tera::Context;

use crate::controller::utils;
use crate::controller::wallet::Wallet;
use crate::db;
use crate::queries;
use crate::templates;
use crate::validation;

pub async fn table(
    wallet: &Wallet,
    query: queries::Categories,
) -> Response<Body> {
    let categories = db::categories::list(&wallet.pool).await;

    let mut context = Context::new();
    context.insert("header", &templates::Header::Categories);
    context.insert("connected_user", &wallet.user);
    context.insert("categories", &categories);
    context.insert("highlight", &query.highlight);

    utils::template(
        &wallet.assets,
        &wallet.templates,
        "category/table.html",
        context,
    )
}

pub async fn create_form(wallet: &Wallet) -> Response<Body> {
    create_form_feedback(wallet, HashMap::new(), None).await
}

async fn create_form_feedback(
    wallet: &Wallet,
    form: HashMap<String, String>,
    error: Option<String>,
) -> Response<Body> {
    let mut context = Context::new();
    context.insert("header", &templates::Header::Categories);
    context.insert("connected_user", &wallet.user.clone());
    context.insert("form", &form);
    context.insert("error", &error);

    utils::template(
        &wallet.assets,
        &wallet.templates,
        "category/create.html",
        context,
    )
}

pub async fn create(
    wallet: &Wallet,
    form: HashMap<String, String>,
) -> Response<Body> {
    let error = |e: &str| {
        create_form_feedback(wallet, form.clone(), Some(e.to_string()))
    };

    match validation::category::create(&form) {
        Some(category) => {
            match db::categories::create(&wallet.pool, &category).await {
                Some(id) => {
                    utils::redirect(&format!("/categories?highlight={}", id))
                }
                None => error("Erreur serveur").await,
            }
        }
        None => error("Erreur lors de la validation du formulaire.").await,
    }
}

pub async fn update_form(id: i64, wallet: &Wallet) -> Response<Body> {
    update_form_feedback(id, wallet, HashMap::new(), None).await
}

async fn update_form_feedback(
    id: i64,
    wallet: &Wallet,
    form: HashMap<String, String>,
    error: Option<String>,
) -> Response<Body> {
    let category = db::categories::get(&wallet.pool, id).await;
    let is_category_used =
        db::payments::is_category_used(&wallet.pool, id).await;

    let mut context = Context::new();
    context.insert("header", &templates::Header::Categories);
    context.insert("connected_user", &wallet.user);
    context.insert("id", &id);
    context.insert("category", &category);
    context.insert("is_category_used", &is_category_used);
    context.insert("form", &form);
    context.insert("error", &error);

    utils::template(
        &wallet.assets,
        &wallet.templates,
        "category/update.html",
        context,
    )
}

pub async fn update(
    id: i64,
    wallet: &Wallet,
    form: HashMap<String, String>,
) -> Response<Body> {
    let error = |e: &str| {
        update_form_feedback(id, wallet, form.clone(), Some(e.to_string()))
    };

    match validation::category::update(&form) {
        Some(update_category) => {
            if db::categories::update(&wallet.pool, id, &update_category).await
            {
                utils::redirect(&format!("/categories?highlight={}", id))
            } else {
                error("Erreur serveur").await
            }
        }
        None => error("Erreur lors de la validation du formulaire.").await,
    }
}

pub async fn delete(id: i64, wallet: &Wallet) -> Response<Body> {
    if db::categories::delete(&wallet.pool, id).await {
        utils::redirect("/categories")
    } else {
        update_form_feedback(
            id,
            wallet,
            HashMap::new(),
            Some("Erreur serveur".to_string()),
        )
        .await
    }
}