aboutsummaryrefslogtreecommitdiff
path: root/src/controller
diff options
context:
space:
mode:
Diffstat (limited to 'src/controller')
-rw-r--r--src/controller/balance.rs6
-rw-r--r--src/controller/categories.rs20
-rw-r--r--src/controller/error.rs10
-rw-r--r--src/controller/incomes.rs23
-rw-r--r--src/controller/login.rs14
-rw-r--r--src/controller/payments.rs22
-rw-r--r--src/controller/statistics.rs6
-rw-r--r--src/controller/utils.rs30
8 files changed, 75 insertions, 56 deletions
diff --git a/src/controller/balance.rs b/src/controller/balance.rs
index adde8a9..c5d9d4a 100644
--- a/src/controller/balance.rs
+++ b/src/controller/balance.rs
@@ -1,4 +1,6 @@
-use hyper::{Body, Response};
+use http_body_util::Full;
+use hyper::body::Bytes;
+use hyper::Response;
use std::collections::HashMap;
use tera::Context;
@@ -9,7 +11,7 @@ use crate::model::user::User;
use crate::payer;
use crate::templates;
-pub async fn get(wallet: &Wallet) -> Response<Body> {
+pub async fn get(wallet: &Wallet) -> Response<Full<Bytes>> {
let users = db::users::list(&wallet.pool).await;
let incomes_from = db::incomes::defined_for_all(&wallet.pool).await;
diff --git a/src/controller/categories.rs b/src/controller/categories.rs
index b1a3664..ff2d8e7 100644
--- a/src/controller/categories.rs
+++ b/src/controller/categories.rs
@@ -1,4 +1,6 @@
-use hyper::{Body, Response};
+use http_body_util::Full;
+use hyper::body::Bytes;
+use hyper::Response;
use std::collections::HashMap;
use tera::Context;
@@ -12,7 +14,7 @@ use crate::validation;
pub async fn table(
wallet: &Wallet,
query: queries::Categories,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
let categories = db::categories::list(&wallet.pool).await;
let mut context = Context::new();
@@ -29,7 +31,7 @@ pub async fn table(
)
}
-pub async fn create_form(wallet: &Wallet) -> Response<Body> {
+pub async fn create_form(wallet: &Wallet) -> Response<Full<Bytes>> {
create_form_feedback(wallet, HashMap::new(), None).await
}
@@ -37,7 +39,7 @@ async fn create_form_feedback(
wallet: &Wallet,
form: HashMap<String, String>,
error: Option<String>,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
let mut context = Context::new();
context.insert("header", &templates::Header::Categories);
context.insert("connected_user", &wallet.user.clone());
@@ -55,7 +57,7 @@ async fn create_form_feedback(
pub async fn create(
wallet: &Wallet,
form: HashMap<String, String>,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
let error = |e: &str| {
create_form_feedback(wallet, form.clone(), Some(e.to_string()))
};
@@ -73,7 +75,7 @@ pub async fn create(
}
}
-pub async fn update_form(id: i64, wallet: &Wallet) -> Response<Body> {
+pub async fn update_form(id: i64, wallet: &Wallet) -> Response<Full<Bytes>> {
update_form_feedback(id, wallet, HashMap::new(), None).await
}
@@ -82,7 +84,7 @@ async fn update_form_feedback(
wallet: &Wallet,
form: HashMap<String, String>,
error: Option<String>,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
let category = db::categories::get(&wallet.pool, id).await;
let is_category_used =
db::payments::is_category_used(&wallet.pool, id).await;
@@ -108,7 +110,7 @@ pub async fn update(
id: i64,
wallet: &Wallet,
form: HashMap<String, String>,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
let error = |e: &str| {
update_form_feedback(id, wallet, form.clone(), Some(e.to_string()))
};
@@ -126,7 +128,7 @@ pub async fn update(
}
}
-pub async fn delete(id: i64, wallet: &Wallet) -> Response<Body> {
+pub async fn delete(id: i64, wallet: &Wallet) -> Response<Full<Bytes>> {
if db::categories::delete(&wallet.pool, id).await {
utils::redirect("/categories")
} else {
diff --git a/src/controller/error.rs b/src/controller/error.rs
index 2a84255..0f6dcc1 100644
--- a/src/controller/error.rs
+++ b/src/controller/error.rs
@@ -1,5 +1,7 @@
+use http_body_util::Full;
+use hyper::body::Bytes;
use hyper::header::CACHE_CONTROL;
-use hyper::{Body, Response};
+use hyper::Response;
use std::collections::HashMap;
use tera::{Context, Tera};
@@ -7,7 +9,11 @@ use crate::controller::utils;
use crate::controller::wallet::Wallet;
// TODO error code
-pub fn error(wallet: &Wallet, title: &str, message: &str) -> Response<Body> {
+pub fn error(
+ wallet: &Wallet,
+ title: &str,
+ message: &str,
+) -> Response<Full<Bytes>> {
utils::with_headers(
Response::new(
template(&wallet.assets, &wallet.templates, title, message).into(),
diff --git a/src/controller/incomes.rs b/src/controller/incomes.rs
index cc66ed6..f22098b 100644
--- a/src/controller/incomes.rs
+++ b/src/controller/incomes.rs
@@ -1,6 +1,8 @@
use chrono::Datelike;
use chrono::Utc;
-use hyper::{Body, Response};
+use http_body_util::Full;
+use hyper::body::Bytes;
+use hyper::Response;
use std::collections::HashMap;
use tera::Context;
@@ -13,7 +15,10 @@ use crate::validation;
static PER_PAGE: i64 = 10;
-pub async fn table(wallet: &Wallet, query: queries::Incomes) -> Response<Body> {
+pub async fn table(
+ wallet: &Wallet,
+ query: queries::Incomes,
+) -> Response<Full<Bytes>> {
let page = query.page.unwrap_or(1);
let count = db::incomes::count(&wallet.pool).await;
let incomes = db::incomes::list(&wallet.pool, page, PER_PAGE).await;
@@ -53,7 +58,7 @@ static MONTHS: [&str; 12] = [
pub async fn create_form(
wallet: &Wallet,
query: queries::Incomes,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
create_form_feedback(wallet, query, HashMap::new(), None).await
}
@@ -62,7 +67,7 @@ async fn create_form_feedback(
query: queries::Incomes,
form: HashMap<String, String>,
error: Option<String>,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
let users = db::users::list(&wallet.pool).await;
let mut context = Context::new();
@@ -87,7 +92,7 @@ pub async fn create(
wallet: &Wallet,
query: queries::Incomes,
form: HashMap<String, String>,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
let error = |e: &str| {
create_form_feedback(wallet, query, form.clone(), Some(e.to_string()))
};
@@ -125,7 +130,7 @@ pub async fn update_form(
id: i64,
wallet: &Wallet,
query: queries::Incomes,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
update_form_feedback(id, wallet, query, HashMap::new(), None).await
}
@@ -135,7 +140,7 @@ async fn update_form_feedback(
query: queries::Incomes,
form: HashMap<String, String>,
error: Option<String>,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
let users = db::users::list(&wallet.pool).await;
let income = db::incomes::get(&wallet.pool, id).await;
@@ -163,7 +168,7 @@ pub async fn update(
wallet: &Wallet,
query: queries::Incomes,
form: HashMap<String, String>,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
let error = |e: &str| {
update_form_feedback(
id,
@@ -203,7 +208,7 @@ pub async fn delete(
id: i64,
wallet: &Wallet,
query: queries::Incomes,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
if db::incomes::delete(&wallet.pool, id).await {
utils::redirect(&format!("/incomes?page={}", query.page.unwrap_or(1)))
} else {
diff --git a/src/controller/login.rs b/src/controller/login.rs
index 036e6fc..dd50a3a 100644
--- a/src/controller/login.rs
+++ b/src/controller/login.rs
@@ -1,6 +1,8 @@
use bcrypt;
+use http_body_util::Full;
+use hyper::body::Bytes;
use hyper::header::SET_COOKIE;
-use hyper::{Body, Response};
+use hyper::Response;
use sqlx::sqlite::SqlitePool;
use std::collections::HashMap;
use tera::{Context, Tera};
@@ -18,7 +20,7 @@ pub async fn page(
assets: &HashMap<String, String>,
templates: &Tera,
error: Option<&str>,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
let connected_user: Option<User> = None;
let mut context = Context::new();
@@ -34,7 +36,7 @@ pub async fn login(
templates: &Tera,
form: HashMap<String, String>,
pool: SqlitePool,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
match validation::login::login(&form) {
Some(login) => {
match db::users::get_password_hash(&pool, login.email.clone()).await
@@ -88,14 +90,14 @@ async fn server_error(
assets: &HashMap<String, String>,
templates: &Tera,
msg: &str,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
page(assets, templates, Some(msg)).await
}
async fn not_authorized(
assets: &HashMap<String, String>,
templates: &Tera,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
page(
assets,
templates,
@@ -104,7 +106,7 @@ async fn not_authorized(
.await
}
-pub async fn logout(config: &Config, wallet: &Wallet) -> Response<Body> {
+pub async fn logout(config: &Config, wallet: &Wallet) -> Response<Full<Bytes>> {
if db::users::remove_login_token(&wallet.pool, wallet.user.id).await {
with_headers(
utils::redirect("/"),
diff --git a/src/controller/payments.rs b/src/controller/payments.rs
index 2663fa7..8184015 100644
--- a/src/controller/payments.rs
+++ b/src/controller/payments.rs
@@ -1,5 +1,7 @@
+use http_body_util::Full;
+use hyper::body::Bytes;
use hyper::header::CONTENT_TYPE;
-use hyper::{Body, Response};
+use hyper::Response;
use std::collections::HashMap;
use tera::Context;
@@ -16,7 +18,7 @@ static PER_PAGE: i64 = 10;
pub async fn table(
wallet: &Wallet,
query: queries::Payments,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
let page = query.page.unwrap_or(1);
let count = db::payments::count(&wallet.pool, &query).await;
let payments =
@@ -48,7 +50,7 @@ pub async fn table(
pub async fn create_form(
wallet: &Wallet,
query: queries::Payments,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
create_form_feedback(wallet, query, HashMap::new(), None).await
}
@@ -57,7 +59,7 @@ async fn create_form_feedback(
query: queries::Payments,
form: HashMap<String, String>,
error: Option<String>,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
let users = db::users::list(&wallet.pool).await;
let categories = db::categories::list(&wallet.pool).await;
@@ -82,7 +84,7 @@ pub async fn create(
wallet: &Wallet,
query: queries::Payments,
form: HashMap<String, String>,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
let error = |e: &str| {
create_form_feedback(wallet, query, form.clone(), Some(e.to_string()))
};
@@ -125,7 +127,7 @@ pub async fn update_form(
id: i64,
wallet: &Wallet,
query: queries::Payments,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
update_form_feedback(id, wallet, query, HashMap::new(), None).await
}
@@ -135,7 +137,7 @@ async fn update_form_feedback(
query: queries::Payments,
form: HashMap<String, String>,
error: Option<String>,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
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;
@@ -164,7 +166,7 @@ pub async fn update(
wallet: &Wallet,
query: queries::Payments,
form: HashMap<String, String>,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
let error = |e: &str| {
update_form_feedback(
id,
@@ -207,7 +209,7 @@ pub async fn delete(
id: i64,
wallet: &Wallet,
query: queries::Payments,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
if db::payments::delete(&wallet.pool, id).await {
let query = queries::Payments {
highlight: None,
@@ -229,7 +231,7 @@ pub async fn delete(
pub async fn search_category(
wallet: &Wallet,
query: queries::PaymentCategory,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
match db::payments::search_category(&wallet.pool, query.payment_name).await
{
Some(category_id) => utils::with_headers(
diff --git a/src/controller/statistics.rs b/src/controller/statistics.rs
index 38a5787..eb1e704 100644
--- a/src/controller/statistics.rs
+++ b/src/controller/statistics.rs
@@ -1,4 +1,6 @@
-use hyper::{Body, Response};
+use http_body_util::Full;
+use hyper::body::Bytes;
+use hyper::Response;
use tera::Context;
use crate::controller::utils;
@@ -6,7 +8,7 @@ use crate::controller::wallet::Wallet;
use crate::db;
use crate::templates;
-pub async fn get(wallet: &Wallet) -> Response<Body> {
+pub async fn get(wallet: &Wallet) -> Response<Full<Bytes>> {
let categories = db::categories::list(&wallet.pool).await;
let payments = db::payments::list_for_stats(&wallet.pool).await;
let incomes = db::incomes::total_each_month(&wallet.pool).await;
diff --git a/src/controller/utils.rs b/src/controller/utils.rs
index fb1d9ad..1b58c68 100644
--- a/src/controller/utils.rs
+++ b/src/controller/utils.rs
@@ -1,18 +1,18 @@
+use http_body_util::Full;
+use hyper::body::Bytes;
use hyper::header::{
HeaderName, HeaderValue, CACHE_CONTROL, CONTENT_TYPE, LOCATION,
};
-use hyper::{Body, Response, StatusCode};
+use hyper::{Response, StatusCode};
use std::collections::HashMap;
use tera::{Context, Tera};
-use tokio::fs::File;
-use tokio_util::codec::{BytesCodec, FramedRead};
use crate::controller::error;
pub fn with_headers(
- response: Response<Body>,
+ response: Response<Full<Bytes>>,
headers: Vec<(HeaderName, &str)>,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
let mut response = response;
let response_headers = response.headers_mut();
for (name, value) in headers {
@@ -26,7 +26,7 @@ pub fn template(
templates: &Tera,
path: &str,
context: Context,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
let mut context = context;
context.insert("assets", assets);
@@ -47,7 +47,7 @@ fn server_error(
assets: &HashMap<String, String>,
templates: &Tera,
msg: &str,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
with_headers(
Response::new(
error::template(assets, templates, "Erreur serveur", msg).into(),
@@ -56,36 +56,34 @@ fn server_error(
)
}
-pub fn text(str: String) -> Response<Body> {
+pub fn text(str: String) -> Response<Full<Bytes>> {
let mut response = Response::new(str.into());
*response.status_mut() = StatusCode::OK;
response
}
-pub fn ok() -> Response<Body> {
+pub fn ok() -> Response<Full<Bytes>> {
let mut response = Response::default();
*response.status_mut() = StatusCode::OK;
response
}
-pub fn redirect(uri: &str) -> Response<Body> {
+pub fn redirect(uri: &str) -> Response<Full<Bytes>> {
let mut response = Response::default();
*response.status_mut() = StatusCode::MOVED_PERMANENTLY;
with_headers(response, vec![(LOCATION, uri), (CACHE_CONTROL, "no-cache")])
}
-pub fn not_found() -> Response<Body> {
+pub fn not_found() -> Response<Full<Bytes>> {
let mut response = Response::default();
*response.status_mut() = StatusCode::NOT_FOUND;
response
}
-pub async fn file(filename: &str, content_type: &str) -> Response<Body> {
- if let Ok(file) = File::open(filename).await {
- let stream = FramedRead::new(file, BytesCodec::new());
- let body = Body::wrap_stream(stream);
+pub async fn file(filename: &str, content_type: &str) -> Response<Full<Bytes>> {
+ if let Ok(contents) = tokio::fs::read(filename).await {
with_headers(
- Response::new(body),
+ Response::new(Full::new(contents.into())),
vec![
(CACHE_CONTROL, "max-age=3153600000"),
(CONTENT_TYPE, content_type),