-
Notifications
You must be signed in to change notification settings - Fork 1
/
interfaces.go
118 lines (99 loc) · 2.7 KB
/
interfaces.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package deferr
import (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/julienschmidt/httprouter"
"net/http"
)
////////////////////////////////////////////////////////////////////////////
// Repositories
////////////////////////////////////////////////////////////////////////////
type Storage interface {
Query() []interface{}
Push(i interface{}) error
Pop() (interface{}, error)
Defer() error
}
type TodoRepo struct {
Store Storage
}
func NewTodoRepo(store Storage) *TodoRepo {
return &TodoRepo{Store: store}
}
func (tr *TodoRepo) List() []*Todo {
items := tr.Store.Query()
todos := make([]*Todo, len(items))
for i, item := range items {
todos[i] = item.(*Todo)
}
return todos
}
func (tr *TodoRepo) Push(t *Todo) error {
return tr.Store.Push(t)
}
func (tr *TodoRepo) Pop() (*Todo, error) {
t, err := tr.Store.Pop()
if err != nil {
return nil, err
}
return t.(*Todo), nil
}
func (tr *TodoRepo) Defer() error {
return tr.Store.Defer()
}
////////////////////////////////////////////////////////////////////////////
// Web handlers
////////////////////////////////////////////////////////////////////////////
type TodoInteractor interface {
List() []*Todo
Push(t *Todo) error
Pop() (*Todo, error)
Defer() error
}
type WebHandler struct {
todoManager TodoInteractor
}
func NewWebHandler(todoManager TodoInteractor) *WebHandler {
return &WebHandler{todoManager: todoManager}
}
func (wh *WebHandler) List(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
todos := wh.todoManager.List()
b, _ := json.Marshal(todos)
fmt.Fprint(w, string(b))
}
func (wh *WebHandler) Push(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
var t Todo
var result interface{}
body, _ := ioutil.ReadAll(r.Body)
if err := json.Unmarshal(body, &t); err != nil {
result = map[string]string{"message": "Invalid payload."}
} else {
wh.todoManager.Push(&t)
result = t
}
b, _ := json.Marshal(result)
fmt.Fprint(w, string(b))
}
func (wh *WebHandler) Pop(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
var result interface{}
result, err := wh.todoManager.Pop()
if err != nil {
result = map[string]string{"message": err.Error()}
}
b, _ := json.Marshal(result)
fmt.Fprint(w, string(b))
}
func (wh *WebHandler) Defer(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
err := wh.todoManager.Defer()
msg := "You have successfully procastinated."
if err != nil {
msg = err.Error()
}
b, _ := json.Marshal(map[string]string{"message": msg})
fmt.Fprint(w, string(b))
}