69 lines
2.0 KiB
Rust
69 lines
2.0 KiB
Rust
use actix_web::{web, HttpResponse, Responder};
|
|
use rinja::Template;
|
|
use serde::Deserialize;
|
|
use sqlx::PgPool;
|
|
|
|
use crate::{
|
|
endpoints::{availability::NewOrEditAvailabilityTemplate, IdPath},
|
|
models::{find_free_time_slots, Availability, AvailabilityTime, User},
|
|
utils::ApplicationError,
|
|
END_OF_DAY, START_OF_DAY,
|
|
};
|
|
|
|
#[derive(Deserialize)]
|
|
struct EditAvailabilityQuery {
|
|
#[serde(rename(deserialize = "wholeday"))]
|
|
whole_day: Option<bool>,
|
|
}
|
|
|
|
#[actix_web::get("/availabillity/edit/{id}")]
|
|
pub async fn get(
|
|
user: web::ReqData<User>,
|
|
pool: web::Data<PgPool>,
|
|
path: web::Path<IdPath>,
|
|
query: web::Query<EditAvailabilityQuery>,
|
|
) -> Result<impl Responder, ApplicationError> {
|
|
let Some(availability) = Availability::read_by_id(pool.get_ref(), path.id).await? else {
|
|
return Ok(HttpResponse::NotFound().finish());
|
|
};
|
|
|
|
if availability.user_id != user.id {
|
|
return Err(ApplicationError::Unauthorized);
|
|
}
|
|
|
|
let suggestions = if let AvailabilityTime::Temporarily(start, end) = availability.time {
|
|
let availabilities =
|
|
Availability::read_by_user_and_date(pool.get_ref(), user.id, &availability.date)
|
|
.await?;
|
|
|
|
find_free_time_slots(&availabilities)
|
|
.into_iter()
|
|
.filter(|(a, b)| *b == start || *a == end)
|
|
.collect()
|
|
} else {
|
|
Vec::new()
|
|
};
|
|
|
|
let time_selection = if let Some(whole_day) = query.whole_day {
|
|
if whole_day {
|
|
AvailabilityTime::WholeDay
|
|
} else {
|
|
AvailabilityTime::Temporarily(START_OF_DAY, END_OF_DAY)
|
|
}
|
|
} else {
|
|
availability.time.clone()
|
|
};
|
|
|
|
println!("{:?}", availability.time);
|
|
let template = NewOrEditAvailabilityTemplate {
|
|
user: user.into_inner(),
|
|
date: availability.date,
|
|
id: Some(path.id),
|
|
time_selection,
|
|
comment: availability.comment.as_deref(),
|
|
slot_suggestions: suggestions,
|
|
};
|
|
|
|
Ok(HttpResponse::Ok().body(template.render()?))
|
|
}
|