This repository has been archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
section.go
62 lines (51 loc) · 1.64 KB
/
section.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
package http
import (
"net/http"
"github.com/18F/e-QIP-prototype/api"
)
// SectionHandler is the handler for returning section information.
type SectionHandler struct {
Env api.Settings
Log api.LogService
Token api.TokenService
Database api.DatabaseService
}
// ServeHTTP returns data for one section of the application.
func (service SectionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Get account ID
id := AccountIDFromRequestContext(r)
// Get the account information from the data store
account := &api.Account{ID: id}
if _, err := account.Get(service.Database, id); err != nil {
service.Log.WarnError(api.NoAccount, err, api.LogFields{})
RespondWithStructuredError(w, api.NoAccount, http.StatusUnauthorized)
return
}
// If the account is locked then we cannot proceed
if account.Locked {
service.Log.Warn(api.AccountLocked, api.LogFields{})
RespondWithStructuredError(w, api.AccountLocked, http.StatusForbidden)
return
}
payloadType := r.FormValue("type")
if payloadType == "" {
service.Log.WarnError(api.PayloadMissingType, nil, api.LogFields{})
RespondWithStructuredError(w, api.PayloadMissingType, http.StatusBadRequest)
return
}
payload := &api.Payload{
Type: payloadType,
}
entity, err := payload.Entity()
if err != nil {
service.Log.WarnError(api.PayloadEntityError, err, api.LogFields{})
RespondWithStructuredError(w, api.PayloadEntityError, http.StatusBadRequest)
return
}
if _, err = entity.Get(service.Database, account.ID); err != nil {
service.Log.WarnError(api.EntityError, err, api.LogFields{})
EncodeJSON(w, `{}`)
return
}
EncodeJSON(w, entity.Marshal())
}