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
{
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 {
create_form_feedback(wallet, HashMap::new(), None).await
}
async fn create_form_feedback(
wallet: &Wallet,
form: HashMap,
error: Option,
) -> Response {
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,
) -> Response {
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 {
update_form_feedback(id, wallet, HashMap::new(), None).await
}
async fn update_form_feedback(
id: i64,
wallet: &Wallet,
form: HashMap,
error: Option,
) -> Response {
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,
) -> Response {
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 {
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
}
}