package handlers import ( "log" "net/http" "strconv" "github.com/gin-gonic/gin" "kenaito-notebook-server/services" ) type NoteHandler struct { db *services.Database } func NewNoteHandler(db *services.Database) *NoteHandler { return &NoteHandler{db: db} } func (h *NoteHandler) GetNoteTree(c *gin.Context) { userID := c.GetInt64("userID") result, err := h.db.GetNoteTree(userID) if err != nil { log.Printf("[API] GetNoteTree error: %v", err) c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, result) } func (h *NoteHandler) GetNote(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"}) return } note, err := h.db.GetNote(id) if err != nil { c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, note) } func (h *NoteHandler) CreateNote(c *gin.Context) { userID := c.GetInt64("userID") var req struct { ParentID int64 `json:"parentId"` Title string `json:"title"` IsFolder bool `json:"isFolder"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } note, err := h.db.CreateNote(userID, req.ParentID, req.Title, req.IsFolder) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusCreated, note) } func (h *NoteHandler) UpdateNote(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"}) return } var req struct { Title string `json:"title"` Content string `json:"content"` Tag string `json:"tag"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } if err := h.db.UpdateNote(id, req.Title, req.Content, req.Tag); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"message": "updated"}) } func (h *NoteHandler) DeleteNote(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"}) return } if err := h.db.DeleteNote(id); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"message": "deleted"}) } func (h *NoteHandler) SearchNotes(c *gin.Context) { userID := c.GetInt64("userID") keyword := c.Query("q") if keyword == "" { c.JSON(http.StatusBadRequest, gin.H{"error": "keyword is required"}) return } notes, err := h.db.SearchNotes(userID, keyword) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, notes) } func (h *NoteHandler) GetTrashNotes(c *gin.Context) { userID := c.GetInt64("userID") notes, err := h.db.GetTrashNotes(userID) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, notes) } func (h *NoteHandler) RestoreNote(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"}) return } if err := h.db.RestoreNote(id); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"message": "restored"}) } func (h *NoteHandler) PermanentDeleteNote(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"}) return } if err := h.db.PermanentDeleteNote(id); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"message": "permanently deleted"}) } func (h *NoteHandler) EmptyTrash(c *gin.Context) { userID := c.GetInt64("userID") if err := h.db.EmptyTrash(userID); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"message": "trash emptied"}) }