add todos

This commit is contained in:
2023-09-13 00:44:59 +02:00
parent 4c850f6c0b
commit 6a6c62d736
11 changed files with 495 additions and 5 deletions

View File

@@ -139,6 +139,14 @@ pub fn router(state: AppState) -> Router {
.route(
"/:id/items/:id/unready",
get(trip_item_set_unready).post(trip_item_set_unready_htmx),
)
.route(
"/:id/todo/:id/done",
get(trip_todo_done).post(trip_todo_done_htmx),
)
.route(
"/:id/todo/:id/undone",
get(trip_todo_undone).post(trip_todo_undone_htmx),
),
)
.nest(

View File

@@ -437,6 +437,8 @@ pub async fn trip(
trip.load_trips_types(&ctx, &state.database_pool).await?;
trip.load_todos(&ctx, &state.database_pool).await?;
trip.sync_trip_items_with_inventory(&ctx, &state.database_pool)
.await?;
@@ -1238,3 +1240,97 @@ pub async fn trip_item_packagelist_set_unready_htmx(
trip_id, &item,
))
}
#[tracing::instrument]
pub async fn trip_todo_done_htmx(
Extension(current_user): Extension<models::user::User>,
State(state): State<AppState>,
Path((trip_id, todo_id)): Path<(Uuid, Uuid)>,
) -> Result<impl IntoResponse, Error> {
let ctx = Context::build(current_user);
models::trips::todos::Todo::set_state(
&ctx,
&state.database_pool,
trip_id,
todo_id,
models::trips::todos::State::Done,
)
.await?;
let todo_item = models::trips::todos::Todo::find(&ctx, &state.database_pool, trip_id, todo_id)
.await?
.ok_or_else(|| {
Error::Request(RequestError::NotFound {
message: format!("todo with id {todo_id} not found"),
})
})?;
Ok(view::trip::TripTodo::build(&trip_id, &todo_item))
}
#[tracing::instrument]
pub async fn trip_todo_done(
Extension(current_user): Extension<models::user::User>,
State(state): State<AppState>,
Path((trip_id, todo_id)): Path<(Uuid, Uuid)>,
headers: HeaderMap,
) -> Result<impl IntoResponse, Error> {
let ctx = Context::build(current_user);
models::trips::todos::Todo::set_state(
&ctx,
&state.database_pool,
trip_id,
todo_id,
models::trips::todos::State::Done,
)
.await?;
Ok(Redirect::to(get_referer(&headers)?))
}
#[tracing::instrument]
pub async fn trip_todo_undone_htmx(
Extension(current_user): Extension<models::user::User>,
State(state): State<AppState>,
Path((trip_id, todo_id)): Path<(Uuid, Uuid)>,
) -> Result<impl IntoResponse, Error> {
let ctx = Context::build(current_user);
models::trips::todos::Todo::set_state(
&ctx,
&state.database_pool,
trip_id,
todo_id,
models::trips::todos::State::Todo,
)
.await?;
let todo_item = models::trips::todos::Todo::find(&ctx, &state.database_pool, trip_id, todo_id)
.await?
.ok_or_else(|| {
Error::Request(RequestError::NotFound {
message: format!("todo with id {todo_id} not found"),
})
})?;
Ok(view::trip::TripTodo::build(&trip_id, &todo_item))
}
#[tracing::instrument]
pub async fn trip_todo_undone(
Extension(current_user): Extension<models::user::User>,
State(state): State<AppState>,
Path((trip_id, todo_id)): Path<(Uuid, Uuid)>,
headers: HeaderMap,
) -> Result<impl IntoResponse, Error> {
let ctx = Context::build(current_user);
models::trips::todos::Todo::set_state(
&ctx,
&state.database_pool,
trip_id,
todo_id,
models::trips::todos::State::Todo,
)
.await?;
Ok(Redirect::to(get_referer(&headers)?))
}