From f14d27b93d434b4db6db35cda811895baff2c869 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Wo=C5=BAniak?= Date: Fri, 13 Oct 2023 19:33:47 +0200 Subject: [PATCH] Fix rent offers --- assets/style.css | 32 ++++++- crates/oswilno-parking-space/src/lib.rs | 85 ++++++++++++++++++- .../templates/parking-spaces/all-partial.html | 52 ++++++------ .../parking-spaces/parking_space_actions.html | 2 +- 4 files changed, 139 insertions(+), 32 deletions(-) diff --git a/assets/style.css b/assets/style.css index 0d41424..161a67c 100644 --- a/assets/style.css +++ b/assets/style.css @@ -783,6 +783,10 @@ select { z-index: 50; } +.m-0 { + margin: 0px; +} + .m-1 { margin: 0.25rem; } @@ -1169,10 +1173,6 @@ select { text-align: center; } -.text-right { - text-align: right; -} - .text-2xl { font-size: 1.5rem; line-height: 2rem; @@ -1621,6 +1621,14 @@ select { } @media (min-width: 1280px) { + .xl\:mt-6 { + margin-top: 1.5rem; + } + + .xl\:block { + display: block; + } + .xl\:flex { display: flex; } @@ -1629,9 +1637,25 @@ select { min-height: calc(100vh - 80px - 192px); } + .xl\:w-\[30\%\] { + width: 30%; + } + .xl\:w-\[48\%\] { width: 48%; } + + .xl\:justify-between { + justify-content: space-between; + } + + .xl\:p-0 { + padding: 0px; + } + + .xl\:text-right { + text-align: right; + } } .\[\&\.active\]\:text-black\/90.active { diff --git a/crates/oswilno-parking-space/src/lib.rs b/crates/oswilno-parking-space/src/lib.rs index 3282577..c804fc8 100644 --- a/crates/oswilno-parking-space/src/lib.rs +++ b/crates/oswilno-parking-space/src/lib.rs @@ -5,15 +5,16 @@ use std::sync::Arc; use actix_web::http::header::ContentType; use actix_web::web::{scope, Data, Form, Path, ServiceConfig}; -use actix_web::{get, post, put, HttpRequest, HttpResponse}; +use actix_web::{delete, get, post, put, HttpRequest, HttpResponse}; use askama_actix::Template; use autometrics::autometrics; use oswilno_contract::parking_space_locations::Model as ParkingSpaceLocation; use oswilno_contract::parking_space_rents::Model as ParkingSpaceRent; use oswilno_contract::parking_spaces::Model as ParkingSpace; +use oswilno_contract::prelude::*; use oswilno_contract::sea_orm_active_enums::ParkingSpaceState; use oswilno_contract::{ - accounts, chrono, parking_space_locations, parking_space_rents, parking_spaces, + accounts, chrono, parking_space_locations, parking_space_rents, parking_spaces, rent_requests, }; use oswilno_session::{Authenticated, MaybeAuthenticated}; use oswilno_view::{ @@ -40,7 +41,8 @@ pub fn mount(config: &mut ServiceConfig) { .service(all_parking_spaces) .service(create) .service(edit_show) - .service(update), + .service(update) + .service(delete_parking_space), ); } @@ -132,6 +134,34 @@ async fn search_parking_spaces() -> HttpResponse { HttpResponse::Ok().body("") } +#[delete("/delete/{parking_space_id}")] +async fn delete_parking_space( + _req: HttpRequest, + db: Data, + _session: Authenticated, + _hcx: HelperContext, + parking_space_id: Path, +) -> HttpResponse { + let parking_space_id = parking_space_id.into_inner(); + let db = db.into_inner(); + let Ok(res) = ParkingSpaces::delete_by_id(parking_space_id) + .exec(&*db) + .await + .inspect_err(|e| tracing::error!("Failed to delete parking space {parking_space_id}: {e}")) + else { + return HttpResponse::NotFound() + .body(format!("Parking space {parking_space_id} does not exists")); + }; + if res.rows_affected == 1 { + HttpResponse::SeeOther() + .append_header(("HX-Redirect", "/parking-spaces/all")) + .append_header(("Location", "/parking-spaces/all")) + .body("") + } else { + HttpResponse::NotFound().body(format!("Parking space {parking_space_id} does not exists")) + } +} + async fn load_parking_spaces( db: Arc, account_id: Option, @@ -792,3 +822,52 @@ fn render_rent_form( } .unwrap() } + +#[post("/{parking_space_id}/parking-space-rents/{parking_space_rent_id}/request")] +pub async fn create_rent_request( + session: Authenticated, + db: Data, + parking_space_id: Path, + parking_space_rent_id: Path, + hcx: HelperContext, +) -> HttpResponse { + let db = db.into_inner(); + let parking_space_id = parking_space_id.into_inner(); + let parking_space_rent_id = parking_space_rent_id.into_inner(); + { + use rent_requests::Column::*; + + let res = RentRequests::find() + .filter(ParkingSpaceId.eq(Some(parking_space_id))) + .filter(ParkingSpaceRentId.eq(Some(parking_space_rent_id))) + .filter(AccountId.eq(Some(session.account_id()))).one(&*db).await + .inspect_err(|e| tracing::error!("Failed to check rent request existence for space {parking_space_id} and rent {parking_space_rent_id}: {e}")); + match res { + Ok(Some(_)) => { + return HttpResponse::Conflict() + .body(filters::t("Already requested", &hcx).unwrap_or_default()); + } + Err(_) => { + return HttpResponse::InternalServerError() + .body(filters::t("Internat server error", &hcx).unwrap_or_default()); + } + _ => {} + }; + }; + let Ok(_rent_request) = rent_requests::ActiveModel { + parking_space_id: Set(Some(parking_space_id)), + parking_space_rent_id: Set(Some(parking_space_rent_id)), + account_id: Set(Some(session.account_id())), + ..Default::default() + }.save(&*db).await + .inspect_err(|e| tracing::error!("Failed to create rent request for space {parking_space_id} and rent {parking_space_rent_id}: {e}")) else { + return HttpResponse::InternalServerError() + .body(filters::t("Internat server error", &hcx).unwrap_or_default()); + }; + HttpResponse::Ok() + .append_header(( + "HX-Retarget", + format!("#submit-request-rent-{parking_space_rent_id}").as_str(), + )) + .finish() +} diff --git a/crates/oswilno-parking-space/templates/parking-spaces/all-partial.html b/crates/oswilno-parking-space/templates/parking-spaces/all-partial.html index 60dfab6..63b04b0 100644 --- a/crates/oswilno-parking-space/templates/parking-spaces/all-partial.html +++ b/crates/oswilno-parking-space/templates/parking-spaces/all-partial.html @@ -1,15 +1,16 @@
-
-

+
+

{{"Parking spaces"}}

-
{% endif %} - + {% for parking_space_rent in parking_space_rents -%} {% if let Some(parking_space) = parking_space_by_id.get(parking_space_rent.parking_space_id) %} {% if parking_space.state == ParkingSpaceState::Verified || Some(parking_space.account_id.clone()) == account_id %} @@ -72,54 +73,57 @@ - +
{% if let Some(spot) = parking_space.spot %} {{ spot }} {% else %}   {% endif %} - +
- {{ location.name }} - {{ location.number }} - -  {{"stage"|t(hcx)}}  - - - {{ location.stage }} - +
{{ location.name }}
+
 
+
{{ location.number }}
+
 
+
+ {{"stage"|t(hcx)}} +
+
 
+
{{ location.stage }}
-
{{ account.login }}
+ {{ account.login }}
-