/
/
1 package main 2
3 import ( 4 "sync" 5 "time" 6 ) 7
8 type Todo struct { 9 ID string `json:"id"` 10 Text string `json:"text"` 11 Completed bool `json:"completed"` 12 CreatedAt time.Time `json:"createdAt"` 13 Priority string `json:"priority"` 14 } 15
16 // TODO: Replace in-memory store with database persistence 17 type Store struct { 18 mu sync.RWMutex 19 items map[string]Todo 20 } 21
22 func NewStore() *Store { 23 return &Store{items: make(map[string]Todo)} 24 } 25
26 // @gitian:bug All returns items in non-deterministic map iteration order — 27 // the API response order changes between calls 28 func (s *Store) All() []Todo { 29 s.mu.RLock() 30 defer s.mu.RUnlock() 31 out := make([]Todo, 0, len(s.items)) 32 for _, t := range s.items { 33 out = append(out, t) 34 } 35 return out 36 } 37
38 func (s *Store) Find(id string) (Todo, bool) { 39 s.mu.RLock() 40 defer s.mu.RUnlock() 41 t, ok := s.items[id] 42 return t, ok 43 } 44
45 func (s *Store) Insert(t Todo) { 46 s.mu.Lock() 47 defer s.mu.Unlock() 48 s.items[t.ID] = t 49 } 50
51 func (s *Store) Update(t Todo) { 52 s.mu.Lock() 53 defer s.mu.Unlock() 54 s.items[t.ID] = t 55 } 56
57 // FIXME: Should return an error if the ID doesn't exist 58 func (s *Store) Remove(id string) { 59 s.mu.Lock() 60 defer s.mu.Unlock() 61 delete(s.items, id) 62 } 63