rename syncable

This commit is contained in:
Erik Winter 2024-09-09 07:36:05 +02:00
parent 42fb906dad
commit 32bed5acc8
5 changed files with 20 additions and 20 deletions

View File

@ -85,7 +85,7 @@ func (s *Server) SyncPost(w http.ResponseWriter, r *http.Request) {
} }
defer r.Body.Close() defer r.Body.Close()
var items []Syncable var items []Item
if err := json.Unmarshal(body, &items); err != nil { if err := json.Unmarshal(body, &items); err != nil {
http.Error(w, fmtError(err), http.StatusBadRequest) http.Error(w, fmtError(err), http.StatusBadRequest)
return return

View File

@ -5,23 +5,23 @@ import (
) )
type Memory struct { type Memory struct {
items map[string]Syncable items map[string]Item
} }
func NewMemory() *Memory { func NewMemory() *Memory {
return &Memory{ return &Memory{
items: make(map[string]Syncable), items: make(map[string]Item),
} }
} }
func (m *Memory) Update(item Syncable) error { func (m *Memory) Update(item Item) error {
m.items[item.ID] = item m.items[item.ID] = item
return nil return nil
} }
func (m *Memory) Updated(timestamp time.Time) ([]Syncable, error) { func (m *Memory) Updated(timestamp time.Time) ([]Item, error) {
result := make([]Syncable, 0) result := make([]Item, 0)
for _, i := range m.items { for _, i := range m.items {
if timestamp.IsZero() || i.Updated.Equal(timestamp) || i.Updated.After(timestamp) { if timestamp.IsZero() || i.Updated.Equal(timestamp) || i.Updated.After(timestamp) {

View File

@ -12,19 +12,19 @@ const (
KindTask Kind = "task" KindTask Kind = "task"
) )
type Syncable struct { type Item struct {
ID string `json:"id"` ID string `json:"id"`
Kind Kind `json:"kind"` Kind Kind `json:"kind"`
Updated time.Time `json:"updated"` Updated time.Time `json:"updated"`
Deleted bool `json:"deleted"` Deleted bool `json:"deleted"`
Item string `json:"item"` Body string `json:"body"`
} }
func NewSyncable(item string) Syncable { func NewItem(body string) Item {
return Syncable{ return Item{
ID: uuid.New().String(), ID: uuid.New().String(),
Updated: time.Now(), Updated: time.Now(),
Item: item, Body: body,
} }
} }

View File

@ -48,7 +48,7 @@ func NewSqlite(dbPath string) (*Sqlite, error) {
return s, nil return s, nil
} }
func (s *Sqlite) Update(item Syncable) error { func (s *Sqlite) Update(item Item) error {
if _, err := s.db.Exec(` if _, err := s.db.Exec(`
INSERT INTO items INSERT INTO items
(id, kind, updated, deleted, body) (id, kind, updated, deleted, body)
@ -60,14 +60,14 @@ kind=?,
updated=?, updated=?,
deleted=?, deleted=?,
body=?`, body=?`,
item.ID, item.Kind, item.Updated.Format(timestampFormat), item.Deleted, item.Item, item.ID, item.Kind, item.Updated.Format(timestampFormat), item.Deleted, item.Body,
item.Kind, item.Updated.Format(timestampFormat), item.Deleted, item.Item); err != nil { item.Kind, item.Updated.Format(timestampFormat), item.Deleted, item.Body); err != nil {
return fmt.Errorf("%w: %v", ErrSqliteFailure, err) return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
} }
return nil return nil
} }
func (s *Sqlite) Updated(t time.Time) ([]Syncable, error) { func (s *Sqlite) Updated(t time.Time) ([]Item, error) {
rows, err := s.db.Query(` rows, err := s.db.Query(`
SELECT id, kind, updated, deleted, body SELECT id, kind, updated, deleted, body
FROM items FROM items
@ -76,11 +76,11 @@ WHERE updated > ?`, t.Format(timestampFormat))
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err) return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
} }
result := make([]Syncable, 0) result := make([]Item, 0)
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
var item Syncable var item Item
if err := rows.Scan(&item.ID, &item.Kind, &item.Updated, &item.Deleted, &item.Item); err != nil { if err := rows.Scan(&item.ID, &item.Kind, &item.Updated, &item.Deleted, &item.Body); err != nil {
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err) return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
} }
result = append(result, item) result = append(result, item)

View File

@ -10,6 +10,6 @@ var (
) )
type Syncer interface { type Syncer interface {
Update(item Syncable) error Update(item Item) error
Updated(t time.Time) ([]Syncable, error) Updated(t time.Time) ([]Item, error)
} }