package store import ( "context" "fmt" "strings" "sync" ) type MemoryNoteStore struct { mu sync.RWMutex notes map[string]*Note } func NewMemoryNoteStore() *MemoryNoteStore { return &MemoryNoteStore{ notes: make(map[string]*Note), } } func (s *MemoryNoteStore) GetByID(_ context.Context, id string) (*Note, error) { s.mu.RLock() defer s.mu.RUnlock() n, ok := s.notes[id] if !ok { return nil, fmt.Errorf("note not found: %s", id) } return n, nil } func (s *MemoryNoteStore) List(_ context.Context) ([]*Note, error) { s.mu.RLock() defer s.mu.RUnlock() result := make([]*Note, 0, len(s.notes)) for _, n := range s.notes { result = append(result, n) } return result, nil } func (s *MemoryNoteStore) Create(_ context.Context, note *Note) error { s.mu.Lock() defer s.mu.Unlock() if _, exists := s.notes[note.ID]; exists { return fmt.Errorf("note already exists: %s", note.ID) } s.notes[note.ID] = note return nil } func (s *MemoryNoteStore) Update(_ context.Context, note *Note) error { s.mu.Lock() defer s.mu.Unlock() if _, exists := s.notes[note.ID]; !exists { return fmt.Errorf("note not found: %s", note.ID) } s.notes[note.ID] = note return nil } func (s *MemoryNoteStore) Delete(_ context.Context, id string) error { s.mu.Lock() defer s.mu.Unlock() if _, exists := s.notes[id]; !exists { return fmt.Errorf("note not found: %s", id) } delete(s.notes, id) return nil } func (s *MemoryNoteStore) SearchByKeyword(_ context.Context, keyword string) ([]*Note, error) { s.mu.RLock() defer s.mu.RUnlock() keyword = strings.ToLower(keyword) var result []*Note for _, n := range s.notes { if strings.Contains(strings.ToLower(n.Title), keyword) || strings.Contains(strings.ToLower(n.Content), keyword) { result = append(result, n) } } return result, nil }