wip
This commit is contained in:
parent
a1e72c0400
commit
45004829c7
|
@ -0,0 +1,33 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"code.ewintr.nl/planner/storage"
|
||||
)
|
||||
|
||||
func Index(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprint(w, `{"status":"ok"}`)
|
||||
}
|
||||
|
||||
func NewSyncHandler(mem storage.Repository) func(w http.ResponseWriter, r *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
token := r.URL.Query().Get("token")
|
||||
items, err := mem.NewSince(token)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
body, err := json.Marshal(items)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprint(w, body)
|
||||
}
|
||||
|
||||
}
|
15
main.go
15
main.go
|
@ -1,6 +1,17 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"code.ewintr.nl/planner/handler"
|
||||
"code.ewintr.nl/planner/storage"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// td := NewTodoist(os.Getenv("TODOIST_API_TOKEN"), "https://api.todoist.com")
|
||||
// td.Run()
|
||||
mem := storage.NewMemory()
|
||||
|
||||
http.HandleFunc("/", handler.Index)
|
||||
http.HandleFunc("/sync", handler.NewSyncHandler(mem))
|
||||
|
||||
http.ListenAndServer(":8092", nil)
|
||||
}
|
||||
|
|
|
@ -7,19 +7,28 @@ import (
|
|||
)
|
||||
|
||||
type Syncable interface {
|
||||
LastUpdated() time.Time
|
||||
ID() string
|
||||
Updated() time.Time
|
||||
}
|
||||
|
||||
type Task struct {
|
||||
ID string
|
||||
id string
|
||||
description string
|
||||
updated time.Time
|
||||
}
|
||||
|
||||
func NewTask(description string) Task {
|
||||
return Task{
|
||||
ID: uuid.New(),
|
||||
id: uuid.New().String(),
|
||||
description: description,
|
||||
updated: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Task) ID() string {
|
||||
return t.id
|
||||
}
|
||||
|
||||
func (t *Task) Updated() time.Time {
|
||||
return t.updated
|
||||
}
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
package service
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("not found")
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
FindProject(id string) (Project, error)
|
||||
FindAllProjects() ([]Project, error)
|
||||
StoreProject(project Project) error
|
||||
}
|
|
@ -3,32 +3,33 @@ package storage
|
|||
import "code.ewintr.nl/planner/planner"
|
||||
|
||||
type Memory struct {
|
||||
projects map[string]planner.Task
|
||||
items map[string]planner.Syncable
|
||||
}
|
||||
|
||||
func NewMemory() *Memory {
|
||||
return &Memory{
|
||||
projects: make(map[string]planner.Task),
|
||||
items: make(map[string]planner.Syncable),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Memory) StoreProject(project planner.Task) error {
|
||||
m.projects[project.ID] = project
|
||||
func (m *Memory) StoreProject(item planner.Syncable) error {
|
||||
m.items[item.ID()] = item
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
func (m *Memory) RemoveProject(id string) error {
|
||||
if _, ok := m.projects[id]; !ok {
|
||||
if _, ok := m.items[id]; !ok {
|
||||
return ErrNotFound
|
||||
}
|
||||
delete(m.projects, id)
|
||||
delete(m.items, id)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Memory) FindProject(id string) (Project, error) {
|
||||
project, ok := m.projects[id]
|
||||
project, ok := m.items[id]
|
||||
if !ok {
|
||||
return Project{}, ErrNotFound
|
||||
}
|
||||
|
@ -36,9 +37,10 @@ func (m *Memory) FindProject(id string) (Project, error) {
|
|||
}
|
||||
|
||||
func (m *Memory) FindAllProjects() ([]Project, error) {
|
||||
projects := make([]Project, 0, len(m.projects))
|
||||
for _, p := range m.projects {
|
||||
projects = append(projects, p)
|
||||
items := make([]Project, 0, len(m.items))
|
||||
for _, p := range m.items {
|
||||
items = append(items, p)
|
||||
}
|
||||
return projects, nil
|
||||
return items, nil
|
||||
}
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"code.ewintr.nl/planner/planner"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("not found")
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
NewSince(t time.Time) ([]planner.Syncable, error)
|
||||
Store(item planner.Syncable) error
|
||||
// FindTask(id string) (planner.Task, error)
|
||||
// FindAllTasks() ([]planner.Task, error)
|
||||
// StoreTask(project planner.Task) error
|
||||
}
|
Loading…
Reference in New Issue