// Create creates a new note.
func (s *Service) Create(ctx context.Context, in *CreateInput) (*Note, error) {
if in.Title == "" {
return nil, fmt.Errorf("title is required")
}
s.mu.Lock()
defer s.mu.Unlock()
s.nextID++
now := time.Now()
note := &Note{
ID: fmt.Sprintf("%d", s.nextID),
Title: in.Title,
Content: in.Content,
CreatedAt: now,
UpdatedAt: now,
}
s.notes[note.ID] = note
return note, nil
}
// List returns all notes.
func (s *Service) List(ctx context.Context) ([]*Note, error) {
s.mu.RLock()
defer s.mu.RUnlock()
notes := make([]*Note, 0, len(s.notes))
for _, n := range s.notes {
notes = append(notes, n)
}
return notes, nil
}
// Get returns a note by ID.
func (s *Service) Get(ctx context.Context, in *GetInput) (*Note, error) {
s.mu.RLock()
defer s.mu.RUnlock()
note, ok := s.notes[in.ID]
if !ok {
return nil, fmt.Errorf("note not found: %s", in.ID)
}
return note, nil
}
// Update updates a note.
func (s *Service) Update(ctx context.Context, in *UpdateInput) (*Note, error) {
s.mu.Lock()
defer s.mu.Unlock()
note, ok := s.notes[in.ID]
if !ok {
return nil, fmt.Errorf("note not found: %s", in.ID)
}
if in.Title != nil {
note.Title = *in.Title
}
if in.Content != nil {
note.Content = *in.Content
}
note.UpdatedAt = time.Now()
return note, nil
}
// Delete deletes a note.
func (s *Service) Delete(ctx context.Context, in *DeleteInput) (*DeleteOutput, error) {
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.notes[in.ID]; !ok {
return nil, fmt.Errorf("note not found: %s", in.ID)
}
delete(s.notes, in.ID)
return &DeleteOutput{Success: true}, nil
}