wip
This commit is contained in:
parent
9f7944274f
commit
9af596fb29
|
@ -13,7 +13,7 @@ func Index(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprint(w, `{"status":"ok"}`)
|
fmt.Fprint(w, `{"status":"ok"}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSyncHandler(mem storage.Repository) func(w http.ResponseWriter, r *http.Request) {
|
func NewSyncHandler(mem storage.Syncer) func(w http.ResponseWriter, r *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
timestamp := time.Time{}
|
timestamp := time.Time{}
|
||||||
tsStr := r.URL.Query().Get("ts")
|
tsStr := r.URL.Query().Get("ts")
|
||||||
|
@ -25,7 +25,7 @@ func NewSyncHandler(mem storage.Repository) func(w http.ResponseWriter, r *http.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
items, err := mem.NewSince(timestamp)
|
items, err := mem.Updated(timestamp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -39,5 +39,10 @@ func NewSyncHandler(mem storage.Repository) func(w http.ResponseWriter, r *http.
|
||||||
|
|
||||||
fmt.Fprint(w, string(body))
|
fmt.Fprint(w, string(body))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewStoreHandler(mem storage.Syncer) func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,21 +6,34 @@ import (
|
||||||
"code.ewintr.nl/planner/planner"
|
"code.ewintr.nl/planner/planner"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type deletedItem struct {
|
||||||
|
ID string
|
||||||
|
Timestamp time.Time
|
||||||
|
}
|
||||||
|
|
||||||
type Memory struct {
|
type Memory struct {
|
||||||
items map[string]planner.Syncable
|
items map[string]planner.Syncable
|
||||||
|
deleted []deletedItem
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMemory() *Memory {
|
func NewMemory() *Memory {
|
||||||
return &Memory{
|
return &Memory{
|
||||||
items: make(map[string]planner.Syncable),
|
items: make(map[string]planner.Syncable),
|
||||||
|
deleted: make([]deletedItem, 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Memory) NewSince(timestamp time.Time) ([]planner.Syncable, error) {
|
func (m *Memory) Update(item planner.Syncable) error {
|
||||||
|
m.items[item.ID()] = item
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Memory) Updated(timestamp time.Time) ([]planner.Syncable, error) {
|
||||||
result := make([]planner.Syncable, 0)
|
result := make([]planner.Syncable, 0)
|
||||||
|
|
||||||
for _, i := range m.items {
|
for _, i := range m.items {
|
||||||
if timestamp.IsZero() || i.Updated().After(timestamp) {
|
if timestamp.IsZero() || i.Updated().Equal(timestamp) || i.Updated().After(timestamp) {
|
||||||
result = append(result, i)
|
result = append(result, i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,35 +41,26 @@ func (m *Memory) NewSince(timestamp time.Time) ([]planner.Syncable, error) {
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Memory) Store(item planner.Syncable) error {
|
func (m *Memory) Delete(id string) error {
|
||||||
m.items[item.ID()] = item
|
if _, exists := m.items[id]; !exists {
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
func (m *Memory) RemoveProject(id string) error {
|
|
||||||
if _, ok := m.items[id]; !ok {
|
|
||||||
return ErrNotFound
|
return ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(m.items, id)
|
delete(m.items, id)
|
||||||
|
m.deleted = append(m.deleted, deletedItem{
|
||||||
|
ID: id,
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
})
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Memory) FindProject(id string) (Project, error) {
|
func (m *Memory) Deleted(t time.Time) ([]string, error) {
|
||||||
project, ok := m.items[id]
|
result := make([]string, 0)
|
||||||
if !ok {
|
for _, di := range m.deleted {
|
||||||
return Project{}, ErrNotFound
|
if di.Timestamp.Equal(t) || di.Timestamp.After(t) {
|
||||||
|
result = append(result, di.ID)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return project, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Memory) FindAllProjects() ([]Project, error) {
|
|
||||||
items := make([]Project, 0, len(m.items))
|
|
||||||
for _, p := range m.items {
|
|
||||||
items = append(items, p)
|
|
||||||
}
|
|
||||||
return items, nil
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ func TestMemoryItem(t *testing.T) {
|
||||||
mem := storage.NewMemory()
|
mem := storage.NewMemory()
|
||||||
|
|
||||||
t.Log("start empty")
|
t.Log("start empty")
|
||||||
actItems, actErr := mem.NewSince(time.Time{})
|
actItems, actErr := mem.Updated(time.Time{})
|
||||||
if actErr != nil {
|
if actErr != nil {
|
||||||
t.Errorf("exp nil, got %v", actErr)
|
t.Errorf("exp nil, got %v", actErr)
|
||||||
}
|
}
|
||||||
|
@ -24,10 +24,10 @@ func TestMemoryItem(t *testing.T) {
|
||||||
|
|
||||||
t.Log("add one")
|
t.Log("add one")
|
||||||
t1 := planner.NewTask("test")
|
t1 := planner.NewTask("test")
|
||||||
if actErr := mem.Store(t1); actErr != nil {
|
if actErr := mem.Update(t1); actErr != nil {
|
||||||
t.Errorf("exp nil, got %v", actErr)
|
t.Errorf("exp nil, got %v", actErr)
|
||||||
}
|
}
|
||||||
actItems, actErr = mem.NewSince(time.Time{})
|
actItems, actErr = mem.Updated(time.Time{})
|
||||||
if actErr != nil {
|
if actErr != nil {
|
||||||
t.Errorf("exp nil, got %v", actErr)
|
t.Errorf("exp nil, got %v", actErr)
|
||||||
}
|
}
|
||||||
|
@ -42,10 +42,10 @@ func TestMemoryItem(t *testing.T) {
|
||||||
|
|
||||||
t.Log("add second")
|
t.Log("add second")
|
||||||
t2 := planner.NewTask("test 2")
|
t2 := planner.NewTask("test 2")
|
||||||
if actErr := mem.Store(t2); actErr != nil {
|
if actErr := mem.Update(t2); actErr != nil {
|
||||||
t.Errorf("exp nil, got %v", actErr)
|
t.Errorf("exp nil, got %v", actErr)
|
||||||
}
|
}
|
||||||
actItems, actErr = mem.NewSince(time.Time{})
|
actItems, actErr = mem.Updated(time.Time{})
|
||||||
if actErr != nil {
|
if actErr != nil {
|
||||||
t.Errorf("exp nil, got %v", actErr)
|
t.Errorf("exp nil, got %v", actErr)
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ func TestMemoryItem(t *testing.T) {
|
||||||
t.Errorf("exp %v, got %v", actItems[1].ID(), t2.ID())
|
t.Errorf("exp %v, got %v", actItems[1].ID(), t2.ID())
|
||||||
}
|
}
|
||||||
|
|
||||||
actItems, actErr = mem.NewSince(before)
|
actItems, actErr = mem.Updated(before)
|
||||||
if actErr != nil {
|
if actErr != nil {
|
||||||
t.Errorf("exp nil, got %v", actErr)
|
t.Errorf("exp nil, got %v", actErr)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,10 +11,9 @@ var (
|
||||||
ErrNotFound = errors.New("not found")
|
ErrNotFound = errors.New("not found")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Repository interface {
|
type Syncer interface {
|
||||||
NewSince(t time.Time) ([]planner.Syncable, error)
|
Update(item planner.Syncable) error
|
||||||
Store(item planner.Syncable) error
|
Updated(t time.Time) ([]planner.Syncable, error)
|
||||||
// FindTask(id string) (planner.Task, error)
|
Delete(id string) error
|
||||||
// FindAllTasks() ([]planner.Task, error)
|
Deleted(t time.Time) ([]string, error)
|
||||||
// StoreTask(project planner.Task) error
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue