From 4799f929616afa115eb65fa47dbed52e720bc147 Mon Sep 17 00:00:00 2001 From: eraden Date: Wed, 11 Oct 2023 11:19:54 +0200 Subject: [PATCH] Insert defaul with 1 query --- argonfand | 1 + crates/oswilno-parking-space/src/lib.rs | 25 ++++---- .../parking-spaces/parking_space_state.html | 6 +- crates/oswilno-session/src/lib.rs | 31 ++++------ crates/oswilno-view/src/filters.rs | 13 ++--- crates/oswilno-view/src/helper_context.rs | 57 +++++++++++++++++++ crates/oswilno-view/src/lang.rs | 2 +- crates/oswilno-view/src/lib.rs | 3 + 8 files changed, 93 insertions(+), 45 deletions(-) create mode 160000 argonfand create mode 100644 crates/oswilno-view/src/helper_context.rs diff --git a/argonfand b/argonfand new file mode 160000 index 0000000..ece0840 --- /dev/null +++ b/argonfand @@ -0,0 +1 @@ +Subproject commit ece0840d56893410780b77934e85fba53335e071 diff --git a/crates/oswilno-parking-space/src/lib.rs b/crates/oswilno-parking-space/src/lib.rs index 4c754c3..e9d6dfe 100644 --- a/crates/oswilno-parking-space/src/lib.rs +++ b/crates/oswilno-parking-space/src/lib.rs @@ -9,8 +9,8 @@ use oswilno_contract::sea_orm_active_enums::ParkingSpaceState; use oswilno_contract::{accounts, parking_space_locations}; use oswilno_session::{Authenticated, MaybeAuthenticated}; use oswilno_view::{ - filters, is_partial, Blank, Errors, Lang, Layout, Main, MainOpts, SearchOpts, SessionOpts, - TranslationStorage, + filters, is_partial, Blank, Errors, HelperContext, Lang, Layout, Main, MainOpts, SearchOpts, + SessionOpts, TranslationStorage, }; use sea_orm::prelude::*; use sea_orm::ActiveValue::{NotSet, Set}; @@ -67,6 +67,7 @@ struct AllPartialParkingSpace { location_by_id: BTreeMap>, session: Option, account_id: Option, + hcx: HelperContext, } #[autometrics] @@ -75,6 +76,7 @@ async fn all_parking_spaces( req: HttpRequest, db: Data, session: MaybeAuthenticated, + hcx: HelperContext, ) -> HttpResponse { let db = db.into_inner(); let session = session.into_option(); @@ -83,7 +85,7 @@ async fn all_parking_spaces( tracing::debug!("session {session:?}"); - let mut parking_spaces = load_parking_spaces(db, account_id).await; + let mut parking_spaces = load_parking_spaces(db, account_id, htx).await; parking_spaces.session = session.clone(); let main = Main { body: parking_spaces, @@ -117,6 +119,7 @@ async fn search_parking_spaces() -> HttpResponse { async fn load_parking_spaces( db: Arc, account_id: Option, + hcx: HelperContext, ) -> AllPartialParkingSpace { let rents = parking_space_rents::Entity::find().all(&*db).await.unwrap(); let ids = rents @@ -180,6 +183,7 @@ async fn load_parking_spaces( locations, location_by_id, session: None, + hcx, } } @@ -199,8 +203,7 @@ async fn load_locations(db: Arc) -> Vec, - lang: Lang, + hcx: HelperContext, db: Data, ) -> HttpResponse { let db = db.into_inner(); @@ -208,9 +211,8 @@ async fn form_show( let body = ParkingSpaceFormPartial { form: Default::default(), locations: load_locations(db.clone()).await, - lang, errors: Default::default(), - t: t.into_inner(), + hcx, }; let main = Main { body, @@ -235,10 +237,9 @@ async fn form_show( #[template(path = "../templates/parking-spaces/form-partial.html")] struct ParkingSpaceFormPartial { form: CreateParkingSpace, - lang: Lang, errors: Errors, - t: Arc, locations: Vec, + hcx: HelperContext, } #[derive(Debug, Default, serde::Deserialize)] @@ -254,8 +255,7 @@ async fn create( db: Data, p: Form, session: Authenticated, - t: Data, - lang: Lang, + hcx: HelperContext, ) -> HttpResponse { use oswilno_contract::parking_spaces::*; let CreateParkingSpace { @@ -280,8 +280,7 @@ async fn create( spot, id: None, }, - t: t.into_inner(), - lang, + hcx, errors: Default::default(), locations: load_locations(db.clone()).await, } diff --git a/crates/oswilno-parking-space/templates/parking-spaces/parking_space_state.html b/crates/oswilno-parking-space/templates/parking-spaces/parking_space_state.html index 8880327..8e3c13f 100644 --- a/crates/oswilno-parking-space/templates/parking-spaces/parking_space_state.html +++ b/crates/oswilno-parking-space/templates/parking-spaces/parking_space_state.html @@ -1,14 +1,14 @@ {% match parking_space.state %} {% when ParkingSpaceState::Pending %}
- Pending + {{"Pending"|t(lang,t)}}
{% when ParkingSpaceState::Verified %}
- Accepted + {{"Accepted"|t(lang,t)}}
{% when ParkingSpaceState::Banned %}
- Rejected + {{"Rejected"|t(lang,t)}}
{% endmatch %} diff --git a/crates/oswilno-session/src/lib.rs b/crates/oswilno-session/src/lib.rs index 68ecf33..73956d7 100644 --- a/crates/oswilno-session/src/lib.rs +++ b/crates/oswilno-session/src/lib.rs @@ -7,7 +7,9 @@ use actix_web::{get, post, HttpRequest, HttpResponse}; use askama_actix::Template; use autometrics::autometrics; use garde::Validate; -use oswilno_view::{Blank, Errors, Lang, Layout, Main, MainOpts, TranslationStorage}; +use oswilno_view::{ + Blank, Errors, HelperContext, Lang, Layout, Main, MainOpts, TranslationStorage, +}; use sea_orm::DatabaseConnection; use serde::{Deserialize, Serialize}; @@ -148,8 +150,7 @@ impl SessionConfigurator { #[template(path = "./sign-in/partial.html")] struct SignInPartialTemplate { form: SignInPayload, - lang: Lang, - t: Arc, + hcx: HelperContext, errors: Errors, } @@ -160,15 +161,14 @@ pub struct SignInPayload { } #[get("/login")] -async fn login_view(req: HttpRequest, t: Data) -> HttpResponse { +async fn login_view(req: HttpRequest, hcx: HelperContext) -> HttpResponse { HttpResponse::Ok().body( if oswilno_view::is_partial(&req) { Main { body: SignInPartialTemplate { form: SignInPayload::default(), - lang: Lang::Pl, - t: t.into_inner(), errors: Errors::default(), + hcx, }, opts: MainOpts { show: true, @@ -183,9 +183,8 @@ async fn login_view(req: HttpRequest, t: Data) -> HttpRespon main: Main { body: SignInPartialTemplate { form: SignInPayload::default(), - lang: Lang::Pl, - t: t.into_inner(), errors: Errors::default(), + hcx, }, opts: MainOpts { show: true, @@ -208,10 +207,8 @@ async fn login( db: Data, redis: Data, payload: Form, - t: Data, - lang: Lang, + hcx: HelperContext, ) -> Result { - let t = t.into_inner(); let mut errors = Errors::default(); let form = payload.into_inner(); match login_inner( @@ -226,14 +223,9 @@ async fn login( { Some(res) => Ok(res), None => Ok(HttpResponse::Ok().body( - (SignInPartialTemplate { - form, - lang, - t, - errors, - }) - .render() - .unwrap(), + (SignInPartialTemplate { form, errors, hcx }) + .render() + .unwrap(), )), } } @@ -352,6 +344,7 @@ async fn find_account( async fn refresh_token( refresh_token: actix_jwt_session::Authenticated, storage: Data, + _hcx: HelperContext, ) -> HttpResponse { let s = storage.into_inner(); let pair = match s.refresh::(refresh_token.access_jti()).await { diff --git a/crates/oswilno-view/src/filters.rs b/crates/oswilno-view/src/filters.rs index 31426b3..e5c479d 100644 --- a/crates/oswilno-view/src/filters.rs +++ b/crates/oswilno-view/src/filters.rs @@ -1,15 +1,10 @@ -use crate::{Lang, TranslationStorage}; +use crate::HelperContext; use askama::Result; -use std::sync::Arc; -#[tracing::instrument] -pub fn t( - s: T, - lang: &Lang, - t: &Arc, -) -> Result { +#[tracing::instrument(skip(htx))] +pub fn t(s: T, htx: &HelperContext) -> Result { // tracing::debug!("translating {s:?} to lang {lang:?} with {t:?}"); - Ok(t.to_lang(*lang, &s.to_string())) + Ok(htx.t.to_lang(htx.lang, &s.to_string())) } pub fn skip_if(n: &T, skip: T) -> Result { diff --git a/crates/oswilno-view/src/helper_context.rs b/crates/oswilno-view/src/helper_context.rs new file mode 100644 index 0000000..41750a6 --- /dev/null +++ b/crates/oswilno-view/src/helper_context.rs @@ -0,0 +1,57 @@ +use crate::Lang; +use crate::{ExtractLangFuture, TranslationStorage}; +use actix_web::error::Error; +use actix_web::web::Data; +use actix_web::HttpRequest; +use std::future::Future; +use std::pin::Pin; +use std::task::{Context, Poll}; + +#[derive(Clone, Debug)] +pub struct HelperContext { + pub t: Data, + pub lang: Lang, +} + +impl actix_web::FromRequest for HelperContext { + type Error = actix_web::error::Error; + type Future = ExtractFuture; + + fn from_request( + req: &actix_web::HttpRequest, + _payload: &mut actix_web::dev::Payload, + ) -> Self::Future { + ExtractFuture { + req: Some(req.clone()), + } + } +} + +pub struct ExtractFuture { + req: Option, +} + +impl Future for ExtractFuture { + type Output = Result; + + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let this = self.get_mut(); + let lang = Pin::new(&mut ExtractLangFuture { + req: this.req.clone(), + }) + .poll(cx); + let lang = match lang { + Poll::Ready(Ok(lang)) => lang, + _ => panic!("{lang:?}"), + }; + let t = this + .req + .as_ref() + .unwrap() + .app_data::>() + .unwrap() + .clone(); + + Poll::Ready(Ok(HelperContext { t, lang })) + } +} diff --git a/crates/oswilno-view/src/lang.rs b/crates/oswilno-view/src/lang.rs index e4b0f27..7d6be7f 100644 --- a/crates/oswilno-view/src/lang.rs +++ b/crates/oswilno-view/src/lang.rs @@ -24,7 +24,7 @@ impl FromStr for Lang { } pub struct ExtractLangFuture { - req: Option, + pub req: Option, } impl Future for ExtractLangFuture { diff --git a/crates/oswilno-view/src/lib.rs b/crates/oswilno-view/src/lib.rs index 26e111b..42f7ab8 100644 --- a/crates/oswilno-view/src/lib.rs +++ b/crates/oswilno-view/src/lib.rs @@ -4,9 +4,12 @@ use std::sync::{Arc, RwLock}; use actix_web::http::header::ContentType; use actix_web::web::ServiceConfig; use actix_web::{get, HttpRequest, HttpResponse}; + +pub use helper_context::*; pub use lang::*; pub mod filters; +pub mod helper_context; pub mod lang; pub fn mount(config: &mut ServiceConfig) {