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()
var items []Syncable
var items []Item
if err := json.Unmarshal(body, &items); err != nil {
http.Error(w, fmtError(err), http.StatusBadRequest)
return

View File

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

View File

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

View File

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

View File

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