possibly first working draft
This commit is contained in:
@ -1,76 +1,93 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"donniemarko/internal/note"
|
||||
"donniemarko/internal/storage"
|
||||
"sort"
|
||||
)
|
||||
|
||||
|
||||
type NotesService struct {
|
||||
storage storage.Storage
|
||||
storage storage.Storage
|
||||
}
|
||||
|
||||
type SortOption func([]*note.Note)
|
||||
|
||||
type QueryOptions struct {
|
||||
SearchTerm string
|
||||
SortBy string
|
||||
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 (s *NotesService) SetStorage(storage storage.Storage) {
|
||||
s.storage = storage
|
||||
}
|
||||
|
||||
func SortByTitle(notes []*note.Note) {
|
||||
sort.Slice(notes, func(i, j int) bool {
|
||||
return notes[i].Title < notes[j].Title
|
||||
})
|
||||
func SortByDate(notes []*note.Note) {
|
||||
sort.Slice(notes, func(i, j int) bool {
|
||||
return notes[i].UpdatedAt.After(notes[j].UpdatedAt)
|
||||
})
|
||||
}
|
||||
|
||||
func SortByDateAsc(notes []*note.Note) {
|
||||
sort.Slice(notes, func(i, j int) bool {
|
||||
return notes[i].UpdatedAt.Before(notes[j].UpdatedAt)
|
||||
})
|
||||
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 SortByTitle(notes []*note.Note) {
|
||||
sort.Slice(notes, func(i, j int) bool {
|
||||
return notes[i].Title < notes[j].Title
|
||||
})
|
||||
}
|
||||
|
||||
func SortByTitleAsc(notes []*note.Note) {
|
||||
sort.Slice(notes, func(i, j int) bool {
|
||||
return notes[i].Title > notes[j].Title
|
||||
})
|
||||
}
|
||||
|
||||
func (s *NotesService) GetNotesWithSort(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
|
||||
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
|
||||
}
|
||||
|
||||
func (s *NotesService) GetNoteByHash(hash string) (*note.Note, error) {
|
||||
return s.storage.Get(hash)
|
||||
}
|
||||
|
||||
func (s *NotesService) GetNotes() []*note.Note {
|
||||
return s.storage.GetAll()
|
||||
}
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"donniemarko/internal/note"
|
||||
@ -14,56 +13,36 @@ func TestMain(m *testing.M) {
|
||||
storage := storage.NewNoteStorage()
|
||||
|
||||
notes := []*note.Note{
|
||||
{Title: "Golang Tutorial", Content: "Learn Go"},
|
||||
{Title: "Rust Guide", Content: "Learn Rust"},
|
||||
{ID: "test1", Title: "Golang Tutorial", Content: "Learn Go"},
|
||||
{ID: "test2", Title: "Rust Guide", Content: "Learn Rust"},
|
||||
}
|
||||
for _, note := range notes {
|
||||
storage.Create(note)
|
||||
}
|
||||
|
||||
service = NewNoteService()
|
||||
service.storage = storage
|
||||
service.SetStorage(storage)
|
||||
m.Run()
|
||||
}
|
||||
|
||||
func TestQueryNotes_WithSearch(t *testing.T) {
|
||||
|
||||
opts := QueryOptions{
|
||||
SearchTerm: "golang",
|
||||
SortBy: "alpha",
|
||||
}
|
||||
|
||||
results, err := service.QueryNotes(opts)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(results) != 1 {
|
||||
t.Errorf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
|
||||
if results[0].Title != "Golang Tutorial" {
|
||||
t.Error("wrong note returned")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandler_BuildViewState(t *testing.T) {
|
||||
handler := NewHandler(service, nil)
|
||||
|
||||
req := httptest.NewRequest("GET", "/?search=test&sort=alpha", nil)
|
||||
|
||||
state, err := handler.buildViewState(req)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if state.SearchTerm != "test" {
|
||||
t.Error("search term not extracted")
|
||||
}
|
||||
|
||||
if state.SortBy != "alpha" {
|
||||
t.Error("sort option not extracted")
|
||||
}
|
||||
opts := QueryOptions{
|
||||
SearchTerm: "Go",
|
||||
SortBy: "alpha",
|
||||
}
|
||||
|
||||
results, err := service.QueryNotes(opts)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(results) != 1 {
|
||||
t.Errorf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
|
||||
if results[0].Title != "Golang Tutorial" {
|
||||
t.Error("wrong note returned")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user