2025-02-21 09:35:37 +01:00
|
|
|
package notes
|
|
|
|
|
|
|
|
import (
|
2025-02-28 16:56:19 +01:00
|
|
|
"archive/zip"
|
|
|
|
"fmt"
|
2025-02-21 09:35:37 +01:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Handler handles HTTP requests for notes
|
|
|
|
type Handler struct {
|
|
|
|
service *Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHandler creates a new notes handler
|
|
|
|
func NewHandler(service *Service) *Handler {
|
|
|
|
return &Handler{service: service}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterRoutes registers the note routes with the given router group
|
|
|
|
func (h *Handler) RegisterRoutes(group *gin.RouterGroup) {
|
|
|
|
notes := group.Group("/notes")
|
|
|
|
{
|
|
|
|
notes.GET("", h.handleGetNotes)
|
|
|
|
notes.POST("", h.handleCreateNote)
|
|
|
|
notes.GET("/:id", h.handleGetNote)
|
|
|
|
notes.PUT("/:id", h.handleUpdateNote)
|
|
|
|
notes.DELETE("/:id", h.handleDeleteNote)
|
2025-02-28 16:56:19 +01:00
|
|
|
notes.POST("/import", h.handleImportObsidianVault)
|
2025-02-21 09:35:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test endpoint
|
|
|
|
group.POST("/test/reset", h.handleReset)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) handleGetNotes(c *gin.Context) {
|
|
|
|
notes, err := h.service.List()
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, notes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) handleCreateNote(c *gin.Context) {
|
|
|
|
var note Note
|
|
|
|
if err := c.ShouldBindJSON(¬e); err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.service.Create(¬e); err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, note)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) handleGetNote(c *gin.Context) {
|
|
|
|
id := c.Param("id")
|
|
|
|
note, err := h.service.Get(id)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if note == nil {
|
|
|
|
c.Status(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, note)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) handleUpdateNote(c *gin.Context) {
|
|
|
|
id := c.Param("id")
|
|
|
|
var note Note
|
|
|
|
if err := c.ShouldBindJSON(¬e); err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
updates := map[string]interface{}{
|
|
|
|
"title": note.Title,
|
|
|
|
"content": note.Content,
|
|
|
|
"updated_at": note.UpdatedAt,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.service.Update(id, updates); err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the updated note
|
|
|
|
updated, err := h.service.Get(id)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if updated == nil {
|
|
|
|
c.Status(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, updated)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) handleDeleteNote(c *gin.Context) {
|
|
|
|
id := c.Param("id")
|
|
|
|
if err := h.service.Delete(id); err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) handleReset(c *gin.Context) {
|
|
|
|
if err := h.service.Reset(); err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Status(http.StatusOK)
|
2025-02-21 10:48:26 +01:00
|
|
|
}
|
2025-02-28 16:56:19 +01:00
|
|
|
|
|
|
|
func (h *Handler) handleImportObsidianVault(c *gin.Context) {
|
|
|
|
file, _, err := c.Request.FormFile("file")
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Failed to get file: %v", err)})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if closeErr := file.Close(); closeErr != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to close file: %v", closeErr)})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Create a zip reader
|
|
|
|
zipReader, err := zip.NewReader(file, c.Request.ContentLength)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Failed to read zip file: %v", err)})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Import the vault
|
|
|
|
imported, err := h.service.ImportObsidianVault(zipReader)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"imported": imported})
|
|
|
|
}
|