- Responsivo ≤760px: sidebar vira gaveta com backdrop, painel de versões em tela cheia, toolbar/menu rolam sem rótulos, alvos de toque, dvh + safe-area, fonte do editor ≥16px (anti-zoom iOS) - Abas: várias notas abertas com estado sujo por aba, ✕ com confirmação, clique do meio, Alt+W / Alt+←/→ / Alt+1..9; abas e rascunhos sobrevivem à sessão via localStorage - Categorias: modelo vira árvore (categories.json) com notas em múltiplas categorias; explorador no sidebar com expandir/renomear/mover/excluir (sem ciclos, GC de vínculos) e seletor multi-marca no editor; filtro por pasta recursivo por padrão; migração automática do campo único legado - API: POST/PUT/DELETE /api/categories; notas aceitam categories: [ids] - Testes: store (árvore, ciclo, migração, GC) e api (CRUD de categorias)
168 lines
4.3 KiB
Go
168 lines
4.3 KiB
Go
package api
|
|
|
|
import (
|
|
"io/fs"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/lgcosta/notas-online/internal/store"
|
|
)
|
|
|
|
// serveIndex entrega o shell da aplicação.
|
|
func (s *Server) serveIndex(w http.ResponseWriter, r *http.Request) {
|
|
data, err := fs.ReadFile(s.webFS, "index.html")
|
|
if err != nil {
|
|
http.Error(w, "frontend ausente no binário", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
_, _ = w.Write(data)
|
|
}
|
|
|
|
// noteInput é o corpo aceito por POST/PUT de notas.
|
|
type noteInput struct {
|
|
Title string `json:"title"`
|
|
Format string `json:"format"`
|
|
Categories []string `json:"categories"` // IDs da árvore de categorias
|
|
Content string `json:"content"`
|
|
Author string `json:"author"`
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
func (in noteInput) fields() (string, string, []string, string, string, string) {
|
|
f := in.Format
|
|
if f == "" {
|
|
f = store.FormatTxt
|
|
}
|
|
return in.Title, f, in.Categories, in.Content, in.Author, in.Message
|
|
}
|
|
|
|
// noteView é a representação completa (meta + conteúdo) de uma nota.
|
|
type noteView struct {
|
|
*store.Note
|
|
Content string `json:"content"`
|
|
}
|
|
|
|
func (s *Server) handleListNotes(w http.ResponseWriter, r *http.Request) {
|
|
q := r.URL.Query()
|
|
recursive := q.Get("recursive") != "0"
|
|
notes, err := s.st.List(store.Filter{
|
|
Category: q.Get("category"),
|
|
Recursive: recursive,
|
|
Tag: q.Get("tag"),
|
|
Query: q.Get("q"),
|
|
})
|
|
if err != nil {
|
|
mapStoreError(w, err)
|
|
return
|
|
}
|
|
if notes == nil {
|
|
notes = []*store.Note{}
|
|
}
|
|
writeJSON(w, http.StatusOK, notes)
|
|
}
|
|
|
|
func (s *Server) handleCreateNote(w http.ResponseWriter, r *http.Request) {
|
|
var in noteInput
|
|
if err := decodeJSON(r, &in, maxBody); err != nil {
|
|
writeError(w, http.StatusBadRequest, "corpo JSON inválido")
|
|
return
|
|
}
|
|
title, format, categories, content, author, message := in.fields()
|
|
n, err := s.st.Create(title, format, categories, content, author, message)
|
|
if err != nil {
|
|
mapStoreError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusCreated, noteView{n, content})
|
|
}
|
|
|
|
func (s *Server) handleGetNote(w http.ResponseWriter, r *http.Request) {
|
|
content, n, err := s.st.Content(r.PathValue("id"))
|
|
if err != nil {
|
|
mapStoreError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, noteView{n, content})
|
|
}
|
|
|
|
func (s *Server) handleUpdateNote(w http.ResponseWriter, r *http.Request) {
|
|
var in noteInput
|
|
if err := decodeJSON(r, &in, maxBody); err != nil {
|
|
writeError(w, http.StatusBadRequest, "corpo JSON inválido")
|
|
return
|
|
}
|
|
title, format, categories, content, author, message := in.fields()
|
|
n, err := s.st.Update(r.PathValue("id"), title, format, categories, content, author, message)
|
|
if err != nil {
|
|
mapStoreError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, noteView{n, content})
|
|
}
|
|
|
|
func (s *Server) handleDeleteNote(w http.ResponseWriter, r *http.Request) {
|
|
if err := s.st.Delete(r.PathValue("id")); err != nil {
|
|
mapStoreError(w, err)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
// handleRaw baixa a nota como arquivo (Content-Disposition attachment).
|
|
func (s *Server) handleRaw(w http.ResponseWriter, r *http.Request) {
|
|
content, n, err := s.st.Content(r.PathValue("id"))
|
|
if err != nil {
|
|
mapStoreError(w, err)
|
|
return
|
|
}
|
|
ctype := "text/plain; charset=utf-8"
|
|
switch n.Format {
|
|
case store.FormatMD:
|
|
ctype = "text/markdown; charset=utf-8"
|
|
case store.FormatHTML:
|
|
ctype = "text/html; charset=utf-8"
|
|
}
|
|
w.Header().Set("Content-Type", ctype)
|
|
w.Header().Set("Content-Disposition",
|
|
`attachment; filename="`+sanitizeFilename(n.Filename())+`"`)
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
_, _ = w.Write([]byte(content))
|
|
}
|
|
|
|
// sanitizeFilename elimina caracteres que quebrariam o header.
|
|
func sanitizeFilename(name string) string {
|
|
return strings.Map(func(r rune) rune {
|
|
switch r {
|
|
case '"', '\\', '\n', '\r', '/':
|
|
return '_'
|
|
}
|
|
return r
|
|
}, name)
|
|
}
|
|
|
|
func (s *Server) handleTags(w http.ResponseWriter, r *http.Request) {
|
|
tags, err := s.st.Tags()
|
|
if err != nil {
|
|
mapStoreError(w, err)
|
|
return
|
|
}
|
|
if tags == nil {
|
|
tags = []store.TagCount{}
|
|
}
|
|
writeJSON(w, http.StatusOK, tags)
|
|
}
|
|
|
|
func (s *Server) handleCategories(w http.ResponseWriter, r *http.Request) {
|
|
cats, err := s.st.Categories()
|
|
if err != nil {
|
|
mapStoreError(w, err)
|
|
return
|
|
}
|
|
if cats == nil {
|
|
cats = []store.CategoryView{}
|
|
}
|
|
writeJSON(w, http.StatusOK, cats)
|
|
}
|