Adding new errors and centralising handling
This commit is contained in:
35
internal/repository/errors.go
Normal file
35
internal/repository/errors.go
Normal 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)
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"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.
|
||||
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{
|
||||
Title: title,
|
||||
Content: content,
|
||||
LastUpdate: time.Now(),
|
||||
}
|
||||
if err := isNoteVaid(note); err != nil {
|
||||
return models.Note{}, err
|
||||
}
|
||||
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.
|
||||
func (r *NoteRepository) UpdateNote(ctx context.Context, id int, content string) (models.Note, error) {
|
||||
if content == "" {
|
||||
return models.Note{}, fmt.Errorf("content cannot be empty")
|
||||
func (r *NoteRepository) UpdateNote(ctx context.Context, id int, update NoteUpdate) (models.Note, error) {
|
||||
if update.Content == "" && update.Title == "" {
|
||||
return models.Note{}, &EmptyUpdate{}
|
||||
}
|
||||
note, err := r.store.GetNoteByID(ctx, id)
|
||||
if err != nil {
|
||||
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()
|
||||
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 {
|
||||
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
|
||||
}
|
||||
@@ -85,7 +85,7 @@ func (s *Server) getNotes(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) createNote(w http.ResponseWriter, r *http.Request) {
|
||||
note := struct {
|
||||
Content string `json:"content"`
|
||||
Title string `json:"title"`
|
||||
Title string `json:"title"`
|
||||
}{}
|
||||
if err := json.NewDecoder(r.Body).Decode(¬e); err != nil {
|
||||
http.Error(w, "invalid request body", http.StatusBadRequest)
|
||||
@@ -123,7 +123,7 @@ func (s *Server) updateNoteByID(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
note := struct {
|
||||
Content string `json:"content"`
|
||||
Title string `json:"title"`
|
||||
Title string `json:"title"`
|
||||
}{}
|
||||
if err := json.NewDecoder(r.Body).Decode(¬e); err != nil {
|
||||
http.Error(w, "invalid request body", http.StatusBadRequest)
|
||||
|
||||
Reference in New Issue
Block a user