From 7c22567b070e5161c46c080038ef1c2bf39bce5c Mon Sep 17 00:00:00 2001 From: Romulus21 Date: Mon, 26 Oct 2026 15:49:07 +0100 Subject: [PATCH] =?UTF-8?q?Permet=20de=20dupliquer=20une=20entr=C3=A9e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bouton « Dupliquer » sur chaque ligne de /entries : ouvre sous l'entrée un formulaire prérempli (début, fin, projet, note) à ajuster avant de créer la copie. Une entrée en cours propose « maintenant » comme fin. Co-Authored-By: Claude Opus 5.5 --- src/routes/pages.rs | 41 ++++++++++++++++++++++++++++ templates/_entry_duplicate.html | 15 +++++++++++ templates/_entry_row.html | 1 + tests/api.rs | 47 +++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 templates/_entry_duplicate.html diff --git a/src/routes/pages.rs b/src/routes/pages.rs index f6db7ae..ed8cf38 100644 --- a/src/routes/pages.rs +++ b/src/routes/pages.rs @@ -43,6 +43,10 @@ pub fn router() -> Router { get(entry_row).put(update_entry).delete(delete_entry), ) .route("/ui/entries/{id}/edit", get(entry_edit)) + .route( + "/ui/entries/{id}/dupliquer", + get(entry_duplicate).post(duplicate_entry), + ) } // --- Plumbing --------------------------------------------------------------- @@ -487,6 +491,15 @@ struct EntryEditTpl { groups: Vec, } +#[derive(Template)] +#[template(path = "_entry_duplicate.html")] +struct EntryDuplicateTpl { + e: Entry, + /// A running entry has no end yet: offer "now". + end: String, + groups: Vec, +} + async fn entries_page( State(pool): State, Query(filter): Query, @@ -568,6 +581,34 @@ async fn entry_edit(State(pool): State, Path(id): Path) -> Page render(EntryEditTpl { e, groups }) } +/// Form to create a copy of an entry, prefilled with its values. +async fn entry_duplicate(State(pool): State, Path(id): Path) -> PageResult { + let e = entries::get(&pool, id).await?; + let end = clock::input_value(&e.ended_at.unwrap_or_else(clock::now)); + let groups = group_projects( + projects::list(&pool, None, false).await?, + Some(e.project_id), + ); + render(EntryDuplicateTpl { e, end, groups }) +} + +/// Creates the copy and swaps the form for its row. +async fn duplicate_entry(State(pool): State, Form(f): Form) -> PageResult { + let started = parse_datetime(&f.started_at, "début")?; + let ended = parse_datetime(&f.ended_at, "fin")?; + let e = entries::create( + &pool, + &NewEntry { + project_id: f.project_id, + started_at: started.fixed_offset(), + ended_at: ended.fixed_offset(), + note: f.note, + }, + ) + .await?; + Ok(with_entries_changed(render(EntryRowTpl { e })?)) +} + async fn update_entry( State(pool): State, Path(id): Path, diff --git a/templates/_entry_duplicate.html b/templates/_entry_duplicate.html new file mode 100644 index 0000000..e7fa78e --- /dev/null +++ b/templates/_entry_duplicate.html @@ -0,0 +1,15 @@ + + +
+ + +
+ + + + + + + + + diff --git a/templates/_entry_row.html b/templates/_entry_row.html index 11707a9..c7748af 100644 --- a/templates/_entry_row.html +++ b/templates/_entry_row.html @@ -9,6 +9,7 @@ {{ e.duration() }} + diff --git a/tests/api.rs b/tests/api.rs index 0077ad8..43af77f 100644 --- a/tests/api.rs +++ b/tests/api.rs @@ -618,3 +618,50 @@ async fn colors() { assert_eq!(status, StatusCode::OK); assert!(html.contains("--color: #000000")); } + +#[tokio::test] +async fn duplicate_entry() { + let app = app().await; + let p = project(&app, "Acme", "Site web").await; + let (status, _) = send( + &app, + "POST", + "/api/entries", + Some(json!({ + "project_id": p, + "started_at": local(2026, 3, 10, 9, 0), + "ended_at": local(2026, 3, 10, 10, 15), + "note": "réunion", + })), + ) + .await; + assert_eq!(status, StatusCode::CREATED); + + let (status, html) = get_html(&app, "/ui/entries/1/dupliquer").await; + assert_eq!(status, StatusCode::OK); + assert!(html.contains("2026-03-10T09:00") && html.contains("2026-03-10T10:15")); + assert!(html.contains("réunion")); + + let (status, html) = send_form( + &app, + "/ui/entries/1/dupliquer", + &format!("project_id={p}&started_at=2026-03-11T09%3A00&ended_at=2026-03-11T10%3A15¬e=r%C3%A9union"), + ) + .await; + assert_eq!(status, StatusCode::OK); + assert!(html.contains("id=\"entry-2\"") && html.contains("mer. 11/03/2026")); + + let (_, list) = send( + &app, + "GET", + "/api/entries?from=2026-03-09&to=2026-03-15", + None, + ) + .await; + let list = list.as_array().unwrap(); + assert_eq!(list.len(), 2); + assert!(list.iter().all(|e| e["note"] == "réunion")); + + let (status, _) = get_html(&app, "/ui/entries/99/dupliquer").await; + assert_eq!(status, StatusCode::NOT_FOUND); +}