-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.go
59 lines (52 loc) · 1.52 KB
/
scope.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
package main
import (
"encoding/json"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/serbe/edc"
)
func listScopes(w http.ResponseWriter, r *http.Request) {
type context struct {
Title string `json:"title"`
Scopes []edc.ScopeList `json:"scopes"`
}
scopes, err := db.GetScopeListAll()
if err != nil {
errmsg("listScopes GetScopeListAll", err)
return
}
ctx := context{Title: "List", Scopes: scopes}
render.DefaultResponder(w, r, ctx)
}
func getScope(w http.ResponseWriter, r *http.Request) {
type context struct {
Title string `json:"title"`
Scope edc.Scope `json:"scope"`
}
id := toInt(chi.URLParam(r, "id"))
scope, err := db.GetScope(id)
if err != nil {
errmsg("getScope GetScope", err)
return
}
ctx := context{Title: "Create scope", Scope: scope}
render.DefaultResponder(w, r, ctx)
}
func createScope(_ http.ResponseWriter, r *http.Request) {
var scope edc.Scope
errchkmsg("createScope Decode", json.NewDecoder(r.Body).Decode(&scope))
_, err := db.CreateScope(scope)
errmsg("createScope CreateScope", err)
errchkmsg("createScope CloseBody", r.Body.Close())
}
func updateScope(_ http.ResponseWriter, r *http.Request) {
var scope edc.Scope
errchkmsg("updateScope Decode", json.NewDecoder(r.Body).Decode(&scope))
errmsg("updateScope UpdateScope", db.UpdateScope(scope))
errchkmsg("updateScope CloseBody", r.Body.Close())
}
func deleteScope(_ http.ResponseWriter, r *http.Request) {
id := toInt(chi.URLParam(r, "id"))
errchkmsg("deleteScope", db.DeleteScope(id))
}