Add endpoints for working hours

It has an endpoint to show and to delete working hours
This commit is contained in:
Simon Goller 2024-06-27 14:55:05 +02:00
parent d1f6db2104
commit c9e526492c
6 changed files with 201 additions and 14 deletions

View file

@ -1,14 +1,63 @@
use std::rc::Rc;
use axum::{
body::Body, extract::State, response::Response, routing::post, Extension, Json, Router,
body::Body,
extract::{Path, Query, State},
response::Response,
routing::{get, post},
Extension, Json, Router,
};
use rest_types::ExtraHoursTO;
use serde::Deserialize;
use service::extra_hours::ExtraHoursService;
use uuid::Uuid;
use crate::{error_handler, Context, RestStateDef};
pub fn generate_route<RestState: RestStateDef>() -> Router<RestState> {
Router::new().route("/", post(create_extra_hours::<RestState>))
Router::new()
.route("/", post(create_extra_hours::<RestState>))
.route("/:id", post(delete_extra_hours::<RestState>))
.route(
"/by-sales-person/:id",
get(get_extra_hours_for_sales_person::<RestState>),
)
}
#[derive(Clone, Debug, Deserialize)]
pub struct ExtraHoursForSalesPersonAttributes {
year: u32,
until_week: u8,
}
pub async fn get_extra_hours_for_sales_person<RestState: RestStateDef>(
rest_state: State<RestState>,
Extension(context): Extension<Context>,
query: Query<ExtraHoursForSalesPersonAttributes>,
Path(sales_person_id): Path<Uuid>,
) -> Response {
error_handler(
(async {
let extra_hours: Rc<[ExtraHoursTO]> = rest_state
.extra_hours_service()
.find_by_sales_person_id_and_year(
sales_person_id,
query.year,
query.until_week,
context.into(),
)
.await?
.iter()
.map(ExtraHoursTO::from)
.collect();
Ok(Response::builder()
.status(201)
.body(Body::new(serde_json::to_string(&extra_hours).unwrap()))
.unwrap())
})
.await,
)
}
pub async fn create_extra_hours<RestState: RestStateDef>(
@ -25,10 +74,27 @@ pub async fn create_extra_hours<RestState: RestStateDef>(
.await?,
);
Ok(Response::builder()
.status(200)
.status(201)
.body(Body::new(serde_json::to_string(&extra_hours).unwrap()))
.unwrap())
})
.await,
)
}
pub async fn delete_extra_hours<RestState: RestStateDef>(
rest_state: State<RestState>,
Extension(context): Extension<Context>,
Path(extra_hours_id): Path<Uuid>,
) -> Response {
error_handler(
(async {
rest_state
.extra_hours_service()
.delete(extra_hours_id, context.into())
.await?;
Ok(Response::builder().status(204).body(Body::empty()).unwrap())
})
.await,
)
}