todos more traits

This commit is contained in:
2023-09-16 10:28:50 +02:00
parent 2221ee0412
commit 1ffb2c5e74
6 changed files with 114 additions and 64 deletions

View File

@@ -152,10 +152,17 @@ pub fn router(state: AppState) -> Router {
.route("/:id/todo/:id/edit/save", post(trip_todo_edit_save))
.route("/:id/todo/:id/edit/cancel", post(trip_todo_edit_cancel))
.route(
"/:id/todo/new",
&<crate::models::trips::todos::Todo as route::Create>::with_prefix(
"/:id/todo",
),
post(<crate::models::trips::todos::Todo as route::Create>::create),
)
.route("/:id/todo/:id/delete", post(trip_todo_delete)),
.route(
&<crate::models::trips::todos::Todo as route::Delete>::with_prefix(
"/:id/todo",
),
post(<crate::models::trips::todos::Todo as route::Delete>::delete),
),
)
.nest(
(&TopLevelPage::Inventory.path()).into(),

View File

@@ -1479,42 +1479,3 @@ pub async fn trip_todo_edit_cancel(
.into_response()),
}
}
#[tracing::instrument]
pub async fn trip_todo_delete(
Extension(current_user): Extension<models::user::User>,
State(state): State<AppState>,
headers: HeaderMap,
Path((trip_id, todo_id)): Path<(Uuid, Uuid)>,
) -> Result<impl IntoResponse, Error> {
let ctx = Context::build(current_user);
let deleted = models::trips::todos::Todo::delete(
&ctx,
&state.database_pool,
&TodoFilter { trip_id },
todo_id,
)
.await?;
if !deleted {
return Err(Error::Request(RequestError::NotFound {
message: format!("todo with id {todo_id} not found"),
}));
}
let trip = models::trips::Trip::find(&ctx, &state.database_pool, trip_id).await?;
match trip {
None => Err(Error::Request(RequestError::NotFound {
message: format!("trip with id {trip_id} not found"),
})),
Some(mut trip) => {
trip.load_todos(&ctx, &state.database_pool).await?;
Ok(models::trips::todos::TodoList {
trip: &trip,
todos: &trip.todos(),
}
.build(None)
.into_response())
}
}
}