-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.go
137 lines (118 loc) · 3.29 KB
/
rest.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"net/http"
"web-widgets/todo-go/data"
"github.com/go-chi/chi"
)
func initRoutes(r chi.Router, dao *data.DAO) {
r.Get("/tasks", func(w http.ResponseWriter, r *http.Request) {
data, err := dao.Tasks.GetAll()
sendResponse(w, data, err)
})
r.Get("/tasks/projects/{id}", func(w http.ResponseWriter, r *http.Request) {
id := numberParam(r, "id")
data, err := dao.Tasks.GetFromProject(id)
sendResponse(w, data, err)
})
r.Get("/users", func(w http.ResponseWriter, r *http.Request) {
data, err := dao.Users.GetAll()
sendResponse(w, data, err)
})
r.Get("/projects", func(w http.ResponseWriter, r *http.Request) {
data, err := dao.Projects.GetAll()
sendResponse(w, data, err)
})
r.Post("/tasks", func(w http.ResponseWriter, r *http.Request) {
task := data.TaskUpdate{}
err := parseFormObject(w, r, &task)
if err != nil {
format.Text(w, 500, err.Error())
return
}
id, err := dao.Tasks.Add(&task)
sendResponse(w, &Response{id}, err)
})
r.Put("/tasks/{id}", func(w http.ResponseWriter, r *http.Request) {
task := data.TaskUpdate{}
err := parseFormObject(w, r, &task)
id := numberParam(r, "id")
if err == nil {
err = dao.Tasks.Update(id, &task)
}
sendResponse(w, nil, err)
})
r.Delete("/tasks/{id}", func(w http.ResponseWriter, r *http.Request) {
id := numberParam(r, "id")
err := dao.Tasks.Delete(id)
sendResponse(w, nil, err)
})
r.Post("/projects", func(w http.ResponseWriter, r *http.Request) {
project := data.ProjectUpdate{}
err := parseFormObject(w, r, &project)
var id int
if err == nil {
id, err = dao.Projects.Add(&project)
}
sendResponse(w, &Response{id}, err)
})
r.Put("/projects/{id}", func(w http.ResponseWriter, r *http.Request) {
id := numberParam(r, "id")
project := data.ProjectUpdate{}
err := parseFormObject(w, r, &project)
if err == nil {
err = dao.Projects.Update(id, &project)
}
sendResponse(w, nil, err)
})
r.Delete("/projects/{id}", func(w http.ResponseWriter, r *http.Request) {
id := numberParam(r, "id")
err := dao.Projects.Delete(id)
sendResponse(w, nil, err)
})
r.Put("/move/{id}", func(w http.ResponseWriter, r *http.Request) {
id := numberParam(r, "id")
info := data.MoveInfo{}
err := parseFormObject(w, r, &info)
if err == nil {
err = dao.Tasks.MoveToProject(id, &info)
}
sendResponse(w, nil, err)
})
r.Put("/indent/{id}", func(w http.ResponseWriter, r *http.Request) {
id := numberParam(r, "id")
info := data.ShiftInfo{}
err := parseFormObject(w, r, &info)
if err == nil {
err = dao.Tasks.Indent(id, &info)
}
sendResponse(w, nil, err)
})
r.Put("/unindent/{id}", func(w http.ResponseWriter, r *http.Request) {
id := numberParam(r, "id")
info := data.ShiftInfo{}
err := parseFormObject(w, r, &info)
if err == nil {
err = dao.Tasks.Unindent(id, &info)
}
sendResponse(w, nil, err)
})
r.Post("/clone", func(w http.ResponseWriter, r *http.Request) {
info := data.PasteInfo{}
err := parseFormObject(w, r, &info)
var pull map[int]int
if err == nil {
pull, err = dao.Tasks.Paste(&info)
}
sendResponse(w, pull, err)
})
}
func sendResponse(w http.ResponseWriter, data interface{}, err error) {
if err != nil {
format.Text(w, 500, err.Error())
} else {
if data == nil {
data = Response{}
}
format.JSON(w, 200, data)
}
}