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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
use hyper::header::CONTENT_TYPE;
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::model::frequency::Frequency;
use crate::queries;
use crate::templates;
use crate::validation;
static PER_PAGE: i64 = 10;
pub async fn table(
wallet: &Wallet,
query: queries::Payments,
) -> Response<Body> {
let page = query.page.unwrap_or(1);
let count = db::payments::count(&wallet.pool, &query).await;
let payments =
db::payments::list_for_table(&wallet.pool, &query, PER_PAGE).await;
let max_page = (count.count as f32 / PER_PAGE as f32).ceil() as i64;
let mut context = Context::new();
context.insert("header", &templates::Header::Payments);
context.insert("connected_user", &wallet.user);
context.insert("payments", &payments);
context.insert("page", &page);
context.insert("max_page", &max_page);
context.insert("query", &query);
context.insert("count", &count.count);
context.insert("total_cost", &count.total_cost);
utils::template(
&wallet.assets,
&wallet.templates,
"payment/table.html",
context,
)
}
pub async fn create_form(
wallet: &Wallet,
query: queries::Payments,
) -> Response<Body> {
create_form_feedback(wallet, query, HashMap::new(), None).await
}
async fn create_form_feedback(
wallet: &Wallet,
query: queries::Payments,
form: HashMap<String, String>,
error: Option<String>,
) -> Response<Body> {
let users = db::users::list(&wallet.pool).await;
let categories = db::categories::list(&wallet.pool).await;
let mut context = Context::new();
context.insert("header", &templates::Header::Payments);
context.insert("connected_user", &wallet.user);
context.insert("users", &users);
context.insert("categories", &categories);
context.insert("query", &query);
context.insert("form", &form);
context.insert("error", &error);
utils::template(
&wallet.assets,
&wallet.templates,
"payment/create.html",
context,
)
}
pub async fn create(
wallet: &Wallet,
query: queries::Payments,
form: HashMap<String, String>,
) -> Response<Body> {
let error = |e: &str| {
create_form_feedback(wallet, query, form.clone(), Some(e.to_string()))
};
match validation::payment::create(&form) {
Some(create_payment) => {
match db::payments::create(&wallet.pool, &create_payment).await {
Some(id) => {
let row = db::payments::get_row(
&wallet.pool,
id,
create_payment.frequency,
)
.await;
let page = (row - 1) / PER_PAGE + 1;
let query = queries::Payments {
page: Some(page),
search: None,
frequency: Some(create_payment.frequency),
highlight: Some(id),
};
utils::redirect(&format!(
"/{}",
queries::payments_url(query)
))
}
None => error("Erreur serveur.").await,
}
}
None => error("Erreur lors de la validation du formulaire.").await,
}
}
pub async fn update_form(
id: i64,
wallet: &Wallet,
query: queries::Payments,
) -> Response<Body> {
update_form_feedback(id, wallet, query, HashMap::new(), None).await
}
async fn update_form_feedback(
id: i64,
wallet: &Wallet,
query: queries::Payments,
form: HashMap<String, String>,
error: Option<String>,
) -> Response<Body> {
let payment = db::payments::get_for_form(&wallet.pool, id).await;
let users = db::users::list(&wallet.pool).await;
let categories = db::categories::list(&wallet.pool).await;
let mut context = Context::new();
context.insert("header", &templates::Header::Payments);
context.insert("connected_user", &wallet.user);
context.insert("id", &id);
context.insert("payment", &payment);
context.insert("users", &users);
context.insert("categories", &categories);
context.insert("query", &query);
context.insert("form", &form);
context.insert("error", &error);
utils::template(
&wallet.assets,
&wallet.templates,
"payment/update.html",
context,
)
}
pub async fn update(
id: i64,
wallet: &Wallet,
query: queries::Payments,
form: HashMap<String, String>,
) -> Response<Body> {
let error = |e: &str| {
update_form_feedback(
id,
wallet,
query.clone(),
form.clone(),
Some(e.to_string()),
)
};
match validation::payment::update(&form) {
Some(update_payment) => {
if db::payments::update(&wallet.pool, id, &update_payment).await {
let frequency = query.frequency.unwrap_or(Frequency::Punctual);
let row =
db::payments::get_row(&wallet.pool, id, frequency).await;
let page = (row - 1) / PER_PAGE + 1;
let query = queries::Payments {
page: Some(page),
search: None,
frequency: Some(frequency),
highlight: Some(id),
};
utils::redirect(&format!("/{}", queries::payments_url(query)))
} else {
error("Erreur serveur").await
}
}
None => error("Erreur lors de la validation du formulaire.").await,
}
}
pub async fn delete(
id: i64,
wallet: &Wallet,
query: queries::Payments,
) -> Response<Body> {
if db::payments::delete(&wallet.pool, id).await {
let query = queries::Payments {
highlight: None,
..query
};
utils::redirect(&format!("/{}", queries::payments_url(query)))
} else {
update_form_feedback(
id,
wallet,
query,
HashMap::new(),
Some("Erreur serveur".to_string()),
)
.await
}
}
pub async fn search_category(
wallet: &Wallet,
query: queries::PaymentCategory,
) -> Response<Body> {
match db::payments::search_category(&wallet.pool, query.payment_name).await
{
Some(category_id) => utils::with_header(
Response::new(format!("{}", category_id).into()),
CONTENT_TYPE,
"application/json",
),
None => utils::not_found(),
}
}
|