-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
241 lines (227 loc) · 7.19 KB
/
controller.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package node
import (
"encoding/json"
"net/http"
"github.com/go-chi/chi"
"github.com/mrexmelle/connect-org/internal/config"
"github.com/mrexmelle/connect-org/internal/designation"
"github.com/mrexmelle/connect-org/internal/dto/dtorespwithdata"
"github.com/mrexmelle/connect-org/internal/dto/dtorespwithoutdata"
"github.com/mrexmelle/connect-org/internal/localerror"
"github.com/mrexmelle/connect-org/internal/membership"
)
type Controller struct {
ConfigService *config.Service
LocalErrorService *localerror.Service
NodeService *Service
DesignationService *designation.Service
MembershipService *membership.Service
}
func NewController(
cfg *config.Service,
les *localerror.Service,
svc *Service,
ds *designation.Service,
ms *membership.Service,
) *Controller {
return &Controller{
ConfigService: cfg,
LocalErrorService: les,
NodeService: svc,
DesignationService: ds,
MembershipService: ms,
}
}
// Get Nodes : HTTP endpoint to get a node
// @Tags Nodes
// @Description Get a node
// @Produce json
// @Param id path string true "Node ID"
// @Success 200 {object} GetResponseDto "Success Response"
// @Failure 400 "BadRequest"
// @Failure 500 "InternalServerError"
// @Router /nodes/{id} [GET]
func (c *Controller) Get(w http.ResponseWriter, r *http.Request) {
data, err := c.NodeService.RetrieveById(
chi.URLParam(r, "id"),
)
info := c.LocalErrorService.Map(err)
dtorespwithdata.New(
data,
info.ServiceErrorCode,
info.ServiceErrorMessage,
).RenderTo(w, info.HttpStatusCode)
}
// Post Nodes : HTTP endpoint to post new nodes
// @Tags Nodes
// @Description Post a new node
// @Accept json
// @Produce json
// @Param data body PostRequestDto true "Node Request"
// @Success 200 {object} PostResponseDto "Success Response"
// @Failure 400 "BadRequest"
// @Failure 500 "InternalServerError"
// @Router /nodes [POST]
func (c *Controller) Post(w http.ResponseWriter, r *http.Request) {
var requestBody PostRequestDto
err := json.NewDecoder(r.Body).Decode(&requestBody)
if err != nil {
dtorespwithdata.NewError(
localerror.ErrBadJson.Error(),
err.Error(),
).RenderTo(w, http.StatusBadRequest)
return
}
data, err := c.NodeService.Create(requestBody)
info := c.LocalErrorService.Map(err)
dtorespwithdata.New(
data,
info.ServiceErrorCode,
info.ServiceErrorMessage,
).RenderTo(w, info.HttpStatusCode)
}
// Patch Nodes : HTTP endpoint to patch a node
// @Tags Nodes
// @Description Patch a node
// @Accept json
// @Produce json
// @Param id path string true "Node ID"
// @Param data body PatchRequestDto true "Node Patch Request"
// @Success 200 {object} PatchResponseDto "Success Response"
// @Failure 400 "BadRequest"
// @Failure 500 "InternalServerError"
// @Router /nodes/{id} [PATCH]
func (c *Controller) Patch(w http.ResponseWriter, r *http.Request) {
var requestBody PatchRequestDto
err := json.NewDecoder(r.Body).Decode(&requestBody)
if err != nil {
dtorespwithoutdata.New(
localerror.ErrBadJson.Error(),
err.Error(),
).RenderTo(w, http.StatusBadRequest)
return
}
err = c.NodeService.UpdateById(requestBody.Fields, chi.URLParam(r, "id"))
info := c.LocalErrorService.Map(err)
dtorespwithoutdata.New(
info.ServiceErrorCode,
info.ServiceErrorMessage,
).RenderTo(w, info.HttpStatusCode)
}
// Delete Nodes : HTTP endpoint to delete nodes
// @Tags Nodes
// @Description Delete a node
// @Produce json
// @Param id path string true "Node ID"
// @Success 200 {object} DeleteResponseDto "Success Response"
// @Failure 400 "BadRequest"
// @Failure 500 "InternalServerError"
// @Router /nodes/{id} [DELETE]
func (c *Controller) Delete(w http.ResponseWriter, r *http.Request) {
err := c.NodeService.DeleteById(chi.URLParam(r, "id"))
info := c.LocalErrorService.Map(err)
dtorespwithoutdata.New(
info.ServiceErrorCode,
info.ServiceErrorMessage,
).RenderTo(w, info.HttpStatusCode)
}
// Get Children of Nodes : HTTP endpoint to get the children of a node
// @Tags Nodes
// @Description Get children of a node
// @Produce json
// @Param id path string true "Node ID"
// @Success 200 {object} GetChildrenResponseDto "Success Response"
// @Failure 400 "BadRequest"
// @Failure 500 "InternalServerError"
// @Router /nodes/{id}/children [GET]
func (c *Controller) GetChildren(w http.ResponseWriter, r *http.Request) {
data, err := c.NodeService.RetrieveChildrenById(
chi.URLParam(r, "id"),
)
info := c.LocalErrorService.Map(err)
dtorespwithdata.New(
&data,
info.ServiceErrorCode,
info.ServiceErrorMessage,
).RenderTo(w, info.HttpStatusCode)
}
// Get Lineage of Nodes : HTTP endpoint to get the lineage of a node
// @Tags Nodes
// @Description Get lineage of a node
// @Produce json
// @Param id path string true "Node ID"
// @Success 200 {object} GetLineageResponseDto "Success Response"
// @Failure 400 "BadRequest"
// @Failure 500 "InternalServerError"
// @Router /nodes/{id}/lineage [GET]
func (c *Controller) GetLineage(w http.ResponseWriter, r *http.Request) {
data, err := c.NodeService.RetrieveLineageById(
chi.URLParam(r, "id"),
)
info := c.LocalErrorService.Map(err)
dtorespwithdata.New(
data,
info.ServiceErrorCode,
info.ServiceErrorMessage,
).RenderTo(w, info.HttpStatusCode)
}
// Get Lineage Siblings of Nodes : HTTP endpoint to get the siblings and ancestral siblings of a node
// @Tags Nodes
// @Description Get siblings and ancestral siblings of a node
// @Produce json
// @Param id path string true "Node ID"
// @Success 200 {object} GetLineagelSiblingsResponseDto "Success Response"
// @Failure 400 "BadRequest"
// @Failure 500 "InternalServerError"
// @Router /nodes/{id}/lineage-siblings [GET]
func (c *Controller) GetLineageSiblings(w http.ResponseWriter, r *http.Request) {
data, err := c.NodeService.RetrieveLineageSiblingsById(
chi.URLParam(r, "id"),
)
info := c.LocalErrorService.Map(err)
dtorespwithdata.New(
data,
info.ServiceErrorCode,
info.ServiceErrorMessage,
).RenderTo(w, info.HttpStatusCode)
}
// Get Officers within Nodes : HTTP endpoint to get the officers within a node
// @Tags Nodes
// @Description Get officers within a node
// @Produce json
// @Param id path string true "Node ID"
// @Success 200 {object} GetOfficersResponseDto "Success Response"
// @Failure 400 "BadRequest"
// @Failure 500 "InternalServerError"
// @Router /nodes/{id}/officers [GET]
func (c *Controller) GetOfficers(w http.ResponseWriter, r *http.Request) {
data, err := c.DesignationService.RetrieveByNodeId(
chi.URLParam(r, "id"),
)
info := c.LocalErrorService.Map(err)
dtorespwithdata.New(
&data,
info.ServiceErrorCode,
info.ServiceErrorMessage,
).RenderTo(w, info.HttpStatusCode)
}
// Get Members within Nodes : HTTP endpoint to get the current members within a node
// @Tags Nodes
// @Description Get current members within a node
// @Produce json
// @Param id path string true "Node ID"
// @Success 200 {object} GetMembersResponseDto "Success Response"
// @Failure 400 "BadRequest"
// @Failure 500 "InternalServerError"
// @Router /nodes/{id}/members [GET]
func (c *Controller) GetMembers(w http.ResponseWriter, r *http.Request) {
data, err := c.MembershipService.RetrieveCurrentByNodeId(
chi.URLParam(r, "id"),
)
info := c.LocalErrorService.Map(err)
dtorespwithdata.New(
&data,
info.ServiceErrorCode,
info.ServiceErrorMessage,
).RenderTo(w, info.HttpStatusCode)
}