draft shits
This commit is contained in:
76
internal/service/note.go
Normal file
76
internal/service/note.go
Normal file
@ -0,0 +1,76 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"donniemarko/internal/note"
|
||||
"donniemarko/internal/storage"
|
||||
)
|
||||
|
||||
|
||||
type NotesService struct {
|
||||
storage storage.Storage
|
||||
}
|
||||
|
||||
type SortOption func([]*note.Note)
|
||||
|
||||
type QueryOptions struct {
|
||||
SearchTerm string
|
||||
SortBy string
|
||||
}
|
||||
|
||||
func NewNoteService() *NotesService {
|
||||
return &NotesService{}
|
||||
}
|
||||
|
||||
func SortByDate(notes []*note.Note) {
|
||||
sort.Slice(notes, func(i, j int) bool {
|
||||
return notes[i].UpdatedAt.After(notes[j].UpdatedAt)
|
||||
})
|
||||
}
|
||||
|
||||
func SortByTitle(notes []*note.Note) {
|
||||
sort.Slice(notes, func(i, j int) bool {
|
||||
return notes[i].Title < notes[j].Title
|
||||
})
|
||||
}
|
||||
|
||||
func SortByDateAsc(notes []*note.Note) {
|
||||
sort.Slice(notes, func(i, j int) bool {
|
||||
return notes[i].UpdatedAt.Before(notes[j].UpdatedAt)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *NotesService) GetNotes(sortBy SortOption) ([]*note.Note, error) {
|
||||
notes := s.storage.GetAll()
|
||||
|
||||
if sortBy != nil {
|
||||
sortBy(notes)
|
||||
}
|
||||
|
||||
return notes, nil
|
||||
}
|
||||
|
||||
func (s *NotesService) QueryNotes(opts QueryOptions) ([]*note.Note, error) {
|
||||
var notes []*note.Note
|
||||
|
||||
// Search or get all
|
||||
if opts.SearchTerm != "" {
|
||||
notes = s.storage.Search(opts.SearchTerm)
|
||||
} else {
|
||||
notes = s.storage.GetAll()
|
||||
}
|
||||
|
||||
// Apply sorting
|
||||
switch opts.SortBy {
|
||||
case "recent":
|
||||
SortByDate(notes)
|
||||
case "alpha":
|
||||
SortByTitle(notes)
|
||||
case "oldest":
|
||||
SortByDateAsc(notes)
|
||||
default:
|
||||
SortByDate(notes) // Default sort
|
||||
}
|
||||
|
||||
return notes, nil
|
||||
}
|
||||
Reference in New Issue
Block a user