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 (
|
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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user