Irvin Blais rock!

This commit is contained in:
didier amyot committed 2023-04-01 14:27:38 -04:00
1 parent f2c6c8d65c
commit 0d0337a6aa
17 files changed
+175 -39

No files matched your search

+7 -9
View File
@@ -28,7 +28,7 @@ pub async fn login(
return Ok(tmpl_env.render("partials/index/failure.html", context!{})?.customize());
}
let user = maybeUser.unwrap();
let authentication_service = service_factory.get_authentication_service()?;
let authentication_service = service_factory.get_authentication_service(&conn)?;
let cookie = authentication_service.create_login_cookie(&user.id);
let ctx = context! {
user => & user,
@@ -44,7 +44,8 @@ pub async fn logout(
service_factory: web::Data<ServicesFactory>,
tmpl_env: MiniJinjaRenderer,
) -> actix_web::Result<impl Responder> {
let authentication_service = service_factory.get_authentication_service()?;
let conn = service_factory.get_connection()?;
let authentication_service = service_factory.get_authentication_service(&conn)?;
let res = tmpl_env.render("partials/index/form.html", context! {})?;
Ok(res
.customize()
@@ -58,15 +59,12 @@ pub async fn index(
tmpl_env: MiniJinjaRenderer,
) -> actix_web::Result<impl Responder> {
let conn = service_factory.get_connection()?;
let authentication_service = service_factory.get_authentication_service()?;
let user_service = service_factory.get_user_service(&conn)?;
let authentication_service = service_factory.get_authentication_service(&conn)?;
let is_logged_in = authentication_service.get_login(&req).is_some();
let userid = authentication_service.get_login(&req);
let username = userid.clone().unwrap_or("".to_string());
let ctx = context! {
is_logged_in => userid.is_some(),
// user => user_service.get_user_for_authentication(userid.unwrap(),
is_logged_in => is_logged_in,
user => authentication_service.get_logged_in_user(&req).unwrap(),
};
tmpl_env.render("index.html", ctx)
}
+1
View File
@@ -67,6 +67,7 @@ fn create_views_scope() -> Scope {
.service(index_views::logout)
.service(views::actions)
.service(views::accounts)
.service(views::securities)
.service(views::overview)
.service(views::overview_submit_button)
.service(views::actions_partials)
+6 -3
View File
@@ -1,6 +1,6 @@
use crate::services::{SecurityService, SessionService, UserService, AccountService};
use actix_web::{error, Result};
use actix_web::{error, Error, Result};
use r2d2::PooledConnection;
use r2d2_sqlite::SqliteConnectionManager;
@@ -39,8 +39,11 @@ impl ServicesFactory {
.map_err(|_e| error::ErrorBadRequest("Cannot get DB connection."))
}
pub fn get_authentication_service(&self) -> Result<SessionService> {
Ok(SessionService::from_key(& self.cache.session_service_key))
pub fn get_authentication_service<'a>(
&'a self,
_conn: &'a PooledConnection<SqliteConnectionManager>,) -> Result<SessionService> {
let user_service = self.get_user_service(_conn)?;
Ok(SessionService::create(& self.cache.session_service_key, user_service))
}
pub fn get_user_service<'a>(
+19 -3
View File
@@ -24,14 +24,27 @@ pub async fn accounts(tmpl_env: MiniJinjaRenderer,
req: HttpRequest,
service_factory: web::Data<ServicesFactory>) -> actix_web::Result<impl Responder> {
let conn = service_factory.get_connection()?;
let auth_service = service_factory.get_authentication_service()?;
let auth_service = service_factory.get_authentication_service(&conn)?;
let user = auth_service.get_login(&req);
let account_service = service_factory.get_account_service(& conn)?;
let accounts = account_service.get_user_accounts(user.unwrap())
.map_err(actix_web::error::ErrorInternalServerError)?;
let accounts = account_service.get_user_accounts(&user.unwrap())
.map_err(error::ErrorInternalServerError)?;
tmpl_env.render("accounts.html", context!{accounts => accounts})
}
#[actix_web::get("/securities")]
pub async fn securities(tmpl_env: MiniJinjaRenderer,
service_factory: web::Data<ServicesFactory>) -> actix_web::Result<impl Responder> {
let conn = service_factory.get_connection()?;
let account_service = service_factory.get_security_service(& conn)?;
let ctx = context!{
securities => account_service.list_securities()
};
tmpl_env.render("securities.html", ctx)
}
#[actix_web::get("/overview")]
pub async fn overview(
tmpl_env: MiniJinjaRenderer,
@@ -60,6 +73,7 @@ pub async fn overview_submit_button(
#[actix_web::get("/partials/actions/action-form")]
pub async fn actions_partials(
req: HttpRequest,
tmpl_env: MiniJinjaRenderer,
query: web::Query<HashMap<String, String>>,
service_factory: web::Data<ServicesFactory>,
@@ -70,9 +84,11 @@ pub async fn actions_partials(
.to_string();
let conn = service_factory.get_connection()?;
let security_service = service_factory.get_security_service(&conn)?;
let authentication_service = service_factory.get_authentication_service(&conn)?;
let ctx = context! {
event_type => event_type,
securities => security_service.list_securities(),
user => authentication_service.get_logged_in_user(&req).map_err(actix_web::error::ErrorInternalServerError)?,
};
let template_location = format!("partials/actions/{:}.html", event_type);
info!("Getting template at: {:?}", template_location);