Adding new errors and centralising handling

This commit is contained in:
2026-03-18 22:41:22 +00:00
parent 8749ff3c03
commit a33cedf09e
3 changed files with 77 additions and 10 deletions

View File

@@ -0,0 +1,35 @@
package repository
import (
"fmt"
"git.hrafn.xyz/aether/notes/internal/models"
)
type EmptyContent struct {}
type EmptyTitle struct {}
type NoOP struct {}
type EmptyUpdate struct{}
type TitleOverflow struct {
length int
}
func (e *EmptyContent) Error() string {
return "content cannot be empty"
}
func (e *EmptyTitle) Error() string {
return "title cannot be empty"
}
func (e *NoOP) Error() string {
return "no operation required"
}
func (e *EmptyUpdate) Error() string {
return "update cannot be empty"
}
func (e *TitleOverflow) Error() string {
return fmt.Sprintf("title max length %d, %d provided", models.NoteTitleMaxLength, e.length)
}

View File

@@ -2,7 +2,6 @@ package repository
import ( import (
"context" "context"
"fmt"
"time" "time"
"git.hrafn.xyz/aether/notes/internal/models" "git.hrafn.xyz/aether/notes/internal/models"
@@ -25,13 +24,14 @@ func NewNoteRepository(store NoteStore) *NoteRepository {
// CreateNote creates a new note with the given content. // CreateNote creates a new note with the given content.
func (r *NoteRepository) CreateNote(ctx context.Context, title string, content string) (models.Note, error) { func (r *NoteRepository) CreateNote(ctx context.Context, title string, content string) (models.Note, error) {
if content == "" {
return models.Note{}, fmt.Errorf("content cannot be empty")
}
note := models.Note{ note := models.Note{
Title: title,
Content: content, Content: content,
LastUpdate: time.Now(), LastUpdate: time.Now(),
} }
if err := isNoteVaid(note); err != nil {
return models.Note{}, err
}
return r.store.SaveNote(ctx, note) return r.store.SaveNote(ctx, note)
} }
@@ -46,15 +46,30 @@ func (r *NoteRepository) ListNotes(ctx context.Context) ([]models.Note, error) {
} }
// UpdateNote updates the content of an existing note. // UpdateNote updates the content of an existing note.
func (r *NoteRepository) UpdateNote(ctx context.Context, id int, content string) (models.Note, error) { func (r *NoteRepository) UpdateNote(ctx context.Context, id int, update NoteUpdate) (models.Note, error) {
if content == "" { if update.Content == "" && update.Title == "" {
return models.Note{}, fmt.Errorf("content cannot be empty") return models.Note{}, &EmptyUpdate{}
} }
note, err := r.store.GetNoteByID(ctx, id) note, err := r.store.GetNoteByID(ctx, id)
if err != nil { if err != nil {
return models.Note{}, err return models.Note{}, err
} }
note.Content = content
if update.Content == note.Content && update.Title == note.Title {
return models.Note{}, &NoOP{}
}
if update.Content != "" {
note.Content = update.Content
}
if update.Title != "" {
note.Title = update.Title
}
if err := isNoteVaid(note); err != nil {
return models.Note{}, err
}
note.LastUpdate = time.Now() note.LastUpdate = time.Now()
return r.store.SaveNote(ctx, note) return r.store.SaveNote(ctx, note)
} }
@@ -63,3 +78,20 @@ func (r *NoteRepository) UpdateNote(ctx context.Context, id int, content string)
func (r *NoteRepository) DeleteNote(ctx context.Context, id int) error { func (r *NoteRepository) DeleteNote(ctx context.Context, id int) error {
return r.store.DeleteNoteByID(ctx, id) return r.store.DeleteNoteByID(ctx, id)
} }
func isNoteVaid(note models.Note) error {
if note.Title == "" {
return &EmptyTitle{}
}
if note.Content == "" {
return &EmptyContent{}
}
if titleLen := len(note.Title); titleLen > models.NoteTitleMaxLength {
return &TitleOverflow{
length: titleLen,
}
}
return nil
}

View File

@@ -85,7 +85,7 @@ func (s *Server) getNotes(w http.ResponseWriter, r *http.Request) {
func (s *Server) createNote(w http.ResponseWriter, r *http.Request) { func (s *Server) createNote(w http.ResponseWriter, r *http.Request) {
note := struct { note := struct {
Content string `json:"content"` Content string `json:"content"`
Title string `json:"title"` Title string `json:"title"`
}{} }{}
if err := json.NewDecoder(r.Body).Decode(&note); err != nil { if err := json.NewDecoder(r.Body).Decode(&note); err != nil {
http.Error(w, "invalid request body", http.StatusBadRequest) http.Error(w, "invalid request body", http.StatusBadRequest)
@@ -123,7 +123,7 @@ func (s *Server) updateNoteByID(w http.ResponseWriter, r *http.Request) {
} }
note := struct { note := struct {
Content string `json:"content"` Content string `json:"content"`
Title string `json:"title"` Title string `json:"title"`
}{} }{}
if err := json.NewDecoder(r.Body).Decode(&note); err != nil { if err := json.NewDecoder(r.Body).Decode(&note); err != nil {
http.Error(w, "invalid request body", http.StatusBadRequest) http.Error(w, "invalid request body", http.StatusBadRequest)