Fix branch

This commit is contained in:
Adrian Woźniak 2023-10-11 11:21:33 +02:00
parent 1f18ba54d3
commit d4135d425f
3 changed files with 59 additions and 27 deletions

View File

@ -28,6 +28,9 @@ pub fn mount(config: &mut ServiceConfig) {
config.service(root).service(
scope("/parking-spaces")
.service(parking_space_rent_form)
.service(parking_space_rent_edit)
.service(parking_space_rent_create)
.service(parking_space_rent_update)
.service(form_show)
.service(all_parking_spaces)
.service(create)
@ -459,6 +462,7 @@ async fn update(
struct ParkingSpaceRentForm {
pub id: Option<i32>,
pub price: Option<i32>,
pub price_f: Option<f64>,
}
#[derive(Debug, Template)]
@ -557,11 +561,39 @@ async fn parking_space_rent_form(
async fn parking_space_rent_edit(_id: Path<i32>) -> HttpResponse {
todo!()
}
#[post("/parking-space-rents/create")]
async fn parking_space_rent_create(_form: Form<ParkingSpaceRentForm>) -> HttpResponse {
#[post("/{parking_space_id}/parking-space-rents/create")]
async fn parking_space_rent_create(form: Form<ParkingSpaceRentForm>) -> HttpResponse {
let mut form = form.into_inner();
form.price = form.price_f.map(|n| n * 100.0).map(|n| n as i32);
todo!()
}
#[put("/parking-space-rents/update")]
async fn parking_space_rent_update(_form: Form<ParkingSpaceRentForm>) -> HttpResponse {
#[put("/{parking_space_id}/parking-space-rents/update")]
async fn parking_space_rent_update(form: Form<ParkingSpaceRentForm>) -> HttpResponse {
let mut form = form.into_inner();
form.price = form.price_f.map(|n| n * 100.0).map(|n| n as i32);
todo!()
}
fn render_rent_form(form: ParkingSpaceRentForm, req: &HttpRequest) -> String {
let body = ParkingSpaceRentFormTemplate {
parking_space,
location,
form,
lang,
t: t.into_inner(),
};
let main = Main {
body,
title: Blank,
opts: MainOpts {
show: true,
search: None,
session,
},
};
if is_partial(&req) {
main.render()
} else {
Layout { main }.render()
}
}

View File

@ -6,9 +6,9 @@
<div class="max-w-md w-full p-6 bg-white rounded-lg shadow-lg">
<form
{% if let Some(id) = form.id %}
hx-put='/parking-space-rents/update/{{id}}'
hx-put='/parking-spaces/{{parking_space.id}}/parking-space-rents/update/{{id}}'
{% else %}
hx-post='/parking-space-rents/create'
hx-post='/parking-spaces/{{parking_space.id}}/parking-space-rents/create'
{% endif %}
hx-headers="{'Accept':'text/html-partial'}"
>
@ -27,21 +27,15 @@
</label>
<div class="flex w-full">
<input
id="price-major"
name="price-major"
id="price"
name="price_f"
class="px-4 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-cyan-500"
type="number"
step="0.01"
min="0.00"
max="1000000.00"
required
{% if let Some(v) = form.price %} value="{{ v / 100 }}" {% endif %}
/>
<span class="m-1 text-lg font-medium">.</span>
<input
id="price-minor
name="price-minor"
class="px-4 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-cyan-500"
type="number"
required
{% if let Some(v) = form.price %} value="{{ v % 100 }}" {% endif %}
{% if let Some(v) = form.price %} value="{{ v|display_price }}" {% endif %}
/>
<span class="m-1">PLN</span>
</div>

View File

@ -14,3 +14,9 @@ pub fn skip_if<T: std::fmt::Display + PartialEq + Copy>(n: &T, skip: T) -> Resul
n.to_string()
})
}
#[tracing::instrument]
pub fn display_price(n: &&i32) -> Result<String> {
let n: i32 = **n;
Ok(format!("{}.{}", n / 100, n % 100))
}