Refactoring validation to belong to model
This commit is contained in:
@@ -29,7 +29,7 @@ func (r *NoteRepository) CreateNote(ctx context.Context, title string, content s
|
||||
Content: content,
|
||||
LastUpdate: time.Now(),
|
||||
}
|
||||
if err := isNoteVaid(note); err != nil {
|
||||
if err := note.Validate(); err != nil {
|
||||
return models.Note{}, err
|
||||
}
|
||||
return r.store.SaveNote(ctx, note)
|
||||
@@ -48,7 +48,7 @@ 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, update NoteUpdate) (models.Note, error) {
|
||||
if update.Content == "" && update.Title == "" {
|
||||
return models.Note{}, &EmptyUpdate{}
|
||||
return models.Note{}, &ErrEmptyUpdate{}
|
||||
}
|
||||
note, err := r.store.GetNoteByID(ctx, id)
|
||||
if err != nil {
|
||||
@@ -56,7 +56,7 @@ func (r *NoteRepository) UpdateNote(ctx context.Context, id int, update NoteUpda
|
||||
}
|
||||
|
||||
if update.Content == note.Content && update.Title == note.Title {
|
||||
return models.Note{}, &NoOP{}
|
||||
return models.Note{}, &ErrNoOP{}
|
||||
}
|
||||
|
||||
if update.Content != "" {
|
||||
@@ -65,8 +65,8 @@ func (r *NoteRepository) UpdateNote(ctx context.Context, id int, update NoteUpda
|
||||
if update.Title != "" {
|
||||
note.Title = update.Title
|
||||
}
|
||||
|
||||
if err := isNoteVaid(note); err != nil {
|
||||
|
||||
if err := note.Validate(); err != nil {
|
||||
return models.Note{}, err
|
||||
}
|
||||
|
||||
@@ -78,20 +78,3 @@ func (r *NoteRepository) UpdateNote(ctx context.Context, id int, update NoteUpda
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user