allow to name checklist

This commit is contained in:
lubiana 2025-07-25 20:06:40 +02:00
parent c7eae58857
commit eb023a40d0
Signed by: lubiana
SSH key fingerprint: SHA256:vW1EA0fRR3Fw+dD/sM0K+x3Il2gSry6YRYHqOeQwrfk
6 changed files with 202 additions and 2 deletions

59
main.go
View file

@ -243,6 +243,17 @@ func addChecklist(name string) (string, error) {
return uuidStr, err
}
func updateChecklistName(uuid string, name string) error {
db, err := getChecklistDB(uuid)
if err != nil {
return err
}
defer db.Close()
_, err = db.Exec(`UPDATE checklist_info SET name = ? WHERE uuid = ?`, name, uuid)
return err
}
func addItem(uuid, content string, parentID *int, notBefore *time.Time, notAfter *time.Time) (ChecklistItem, error) {
db, err := getChecklistDB(uuid)
if err != nil {
@ -479,11 +490,16 @@ func deleteItem(uuid string, id int) error {
func broadcast(uuid string, msg interface{}) {
js, _ := json.Marshal(msg)
log.Printf("Broadcasting to %s: %s", uuid, string(js))
sseClientsMutex.Lock()
clientCount := len(sseClients[uuid])
log.Printf("Number of SSE clients for %s: %d", uuid, clientCount)
for ch := range sseClients[uuid] {
select {
case ch <- string(js):
log.Printf("Message sent to client")
default:
log.Printf("Channel full, skipping message")
// skip if channel is full (consider logging in prod!)
}
}
@ -688,6 +704,46 @@ func handleLockItem(w http.ResponseWriter, r *http.Request) {
})
}
func handleUpdateChecklistName(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/")
uuid := parts[3]
log.Printf("handleUpdateChecklistName called for uuid: %s", uuid)
// Ensure checklist exists
if err := ensureChecklistExists(uuid); err != nil {
log.Printf("Failed to ensure checklist exists: %v", err)
http.Error(w, "Failed to ensure checklist exists", 500)
return
}
type Req struct {
Name string `json:"name"`
}
var req Req
if err := json.NewDecoder(r.Body).Decode(&req); err != nil || strings.TrimSpace(req.Name) == "" {
log.Printf("Invalid request body: %v", err)
http.Error(w, "Missing or empty name", 400)
return
}
log.Printf("Updating checklist name to: %s", req.Name)
if err := updateChecklistName(uuid, req.Name); err != nil {
log.Printf("Failed to update checklist name: %v", err)
http.Error(w, "Failed to update checklist name", 500)
return
}
log.Printf("Checklist name updated successfully, broadcasting...")
// Broadcast name update
broadcast(uuid, map[string]interface{}{"type": "checklist_name_updated", "name": req.Name})
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"success": true,
"message": "Checklist name updated successfully",
"name": req.Name,
})
}
func lockExpiryDaemon() {
for {
time.Sleep(1 * time.Second)
@ -810,6 +866,9 @@ func main() {
log.Printf("API request: %s %s", r.Method, path)
switch {
case strings.HasSuffix(path, "/name") && r.Method == "PATCH":
log.Printf("Handling PATCH checklist name")
handleUpdateChecklistName(w, r)
case strings.HasSuffix(path, "/items") && r.Method == "GET":
log.Printf("Handling GET items")
handleGetItems(w, r)