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
}

View File

@ -0,0 +1,69 @@
package service
import (
"net/http/httptest"
"testing"
"donniemarko/internal/note"
"donniemarko/internal/storage"
)
var service *NotesService
func TestMain(m *testing.M) {
storage := storage.NewNoteStorage()
notes := []*note.Note{
{Title: "Golang Tutorial", Content: "Learn Go"},
{Title: "Rust Guide", Content: "Learn Rust"},
}
for _, note := range notes {
storage.Create(note)
}
service = NewNoteService()
service.storage = 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")
}
}