all repos

onasty @ 73fe76e

a one-time notes service

onasty/internal/transport/http/apiv1/user.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
refactor: endpoints should not be pointer receiver (#217), 8 months ago
1
package apiv1
2
3
import (
4
	"net/http"
5
	"time"
6
7
	"github.com/gin-gonic/gin"
8
	"github.com/olexsmir/onasty/internal/dtos"
9
)
10
11
type getMeResponse struct {
12
	Email        string    `json:"email"`
13
	CreatedAt    time.Time `json:"created_at"`
14
	LastLoginAt  time.Time `json:"last_login_at"`
15
	NotesCreated int       `json:"notes_created"`
16
}
17
18
func (a APIV1) getMeHandler(c *gin.Context) {
19
	uinfo, err := a.usersrv.GetUserInfo(c.Request.Context(), a.getUserID(c))
20
	if err != nil {
21
		errorResponse(c, err)
22
		return
23
	}
24
25
	c.JSON(http.StatusOK, getMeResponse{
26
		Email:        uinfo.Email,
27
		CreatedAt:    uinfo.CreatedAt,
28
		LastLoginAt:  uinfo.LastLoginAt,
29
		NotesCreated: uinfo.NotesCreated,
30
	})
31
}
32
33
type changePasswordRequest struct {
34
	CurrentPassword string `json:"current_password"`
35
	NewPassword     string `json:"new_password"`
36
}
37
38
func (a APIV1) changePasswordHandler(c *gin.Context) {
39
	var req changePasswordRequest
40
	if err := c.ShouldBindJSON(&req); err != nil {
41
		invalidRequest(c)
42
		return
43
	}
44
45
	if err := a.usersrv.ChangePassword(
46
		c.Request.Context(),
47
		a.getUserID(c),
48
		dtos.ChangeUserPassword{
49
			CurrentPassword: req.CurrentPassword,
50
			NewPassword:     req.NewPassword,
51
		}); err != nil {
52
		errorResponse(c, err)
53
		return
54
	}
55
56
	c.Status(http.StatusOK)
57
}
58
59
type requestResetPasswordRequest struct {
60
	Email string `json:"email"`
61
}
62
63
func (a APIV1) requestResetPasswordHandler(c *gin.Context) {
64
	var req requestResetPasswordRequest
65
	if err := c.ShouldBindJSON(&req); err != nil {
66
		invalidRequest(c)
67
		return
68
	}
69
70
	if err := a.usersrv.RequestPasswordReset(
71
		c.Request.Context(),
72
		dtos.RequestResetPassword{
73
			Email: req.Email,
74
		},
75
	); err != nil {
76
		errorResponse(c, err)
77
		return
78
	}
79
80
	c.Status(http.StatusOK)
81
}
82
83
type resetPasswordRequest struct {
84
	Password string `json:"password"`
85
}
86
87
func (a APIV1) resetPasswordHandler(c *gin.Context) {
88
	var req resetPasswordRequest
89
	if err := c.ShouldBindJSON(&req); err != nil {
90
		invalidRequest(c)
91
		return
92
	}
93
94
	if err := a.usersrv.ResetPassword(
95
		c.Request.Context(),
96
		dtos.ResetPassword{
97
			Token:       c.Param("token"),
98
			NewPassword: req.Password,
99
		},
100
	); err != nil {
101
		errorResponse(c, err)
102
		return
103
	}
104
105
	c.Status(http.StatusOK)
106
}
107
108
type changeEmailRequest struct {
109
	NewEmail string `json:"new_email"`
110
}
111
112
func (a APIV1) requestEmailChangeHandler(c *gin.Context) {
113
	var req changeEmailRequest
114
	if err := c.ShouldBindJSON(&req); err != nil {
115
		invalidRequest(c)
116
		return
117
	}
118
119
	if err := a.usersrv.RequestEmailChange(
120
		c.Request.Context(),
121
		a.getUserID(c),
122
		dtos.ChangeEmail{
123
			NewEmail: req.NewEmail,
124
		}); err != nil {
125
		errorResponse(c, err)
126
		return
127
	}
128
129
	c.Status(http.StatusOK)
130
}
131
132
func (a APIV1) changeEmailHandler(c *gin.Context) {
133
	if err := a.usersrv.ChangeEmail(
134
		c.Request.Context(),
135
		c.Param("token"),
136
	); err != nil {
137
		errorResponse(c, err)
138
		return
139
	}
140
141
	c.String(http.StatusOK, "email changed")
142
}
143
144
func (a APIV1) verifyHandler(c *gin.Context) {
145
	if err := a.usersrv.Verify(
146
		c.Request.Context(),
147
		c.Param("token"),
148
	); err != nil {
149
		errorResponse(c, err)
150
		return
151
	}
152
153
	c.String(http.StatusOK, "email verified")
154
}
155
156
type resendVerificationEmailRequest struct {
157
	Email string `json:"email"`
158
}
159
160
func (a APIV1) resendVerificationEmailHandler(c *gin.Context) {
161
	var req resendVerificationEmailRequest
162
	if err := c.ShouldBindJSON(&req); err != nil {
163
		invalidRequest(c)
164
		return
165
	}
166
167
	if err := a.usersrv.ResendVerificationEmail(
168
		c.Request.Context(),
169
		dtos.ResendVerificationEmail{
170
			Email: req.Email,
171
		}); err != nil {
172
		errorResponse(c, err)
173
		return
174
	}
175
176
	c.Status(http.StatusOK)
177
}