diff --git a/backend/mvp.py b/backend/mvp.py index c3992a7..7dc367b 100644 --- a/backend/mvp.py +++ b/backend/mvp.py @@ -630,6 +630,14 @@ async def archive_habit(habit_id: UUID, user: User = Depends(current_user), db: row = await owned_habit(db, user.id, habit_id); row.archived_at = utcnow(); await db.commit(); return Response(status_code=204) +@router.delete("/habits/{habit_id}/permanent", status_code=204) +async def delete_habit_permanently(habit_id: UUID, user: User = Depends(current_user), db: AsyncSession = Depends(get_db)): + row = await owned_habit(db, user.id, habit_id) + await db.delete(row) + await db.commit() + return Response(status_code=204) + + @router.post("/habits/{habit_id}/logs") async def add_habit_log(habit_id: UUID, payload: HabitLogInput, user: User = Depends(current_user), db: AsyncSession = Depends(get_db)): habit = await owned_habit(db, user.id, habit_id) diff --git a/frontend/src/MvpPanel.vue b/frontend/src/MvpPanel.vue index 5ea6e45..b005268 100644 --- a/frontend/src/MvpPanel.vue +++ b/frontend/src/MvpPanel.vue @@ -322,6 +322,15 @@ async function archiveHabit(h: Habit) { emit('notice', '习惯已归档') }) } +async function deleteHabit(h: Habit) { + if (!confirm(`永久删除习惯“${h.name}”?所有历史打卡记录也会被删除,且无法恢复。`)) return + await safe(async () => { + await request(`/habits/${h.id}/permanent`, { method: 'DELETE' }) + selectedHabit.value = null + await loadHabits() + emit('notice', '习惯已永久删除') + }) +} function refreshHabitDay() { const next = dateKey(new Date()) if (next !== todayKey.value) { @@ -482,7 +491,7 @@ onBeforeUnmount(() => { diff --git a/frontend/src/style.css b/frontend/src/style.css index 70a2e62..c62dd08 100644 --- a/frontend/src/style.css +++ b/frontend/src/style.css @@ -26,7 +26,7 @@ main{min-width:0;padding:27px 34px 50px;overflow:auto;background:linear-gradient .habit-row .icon.ghost:hover{color:var(--danger)} .numeric-habit>span:first-child{display:grid;gap:3px}.numeric-habit small{font-size:11px;color:var(--muted)} .numeric-action{display:flex;gap:6px;align-items:center}.numeric-action input{width:74px;border:1px solid var(--line);border-radius:8px;padding:7px;background:#fff}.numeric-action .soft-button{min-height:44px;padding:9px 12px} -.habit-row>.icon.ghost{width:44px;height:44px;flex:0 0 44px}.habit-check{margin-left:-4px}.habit-row.done .habit-check .task-check-mark{background:#71856b;border-color:#71856b;color:#fff}.habit-toolbar{display:flex;align-items:center;gap:12px;justify-content:flex-end;color:var(--muted);font-size:12px}.habit-toolbar label{display:inline-flex;align-items:center;gap:6px}.habit-toolbar input{margin:0}.habit-toolbar-today{justify-content:flex-start;margin:0 0 2px 2px}.habit-detail-mask{position:fixed;z-index:85;inset:0;background:rgba(45,38,31,.36);display:grid;place-items:end center;padding:20px}.habit-detail-sheet{width:min(500px,100%);background:#fffdf8;border:1px solid var(--line);border-radius:20px;box-shadow:0 12px 36px rgba(56,40,24,.18);padding:18px;display:grid;gap:16px}.habit-detail-sheet header{display:flex;align-items:center;justify-content:space-between;gap:10px}.habit-detail-sheet header small{color:var(--muted);font-size:11px}.habit-detail-sheet header h3{margin:2px 0 0;font-size:19px}.habit-detail-sheet header button{width:44px;height:44px;border:0;background:transparent;display:grid;place-items:center}.habit-detail-progress{display:flex;justify-content:space-between;align-items:center;padding:14px 0;border-block:1px solid var(--line);font-size:12px;color:var(--muted)}.habit-detail-progress strong{font-size:14px;color:#3c372f}.habit-detail-sheet footer{display:flex;justify-content:flex-end} +.habit-row>.icon.ghost{width:44px;height:44px;flex:0 0 44px}.habit-check{margin-left:-4px}.habit-row.done .habit-check .task-check-mark{background:#71856b;border-color:#71856b;color:#fff}.habit-toolbar{display:flex;align-items:center;gap:12px;justify-content:flex-end;color:var(--muted);font-size:12px}.habit-toolbar label{display:inline-flex;align-items:center;gap:6px}.habit-toolbar input{margin:0}.habit-toolbar-today{justify-content:flex-start;margin:0 0 2px 2px}.habit-detail-mask{position:fixed;z-index:85;inset:0;background:rgba(45,38,31,.36);display:grid;place-items:end center;padding:20px}.habit-detail-sheet{width:min(500px,100%);background:#fffdf8;border:1px solid var(--line);border-radius:20px;box-shadow:0 12px 36px rgba(56,40,24,.18);padding:18px;display:grid;gap:16px}.habit-detail-sheet header{display:flex;align-items:center;justify-content:space-between;gap:10px}.habit-detail-sheet header small{color:var(--muted);font-size:11px}.habit-detail-sheet header h3{margin:2px 0 0;font-size:19px}.habit-detail-sheet header button{width:44px;height:44px;border:0;background:transparent;display:grid;place-items:center}.habit-detail-progress{display:flex;justify-content:space-between;align-items:center;padding:14px 0;border-block:1px solid var(--line);font-size:12px;color:var(--muted)}.habit-detail-progress strong{font-size:14px;color:#3c372f}.habit-detail-sheet footer{display:flex;justify-content:flex-end;gap:10px;flex-wrap:wrap}.habit-detail-sheet .habit-delete-button{font-weight:750} .empty-panel{text-align:center;color:var(--muted);display:grid;place-items:center;gap:10px}.today-empty-panel{min-height:130px}.empty-action{margin-top:4px;color:#655d52}.empty-action svg{width:15px;height:15px} .settings-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:12px}.tool-card{display:flex;flex-direction:column;align-items:flex-start;gap:10px}.tool-card>svg{color:var(--accent);width:25px;height:25px}.tool-card p{margin:0;color:var(--muted);font-size:13px}.tool-card.wide{grid-column:1/-1}.password-form{width:100%;display:grid;gap:10px}.password-form label{display:grid;gap:6px;color:#675f54;font-size:12px;font-weight:650}.password-form input{width:100%;border:1px solid var(--line);background:#fff;border-radius:10px;padding:11px 12px;outline:none}.password-form input:focus{border-color:var(--accent);box-shadow:0 0 0 3px rgba(241,90,41,.1)}.password-form button{justify-self:start}.file-button input{display:none}.tool-card pre{width:100%;max-height:180px;overflow:auto;background:#f8f3e8;padding:10px;border-radius:8px;font-size:10px}.session-row,.audit-row{width:100%;display:flex;justify-content:space-between;align-items:center;border-top:1px solid var(--line);padding:9px 0}.session-row span{display:grid}.session-row small,.audit-row small{color:var(--muted);font-size:11px}.inline-error{padding:9px;border-radius:8px;color:var(--danger);background:#fff0ed}.settings.active{background:var(--accent-soft);color:#b7421e;font-weight:700} @media(max-width:930px){.task-row,.habit-row,.countdown-row{min-height:62px;background:#fff;border:1px solid var(--line);border-radius:13px;box-shadow:none}.task-list,.habit-list,.countdown-timeline{display:grid;gap:8px}.task-row{padding:4px 7px}.task-row:hover,.task-row.selected{background:#fffaf5}.habit-row{padding:4px 7px}.countdown-row,.countdown-row:first-of-type{border:1px solid var(--line)}.countdown-row{grid-template-columns:minmax(0,1fr) 72px;padding:8px 9px;gap:8px}.task-main strong,.habit-name,.countdown-main>b{font-size:14px;font-weight:650}.meta,.countdown-main>small,.countdown-state small{font-size:11px;color:var(--muted)}.habit-progress{font-size:16px}.drag-handle{width:36px;flex-basis:36px}.task-check{width:44px;flex-basis:44px}.countdown-icon{width:36px;height:36px;border-radius:10px}.countdown-state strong{font-size:24px}.countdown-group{gap:8px}.countdown-group>h3{padding-left:3px}.habit-detail-mask{padding:0}.habit-detail-sheet{width:100%;border-radius:22px 22px 0 0;padding:18px 16px calc(18px + env(safe-area-inset-bottom))}} diff --git a/frontend/src/style.test.ts b/frontend/src/style.test.ts index 6820cbf..469f035 100644 --- a/frontend/src/style.test.ts +++ b/frontend/src/style.test.ts @@ -48,6 +48,9 @@ describe('mobile list row language', () => { expect(mvpPanel).toContain('class="habit-detail-mask"') expect(mvpPanel).toContain('class="habit-detail-sheet"') expect(mvpPanel).toContain('@click="archiveHabit(selectedHabit)"') + expect(mvpPanel).toContain('@click="deleteHabit(selectedHabit)"') + expect(mvpPanel).toContain('永久删除') + expect(mvpPanel).toContain("request(`/habits/${h.id}/permanent`, { method: 'DELETE' })") expect(mvpPanel).toContain('@click="openHabitDetail(h, $event.currentTarget as HTMLElement)"') expect(mvpPanel).toContain('@keydown.enter.prevent="openHabitDetail(h, $event.currentTarget as HTMLElement)"') expect(mvpPanel).toContain('ref="habitDetailSheet"') diff --git a/tests/test_mvp_backend.py b/tests/test_mvp_backend.py index 4462f06..1446024 100644 --- a/tests/test_mvp_backend.py +++ b/tests/test_mvp_backend.py @@ -272,6 +272,22 @@ def test_habits_numeric_accumulation_pause_archive_grid_and_stats(client): assert client.get(f"/api/v1/habits/{habit_id}/stats").json()["total"] == 8 +def test_habit_permanent_delete_removes_habit_and_history(client): + boot(client) + created = client.post( + "/api/v1/habits", + json={"name": "待删除习惯", "kind": "numeric", "target": 3, "schedule_type": "daily"}, + ) + habit_id = created.json()["id"] + today = datetime.now(UTC).date().isoformat() + assert client.put(f"/api/v1/habits/{habit_id}/logs/{today}", json={"value": 2}).status_code == 200 + + assert client.delete(f"/api/v1/habits/{habit_id}/permanent").status_code == 204 + assert all(row["id"] != habit_id for row in client.get("/api/v1/habits").json()) + assert all(row["id"] != habit_id for row in client.get("/api/v1/habits", params={"archived": True}).json()) + assert client.get(f"/api/v1/habits/{habit_id}/stats").status_code == 404 + + def test_boolean_interval_habit_schedule(client): boot(client) habit = client.post(