-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
121 lines (112 loc) · 3.79 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
package credential
import (
"encoding/json"
"net/http"
"github.com/go-chi/chi"
"github.com/mrexmelle/connect-authx/internal/config"
"github.com/mrexmelle/connect-authx/internal/dto/dtorespwithoutdata"
"github.com/mrexmelle/connect-authx/internal/localerror"
)
type Controller struct {
ConfigService *config.Service
CredentialService *Service
LocalErrorService *localerror.Service
}
func NewController(cfg *config.Service, svc *Service, les *localerror.Service) *Controller {
return &Controller{
ConfigService: cfg,
CredentialService: svc,
LocalErrorService: les,
}
}
// Post Credentials : HTTP endpoint to post new credentials
// @Tags Credentials
// @Description Post a new credential
// @Accept json
// @Produce json
// @Param data body PostRequestDto true "Credential Request"
// @Success 200 {object} PostResponseDto "Success Response"
// @Failure 400 "BadRequest"
// @Failure 500 "InternalServerError"
// @Router /credentials [POST]
func (c *Controller) Post(w http.ResponseWriter, r *http.Request) {
var requestBody PostRequestDto
err := json.NewDecoder(r.Body).Decode(&requestBody)
if err != nil {
dtorespwithoutdata.New(
localerror.ErrBadJson.Error(),
err.Error(),
).RenderTo(w, http.StatusBadRequest)
return
}
err = c.CredentialService.Create(requestBody)
info := c.LocalErrorService.Map(err)
dtorespwithoutdata.New(
info.ServiceErrorCode,
info.ServiceErrorMessage,
).RenderTo(w, info.HttpStatusCode)
}
// Delete Credentials : HTTP endpoint to delete credentials
// @Tags Credentials
// @Description Delete a credential
// @Produce json
// @Param employee_id path string true "Employee ID"
// @Success 200 {object} DeleteResponseDto "Success Response"
// @Failure 400 "BadRequest"
// @Failure 500 "InternalServerError"
// @Router /credentials/{employee_id} [DELETE]
func (c *Controller) Delete(w http.ResponseWriter, r *http.Request) {
err := c.CredentialService.DeleteByEmployeeId(chi.URLParam(r, "employee_id"))
info := c.LocalErrorService.Map(err)
dtorespwithoutdata.New(
info.ServiceErrorCode,
info.ServiceErrorMessage,
).RenderTo(w, info.HttpStatusCode)
}
// Patch Password : HTTP endpoint to patch password
// @Tags Credentials
// @Description Patch a credential's password
// @Accept json
// @Produce json
// @Param employee_id path string true "Employee ID"
// @Param data body PatchPasswordRequestDto true "Password Patch Request"
// @Success 200 {object} PatchPasswordResponseDto "Success Response"
// @Failure 400 "BadRequest"
// @Failure 500 "InternalServerError"
// @Router /credentials/{employee_id}/password [PATCH]
func (c *Controller) PatchPassword(w http.ResponseWriter, r *http.Request) {
var requestBody PatchPasswordRequestDto
err := json.NewDecoder(r.Body).Decode(&requestBody)
if err != nil {
dtorespwithoutdata.New(
localerror.ErrBadJson.Error(),
err.Error(),
).RenderTo(w, http.StatusBadRequest)
return
}
err = c.CredentialService.UpdatePasswordByEmployeeId(
chi.URLParam(r, "employee_id"),
requestBody)
info := c.LocalErrorService.Map(err)
dtorespwithoutdata.New(
info.ServiceErrorCode,
info.ServiceErrorMessage,
).RenderTo(w, info.HttpStatusCode)
}
// Reset Password : HTTP endpoint to reset password
// @Tags Credentials
// @Description Reset a credential's password
// @Produce json
// @Param employee_id path string true "Employee ID"
// @Success 200 {object} PatchPasswordResponseDto "Success Response"
// @Failure 400 "BadRequest"
// @Failure 500 "InternalServerError"
// @Router /credentials/{employee_id}/password [DELETE]
func (c *Controller) DeletePassword(w http.ResponseWriter, r *http.Request) {
err := c.CredentialService.ResetPasswordByEmployeeId(chi.URLParam(r, "employee_id"))
info := c.LocalErrorService.Map(err)
dtorespwithoutdata.New(
info.ServiceErrorCode,
info.ServiceErrorMessage,
).RenderTo(w, info.HttpStatusCode)
}