From: PhiTux Date: Wed, 13 Aug 2025 17:25:47 +0000 (+0200) Subject: added feature to delete day X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=c4ec32142b1d3c541808cb05a6d8d786906acf63;p=DailyTxT.git added feature to delete day --- diff --git a/backend/handlers/logs.go b/backend/handlers/logs.go index a611c04..63ed42a 100644 --- a/backend/handlers/logs.go +++ b/backend/handlers/logs.go @@ -915,3 +915,92 @@ func GetHistory(w http.ResponseWriter, r *http.Request) { // Day not found utils.JSONResponse(w, http.StatusOK, []any{}) } + +// DeleteDay deletes all data of the specified day +// Also delete files, that might be uploaded +func DeleteDay(w http.ResponseWriter, r *http.Request) { + utils.UsersFileMutex.Lock() + defer utils.UsersFileMutex.Unlock() + + // Get user ID from context + userID, ok := r.Context().Value(utils.UserIDKey).(int) + if !ok { + http.Error(w, "Unauthorized", http.StatusUnauthorized) + return + } + + // Get parameters from URL + year, err := strconv.Atoi(r.URL.Query().Get("year")) + if err != nil { + http.Error(w, "Invalid year parameter", http.StatusBadRequest) + return + } + month, err := strconv.Atoi(r.URL.Query().Get("month")) + if err != nil { + http.Error(w, "Invalid month parameter", http.StatusBadRequest) + return + } + dayValue, err := strconv.Atoi(r.URL.Query().Get("day")) + if err != nil { + http.Error(w, "Invalid day parameter", http.StatusBadRequest) + return + } + + // Get month data + content, err := utils.GetMonth(userID, year, month) + if err != nil { + http.Error(w, fmt.Sprintf("Error retrieving month data: %v", err), http.StatusInternalServerError) + return + } + + days, ok := content["days"].([]any) + if !ok { + utils.JSONResponse(w, http.StatusOK, map[string]bool{"success": true}) + return + } + + for i, dayInterface := range days { + day, ok := dayInterface.(map[string]any) + if !ok { + continue + } + + dayNum, ok := day["day"].(float64) + if !ok || int(dayNum) != dayValue { + continue + } + + // Delete associated files before removing the day entry + if filesList, ok := day["files"].([]any); ok && len(filesList) > 0 { + for _, fileInterface := range filesList { + file, ok := fileInterface.(map[string]any) + if !ok { + continue + } + + if fileID, ok := file["uuid_filename"].(string); ok { + // Delete the actual file from the filesystem + err := utils.RemoveFile(userID, fileID) + if err != nil { + utils.Logger.Printf("Warning: Failed to delete file %s for user %d: %v", fileID, userID, err) + // Continue with deletion even if file removal fails + } + } + } + } + + // Remove the day from the days array + days = append(days[:i], days[i+1:]...) + content["days"] = days + + if err := utils.WriteMonth(userID, year, month, content); err != nil { + http.Error(w, fmt.Sprintf("Error writing month data: %v", err), http.StatusInternalServerError) + return + } + + utils.JSONResponse(w, http.StatusOK, map[string]bool{"success": true}) + return + } + + utils.JSONResponse(w, http.StatusOK, map[string]bool{"success": true}) +} diff --git a/backend/main.go b/backend/main.go index a230083..0f65dd8 100644 --- a/backend/main.go +++ b/backend/main.go @@ -63,6 +63,7 @@ func main() { mux.HandleFunc("GET /logs/deleteFile", middleware.RequireAuth(handlers.DeleteFile)) mux.HandleFunc("GET /logs/getHistory", middleware.RequireAuth(handlers.GetHistory)) mux.HandleFunc("GET /logs/bookmarkDay", middleware.RequireAuth(handlers.BookmarkDay)) + mux.HandleFunc("GET /logs/deleteDay", middleware.RequireAuth(handlers.DeleteDay)) // Create a handler chain with Logger and CORS middleware // Logger middleware will be executed first, then CORS diff --git a/frontend/src/routes/(authed)/write/+page.svelte b/frontend/src/routes/(authed)/write/+page.svelte index a3dcce0..3230265 100644 --- a/frontend/src/routes/(authed)/write/+page.svelte +++ b/frontend/src/routes/(authed)/write/+page.svelte @@ -18,7 +18,8 @@ faQuestionCircle, faClockRotateLeft, faArrowLeft, - faArrowRight + faArrowRight, + faTrash } from '@fortawesome/free-solid-svg-icons'; import Fa from 'svelte-fa'; import { v4 as uuidv4 } from 'uuid'; @@ -797,6 +798,46 @@ tinyMDE.setContent(currentLog); tinyMDE.setSelection({ row: 0, col: 0 }); } + + function showDeleteDayModal() { + const modal = new bootstrap.Modal(document.getElementById('modalDeleteDay')); + modal.show(); + } + + function deleteDay() { + axios + .get(API_URL + '/logs/deleteDay', { + params: { + day: $selectedDate.day, + month: $selectedDate.month, + year: $selectedDate.year + } + }) + .then((response) => { + if (response.data.success) { + currentLog = ''; + tinyMDE.setContent(currentLog); + savedLog = ''; + logDateWritten = ''; + + selectedTags = []; + history = []; + filesOfDay = []; + images = []; + $cal.daysBookmarked = $cal.daysBookmarked.filter((day) => day !== $selectedDate.day); + $cal.daysWithFiles = $cal.daysWithFiles.filter((day) => day !== $selectedDate.day); + $cal.daysWithLogs = $cal.daysWithLogs.filter((day) => day !== $selectedDate.day); + } else { + const toast = new bootstrap.Toast(document.getElementById('toastErrorDeletingDay')); + toast.show(); + } + }) + .catch((error) => { + console.error(error); + const toast = new bootstrap.Toast(document.getElementById('toastErrorDeletingDay')); + toast.show(); + }); + } @@ -853,7 +894,11 @@ {/if} -
delete
+
+ +
@@ -1099,6 +1144,18 @@
Fehler beim Download einer Datei!
+ + + +