-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorganisation_route.go
160 lines (149 loc) · 4.35 KB
/
organisation_route.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
149
150
151
152
153
154
155
156
157
158
159
160
package api
import (
"context"
"net/http"
"strconv"
"github.com/pressly/chi"
chiRender "github.com/pressly/chi/render"
"github.com/titouanfreville/popcubeapi/datastores"
"github.com/titouanfreville/popcubeapi/models"
)
const (
oldOrganisationKey key = "oldOrganisation"
)
func initOrganisationRoute(router chi.Router) {
router.Route("/organisation", func(r chi.Router) {
r.Use(tokenAuth.Verifier)
r.Use(Authenticator)
// swagger:route GET /organisation Organisations getAllOrganisation
//
// Get organisations
//
// This will get all the organisations available in the organisation.
//
// Responses:
// 200: organisationObjectSuccess
// 503: databaseError
// default: genericError
r.Get("/", getAllOrganisation)
// swagger:route POST /organisation Organisations newOrganisation
//
// New organisation
//
// This will create an organisation for organisation organisations library.
//
// Responses:
// 201: organisationObjectSuccess
// 422: wrongEntity
// 503: databaseError
// default: genericError
r.Post("/", newOrganisation)
// swagger:route GET /organisation/all Organisations getAllOrganisation1
//
// Get organisations
//
// This will get all the organisations available in the organisation.
//
// Responses:
// 200: organisationObjectSuccess
// 503: databaseError
// default: genericError
r.Get("/all", getAllOrganisation)
// swagger:route POST /organisation/new Organisations newOrganisation1
//
// New organisation
//
// This will create an organisation for organisation organisations library.
//
// Responses:
// 201: organisationObjectSuccess
// 422: wrongEntity
// 503: databaseError
// default: genericError
r.Post("/new", newOrganisation)
r.Route("/:organisationID", func(r chi.Router) {
r.Use(organisationContext)
// swagger:route PUT /organisation/{organisationID} Organisations updateOrganisation
//
// Get organisation from link
//
// This will return the organisation object corresponding to provided link
//
// Responses:
// 200: organisationObjectSuccess
// 503: databaseError
// default: genericError
r.Put("/update", updateOrganisation)
})
})
}
func organisationContext(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := strconv.ParseUint(chi.URLParam(r, "organisationID"), 10, 64)
oldOrganisation := models.Organisation{}
if err == nil {
oldOrganisation = datastores.Store().Organisation().Get(dbStore.db)
}
ctx := context.WithValue(r.Context(), oldOrganisationKey, oldOrganisation)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func getAllOrganisation(w http.ResponseWriter, r *http.Request) {
store := datastores.Store()
db := dbStore.db
if err := db.DB().Ping(); err == nil {
result := store.Organisation().Get(db)
render.JSON(w, 200, result)
} else {
render.JSON(w, error503.StatusCode, error503)
}
}
func newOrganisation(w http.ResponseWriter, r *http.Request) {
var data struct {
Organisation *models.Organisation
OmitID interface{} `json:"id,omitempty"`
}
store := datastores.Store()
db := dbStore.db
request := r.Body
err := chiRender.Bind(request, &data)
if err != nil || data.Organisation == nil {
render.JSON(w, error422.StatusCode, error422)
} else {
if err := db.DB().Ping(); err == nil {
err := store.Organisation().Save(data.Organisation, db)
if err == nil {
render.JSON(w, 201, data.Organisation)
} else {
render.JSON(w, err.StatusCode, err)
}
} else {
render.JSON(w, error503.StatusCode, error503)
}
}
}
func updateOrganisation(w http.ResponseWriter, r *http.Request) {
var data struct {
Organisation *models.Organisation
OmitID interface{} `json:"id,omitempty"`
}
store := datastores.Store()
db := dbStore.db
request := r.Body
err := chiRender.Bind(request, &data)
organisation := r.Context().Value(oldOrganisationKey).(models.Organisation)
if err != nil || data.Organisation == nil {
render.JSON(w, error422.StatusCode, error422)
} else {
if err := db.DB().Ping(); err == nil {
err := store.Organisation().Update(&organisation, data.Organisation, db)
if err == nil {
render.JSON(w, 200, organisation)
} else {
render.JSON(w, err.StatusCode, err)
}
} else {
render.JSON(w, error503.StatusCode, error503)
}
}
}