diff options
author | Joris | 2021-01-03 13:40:40 +0100 |
---|---|---|
committer | Joris | 2021-01-03 13:54:20 +0100 |
commit | 11052951b74b9ad4b6a9412ae490086235f9154b (patch) | |
tree | 64526ac926c1bf470ea113f6cac8a33158684e8d /src/controller/categories.rs | |
parent | 371449b0e312a03162b78797b83dee9d81706669 (diff) |
Rewrite in Rust
Diffstat (limited to 'src/controller/categories.rs')
-rw-r--r-- | src/controller/categories.rs | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/src/controller/categories.rs b/src/controller/categories.rs new file mode 100644 index 0000000..b1a3664 --- /dev/null +++ b/src/controller/categories.rs @@ -0,0 +1,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 + } +} |