From bfe05377501985cd6c757b05c2243227304ab2d4 Mon Sep 17 00:00:00 2001 From: "D. Scott Boggs" Date: Sun, 25 Jun 2023 15:17:59 -0400 Subject: [PATCH] Fix bug where clearing ticks on one day would clear all ticks for the track on all days --- client/src/components/TickComponent.vue | 2 +- client/src/state.ts | 20 +++++++++++++------- server/src/api/mod.rs | 1 + server/src/api/tracks.rs | 25 +++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/client/src/components/TickComponent.vue b/client/src/components/TickComponent.vue index 7fc1ae0..8b1d863 100644 --- a/client/src/components/TickComponent.vue +++ b/client/src/components/TickComponent.vue @@ -15,7 +15,7 @@ const className = computed(() => isSet.value ? "button is-rounded is-info" : "bu async function toggle() { if (isSet.value) { - await state.taskMarkedIncomplete(props.track) + await state.taskMarkedIncomplete(props.track, props.date) } else await state.taskCompleted(props.track, props.date) } diff --git a/client/src/state.ts b/client/src/state.ts index cc140cd..1b3506b 100644 --- a/client/src/state.ts +++ b/client/src/state.ts @@ -8,6 +8,15 @@ enum State { Fetched, } +function dateQuery(date: Date): URLSearchParams { + let query = new URLSearchParams() + query.set("year", date.getUTCFullYear().toString()) + query.set("month", (date.getUTCMonth() + 1).toString()) + // good thing I still had this ^^^^^^^^^^^^^^ in mind when I wrote this 😬 + query.set("day", date.getUTCDate().toString()) + return query +} + export const state = reactive({ tracks: new Array, state: State.Unfetched, @@ -62,11 +71,7 @@ export const state = reactive({ this.state = State.Fetched }, async taskCompleted(track: Track, date: Date): Promise { - let query = new URLSearchParams() - query.append("year", date.getUTCFullYear().toString()) - query.append("month", (date.getUTCMonth() + 1).toString()) - // good thing I still had this ^^^^^^^^^^^^^^ in mind when I wrote this 😬 - query.append("day", date.getUTCDate().toString()) + const query = dateQuery(date) const response: Response = await fetch(`/api/v1/tracks/${track.id}/ticked?${query.toString()}`, { method: "PATCH" }) const body = await response.text() if (!response.ok) { @@ -75,8 +80,9 @@ export const state = reactive({ } return JSON.parse(body) }, - async taskMarkedIncomplete(track: Track) { - const { ok, status, statusText } = await fetch(`/api/v1/tracks/${track.id}/all-ticks`, { method: 'DELETE' }) + async taskMarkedIncomplete(track: Track, date: Date) { + const query = dateQuery(date) + const { ok, status, statusText } = await fetch(`/api/v1/tracks/${track.id}/all-ticks?${query.toString()}`, { method: 'DELETE' }) if (!ok) error(`error deleting ticks for ${track.id}: ${statusText} (${status})`) } diff --git a/server/src/api/mod.rs b/server/src/api/mod.rs index fc35abf..bec41f1 100644 --- a/server/src/api/mod.rs +++ b/server/src/api/mod.rs @@ -87,6 +87,7 @@ pub(crate) fn start_server(db: DatabaseConnection) -> Rocket { ticked, ticked_on_date, clear_all_ticks, + clear_all_ticks_on_day, ], ) .mount( diff --git a/server/src/api/tracks.rs b/server/src/api/tracks.rs index c14789b..edfa616 100644 --- a/server/src/api/tracks.rs +++ b/server/src/api/tracks.rs @@ -158,3 +158,28 @@ pub(super) async fn clear_all_ticks( } Ok(Right(Json(ticks))) } + +#[delete("//all-ticks?&&")] +pub(super) async fn clear_all_ticks_on_day( + db: &State, + tx: &State>, + id: i32, + year: i32, + month: u32, + day: u32, +) -> ApiResult>> { + let db = db as &DatabaseConnection; + let ticks = Ticks::find() + .filter(ticks::Column::TrackId.eq(id)) + .filter(ticks::Column::Year.eq(year)) + .filter(ticks::Column::Month.eq(month)) + .filter(ticks::Column::Day.eq(day)) + .all(db) + .await + .map_err(Error::from)?; + for tick in ticks.clone() { + tick.clone().delete(db).await.map_err(Error::from)?; + Update::tick_cancelled(tick).send(&tx)?; + } + Ok(Json(ticks)) +}