-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
123 lines (114 loc) · 3.67 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
package designation
import (
"encoding/json"
"net/http"
"github.com/go-chi/chi"
"github.com/mrexmelle/connect-org/internal/config"
"github.com/mrexmelle/connect-org/internal/dto/dtorespwithdata"
"github.com/mrexmelle/connect-org/internal/dto/dtorespwithoutdata"
"github.com/mrexmelle/connect-org/internal/localerror"
)
type Controller struct {
ConfigService *config.Service
LocalErrorService *localerror.Service
DesignationService *Service
}
func NewController(cfg *config.Service, svc *Service) *Controller {
return &Controller{
ConfigService: cfg,
DesignationService: svc,
}
}
// Get Designations : HTTP endpoint to get designations
// @Tags Designations
// @Description Get a designation
// @Produce json
// @Param id path string true "Designation ID"
// @Success 200 {object} GetResponseDto "Success Response"
// @Failure 400 "BadRequest"
// @Failure 500 "InternalServerError"
// @Router /designations/{id} [GET]
func (c *Controller) Get(w http.ResponseWriter, r *http.Request) {
data, err := c.DesignationService.RetrieveById(
chi.URLParam(r, "id"),
)
info := c.LocalErrorService.Map(err)
dtorespwithdata.New(
data,
info.ServiceErrorCode,
info.ServiceErrorMessage,
).RenderTo(w, info.HttpStatusCode)
}
// Post Designations : HTTP endpoint to post new designations
// @Tags Designations
// @Description Post a new designations
// @Accept json
// @Produce json
// @Param data body PostRequestDto true "Designation Request"
// @Success 200 {object} PostResponseDto "Success Response"
// @Failure 400 "BadRequest"
// @Failure 500 "InternalServerError"
// @Router /designations [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.DesignationService.Create(requestBody)
info := c.LocalErrorService.Map(err)
dtorespwithdata.New(
data,
info.ServiceErrorCode,
info.ServiceErrorMessage,
).RenderTo(w, info.HttpStatusCode)
}
// Patch Designations : HTTP endpoint to patch a designation
// @Tags Designations
// @Description Patch a designation
// @Accept json
// @Produce json
// @Param id path string true "Designation ID"
// @Param data body PatchRequestDto true "Designation Patch Request"
// @Success 200 {object} PatchResponseDto "Success Response"
// @Failure 400 "BadRequest"
// @Failure 500 "InternalServerError"
// @Router /designations/{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.DesignationService.UpdateById(requestBody.Fields, chi.URLParam(r, "id"))
info := c.LocalErrorService.Map(err)
dtorespwithoutdata.New(
info.ServiceErrorCode,
info.ServiceErrorMessage,
).RenderTo(w, info.HttpStatusCode)
}
// Delete Designations : HTTP endpoint to delete designations
// @Tags Designations
// @Description Delete a designation
// @Produce json
// @Param id path string true "Designation ID"
// @Success 200 {object} DeleteResponseDto "Success Response"
// @Failure 400 "BadRequest"
// @Failure 500 "InternalServerError"
// @Router /designations/{id} [DELETE]
func (c *Controller) Delete(w http.ResponseWriter, r *http.Request) {
err := c.DesignationService.DeleteById(chi.URLParam(r, "id"))
info := c.LocalErrorService.Map(err)
dtorespwithoutdata.New(
info.ServiceErrorCode,
info.ServiceErrorMessage,
).RenderTo(w, info.HttpStatusCode)
}