-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
schemas.go
148 lines (130 loc) · 3.68 KB
/
schemas.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
138
139
140
141
142
143
144
145
146
147
148
package server
import (
"encoding/json"
"net/http"
"strconv"
"time"
ihttp "github.com/Pigmice2733/peregrine-backend/internal/http"
"github.com/Pigmice2733/peregrine-backend/internal/store"
"github.com/gorilla/mux"
)
func (s *Server) createSchemaHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var schema store.Schema
if err := json.NewDecoder(r.Body).Decode(&schema); err != nil {
ihttp.Error(w, http.StatusUnprocessableEntity)
return
}
roles := ihttp.GetRoles(r)
if schema.Year != nil && !roles.IsSuperAdmin {
ihttp.Error(w, http.StatusForbidden)
return
}
if schema.Year == nil {
realmID, err := ihttp.GetRealmID(r)
if err != nil {
go s.Logger.WithError(err).Error("getting realmID")
ihttp.Error(w, http.StatusInternalServerError)
return
}
schema.RealmID = &realmID
} else {
schema.RealmID = nil
}
err := s.Store.CreateSchema(r.Context(), schema)
if _, ok := err.(store.ErrExists); ok {
ihttp.Respond(w, err, http.StatusConflict)
return
} else if err != nil {
go s.Logger.WithError(err).Error("creating schema")
ihttp.Error(w, http.StatusInternalServerError)
return
}
// If a new schema is created for a specific year, invalidate the events
// route so events (and their schemas) will be updated.
if schema.Year != nil {
expiredUpdate := time.Now().Add(-(expiry + 1) * time.Hour)
s.eventsLastUpdate = &expiredUpdate
}
w.WriteHeader(http.StatusCreated)
}
}
func (s *Server) getSchemasHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if yearQuery := r.URL.Query().Get("year"); yearQuery != "" {
year, err := strconv.Atoi(yearQuery)
if err != nil {
ihttp.Error(w, http.StatusBadRequest)
return
}
schema, err := s.Store.GetSchemaByYear(r.Context(), year)
if _, ok := err.(store.ErrNoResults); ok {
ihttp.Error(w, http.StatusNotFound)
return
} else if err != nil {
go s.Logger.WithError(err).Error("getting schema by year")
ihttp.Error(w, http.StatusInternalServerError)
return
}
ihttp.Respond(w, []store.Schema{schema}, http.StatusOK)
return
}
var schemas []store.Schema
var err error
roles := ihttp.GetRoles(r)
if roles.IsSuperAdmin {
schemas, err = s.Store.GetSchemas(r.Context())
if err != nil {
go s.Logger.WithError(err).Error("getting schemas")
ihttp.Error(w, http.StatusInternalServerError)
return
}
} else {
var realmID *int64
realm, err := ihttp.GetRealmID(r)
if err != nil {
realmID = nil
} else {
realmID = &realm
}
schemas, err = s.Store.GetVisibleSchemas(r.Context(), realmID)
if err != nil {
go s.Logger.WithError(err).Error("getting schemas")
ihttp.Error(w, http.StatusInternalServerError)
return
}
}
ihttp.Respond(w, schemas, http.StatusOK)
}
}
func (s *Server) getSchemaByIDHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(mux.Vars(r)["id"], 10, 64)
if err != nil {
ihttp.Error(w, http.StatusBadRequest)
return
}
schema, err := s.Store.GetSchemaByID(r.Context(), id)
if _, ok := err.(store.ErrNoResults); ok {
ihttp.Error(w, http.StatusNotFound)
return
} else if err != nil {
go s.Logger.WithError(err).Error("getting schema by id")
ihttp.Error(w, http.StatusInternalServerError)
return
}
roles := ihttp.GetRoles(r)
var realmID *int64
realm, err := ihttp.GetRealmID(r)
if err != nil {
realmID = nil
} else {
realmID = &realm
}
if schema.Year == nil && !roles.IsSuperAdmin && schema.RealmID != realmID {
ihttp.Error(w, http.StatusForbidden)
return
}
ihttp.Respond(w, schema, http.StatusOK)
}
}