62 lines
2.3 KiB
Rust
62 lines
2.3 KiB
Rust
use rocket::State;
|
|
use crate::helper::settings::Settings;
|
|
use crate::helper::session_cookies::model::SessionCookie;
|
|
use rocket::serde::json::Json;
|
|
use crate::database::model::appointments::Appointment;
|
|
use crate::modules::api::model::api_outcome::{ApiErrorWrapper, ApiError};
|
|
use crate::modules::api::member_management::controller::parser::{parse_member_cookie, parse_uuid_string};
|
|
use chrono::{NaiveDate};
|
|
use crate::database::controller::members::check_access_to_resource;
|
|
|
|
use crate::database::controller::appointments::add_appointment;
|
|
use crate::helper::translate_diesel_error::translate_diesel;
|
|
|
|
#[derive(Queryable, Clone, Deserialize, Serialize)]
|
|
pub struct CreateAppointmentData{
|
|
type_id: String,
|
|
entity_id: String,
|
|
appointment_date: String,
|
|
}
|
|
|
|
/// Creates new Appointment
|
|
///
|
|
/// # Api Call
|
|
/// * POST
|
|
/// * /api/appointments
|
|
///
|
|
/// # Api Result
|
|
/// * Api returns Appointment or ApiError in ApiErrorWrapper
|
|
///
|
|
/// # Permission required
|
|
/// * modules.scheduler.appointments.edit on specified entity
|
|
#[post("/api/appointments", format = "json", data = "<create_appointment_data>")]
|
|
pub fn create_appointment(
|
|
settings: &State<Settings>,
|
|
cookie: SessionCookie,
|
|
create_appointment_data: Json<CreateAppointmentData>,
|
|
) -> Result<Json<Appointment>, Json<ApiErrorWrapper>> {
|
|
let caller = parse_member_cookie(cookie.member)?;
|
|
let cad = create_appointment_data.into_inner();
|
|
|
|
let apentity_id : uuid::Uuid = parse_uuid_string(cad.entity_id)?;
|
|
|
|
if !check_access_to_resource(settings, caller.entity_id, apentity_id, crate::permissions::modules::scheduler::appointments::EDIT){
|
|
return Err(Json(
|
|
ApiError::new(403, "Keine Berechtigung, Termine anzulegen!".to_string()).to_wrapper(),
|
|
));
|
|
}
|
|
|
|
let apdate: NaiveDate = match NaiveDate::parse_from_str(&cad.appointment_date, "%Y-%m-%d") {
|
|
Ok(apdate) => apdate,
|
|
Err(e) => {
|
|
error!("Couldn't parse appointment date: {}", e);
|
|
return Err(Json(ApiError::new(400, "Das eingegebene Datum konnte nicht verarbeitet werden.".to_string()).to_wrapper()))
|
|
}
|
|
};
|
|
let aptype_id : uuid::Uuid = parse_uuid_string(cad.type_id)?;
|
|
|
|
match add_appointment(settings, aptype_id, apentity_id, apdate){
|
|
Ok(appointment) => Ok(Json(appointment)),
|
|
Err(e) => Err(translate_diesel(e))
|
|
}
|
|
} |