draft shits

This commit is contained in:
2026-02-01 11:55:16 +01:00
parent d17ed8c650
commit e27aadc603
22 changed files with 1341 additions and 0 deletions

76
internal/service/note.go Normal file
View 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
}