-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
88 lines (74 loc) · 2 KB
/
http.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
package transport
import (
"net/http"
"strconv"
andro "github.com/soldevx/kuiper/andro/pkg/utl/model"
"github.com/soldevx/kuiper/andro/pkg/api/password"
"github.com/labstack/echo"
)
// HTTP represents password http transport service
type HTTP struct {
svc password.Service
}
// NewHTTP creates new password http service
func NewHTTP(svc password.Service, er *echo.Group) {
h := HTTP{svc}
pr := er.Group("/password")
// swagger:operation PATCH /v1/password/{id} password pwChange
// ---
// summary: Changes user's password.
// description: If user's old passowrd is correct, it will be replaced with new password.
// parameters:
// - name: id
// in: path
// description: id of user
// type: int
// required: true
// - name: request
// in: body
// description: Request body
// required: true
// schema:
// "$ref": "#/definitions/pwChange"
// responses:
// "200":
// "$ref": "#/responses/ok"
// "400":
// "$ref": "#/responses/errMsg"
// "401":
// "$ref": "#/responses/err"
// "403":
// "$ref": "#/responses/err"
// "500":
// "$ref": "#/responses/err"
pr.PATCH("/:id", h.change)
}
// Custom errors
var (
ErrPasswordsNotMaching = echo.NewHTTPError(http.StatusBadRequest, "passwords do not match")
)
// Password change request
// swagger:model pwChange
type changeReq struct {
ID int `json:"-"`
OldPassword string `json:"old_password" validate:"required,min=8"`
NewPassword string `json:"new_password" validate:"required,min=8"`
NewPasswordConfirm string `json:"new_password_confirm" validate:"required"`
}
func (h *HTTP) change(c echo.Context) error {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
return andro.ErrBadRequest
}
p := new(changeReq)
if err := c.Bind(p); err != nil {
return err
}
if p.NewPassword != p.NewPasswordConfirm {
return ErrPasswordsNotMaching
}
if err := h.svc.Change(c, id, p.OldPassword, p.NewPassword); err != nil {
return err
}
return c.NoContent(http.StatusOK)
}