-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_api.go
220 lines (204 loc) · 7.02 KB
/
user_api.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
package main
import (
"crypto/rand"
"encoding/json"
"errors"
"log/slog"
"net/http"
"time"
"github.com/Suryarpan/chat-api/internal/apiconf"
"github.com/Suryarpan/chat-api/internal/auth"
"github.com/Suryarpan/chat-api/internal/database"
"github.com/Suryarpan/chat-api/render"
"github.com/go-chi/chi/v5"
"github.com/go-playground/validator/v10"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgtype"
)
const (
insufficientStorageUserError = "could not create user at this moment"
)
type PublicUserDetails struct {
UserID pgtype.UUID `json:"user_id"`
Username string `json:"username"`
DisplayName string `json:"display_name"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
LastLoggedIn pgtype.Timestamp `json:"last_logged_in,omitempty"`
}
func convertToPublicUser(u database.User) PublicUserDetails {
slog.Debug("converting to public data", "user_id", u.UserID, "user_name", u.Username)
return PublicUserDetails{
UserID: u.UserID,
Username: u.Username,
DisplayName: u.DisplayName,
CreatedAt: u.CreatedAt,
UpdatedAt: u.UpdatedAt,
LastLoggedIn: u.LastLoggedIn,
}
}
func handleGetUserDetail(w http.ResponseWriter, r *http.Request) {
user := auth.GetUserData(r)
slog.Info("getting user data", "user_id", user.UserID, "user_name", user.Username)
publicData := convertToPublicUser(user)
render.RespondSuccess(w, http.StatusOK, publicData)
}
type createUserData struct {
Username string `json:"username" validate:"required,min=5,max=50"`
DisplayName string `json:"display_name" validate:"required,min=5,max=150"`
Password string `json:"password" validate:"required,printascii,min=8"`
}
func handleCreateUser(w http.ResponseWriter, r *http.Request) {
cu := createUserData{}
reader := json.NewDecoder(r.Body)
err := reader.Decode(&cu)
if err != nil {
slog.Warn("could not decode create user data", "error", err)
render.RespondFailure(w, 400, "could not decode data")
return
}
slog.Info("creating new user", "user_name", cu.Username, "display_name", cu.DisplayName)
apiCfg := apiconf.GetConfig(r)
// validate incoming data
err = apiCfg.Validate.Struct(cu)
if err != nil {
validationErrors, ok := err.(validator.ValidationErrors)
if !ok {
slog.Error("error with validator definition", "error", err)
render.RespondFailure(w, http.StatusInternalServerError, internalServerErrorMssg)
} else {
render.RespondValidationFailure(w, validationErrors)
}
return
}
// check user name with DB
queries := database.New(apiCfg.ConnPool)
_, err = queries.GetUserByName(r.Context(), cu.Username)
if err == nil {
slog.Warn("could not check DB for username", "error", err)
render.RespondFailure(w, http.StatusNotAcceptable, map[string]string{"username": "already exists"})
return
}
// generate the password hash
passwordSalt := make([]byte, 128)
_, err = rand.Read(passwordSalt)
if err != nil {
slog.Error("could not generate salt for password", "error", err)
render.RespondFailure(w, http.StatusInsufficientStorage, insufficientStorageUserError)
return
}
password := auth.SaltyPassword([]byte(cu.Password), passwordSalt)
// store in DB
user, err := queries.CreateUser(r.Context(), database.CreateUserParams{
Username: cu.Username,
DisplayName: cu.DisplayName,
Password: password,
PasswordSalt: passwordSalt,
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
})
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
slog.Error(
"could not create user",
"error", pgErr.Message,
"code", pgErr.Code,
"constraint", pgErr.ConstraintName,
)
}
render.RespondFailure(w, http.StatusInsufficientStorage, insufficientStorageUserError)
return
}
// send back user data
render.RespondSuccess(w, http.StatusCreated, PublicUserDetails{
UserID: user.UserID,
Username: user.Username,
DisplayName: user.DisplayName,
CreatedAt: user.CreatedAt,
})
}
type updateUserData struct {
Username *string `json:"username" validate:"omitnil,min=5,max=50"`
DisplayName *string `json:"display_name" validate:"omitnil,min=5,max=150"`
Password *string `json:"password" validate:"omitnil,printascii,min=8"`
}
func If[T any](cond bool, vTrue, vFalse T) T {
if cond {
return vTrue
}
return vFalse
}
func handleUpdateUser(w http.ResponseWriter, r *http.Request) {
ud := updateUserData{}
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&ud)
if err != nil {
render.RespondFailure(w, 400, "could not decode data")
return
}
user := auth.GetUserData(r)
slog.Info("updating user details", "user_id", user.UserID, "user_name", user.Username)
// nothing to update
emptyData := updateUserData{}
if ud == emptyData {
slog.Info("nothing to update", "user_id", user.UserID, "user_name", user.Username)
render.RespondSuccess(w, http.StatusOK, convertToPublicUser(user))
return
}
apiCfg := apiconf.GetConfig(r)
// validate incoming data
err = apiCfg.Validate.Struct(ud)
if err != nil {
validationErrors, ok := err.(validator.ValidationErrors)
if !ok {
slog.Error("error with validator definition", "error", err)
render.RespondFailure(w, http.StatusInternalServerError, internalServerErrorMssg)
} else {
render.RespondValidationFailure(w, validationErrors)
}
return
}
// find the updated fields
user.Username = If(ud.Username != nil, *ud.Username, user.Username)
user.DisplayName = If(ud.DisplayName != nil, *ud.DisplayName, user.DisplayName)
user.Password = If(ud.Password != nil, auth.SaltyPassword([]byte(*ud.Password), user.PasswordSalt), user.Password)
// update in DB
queries := database.New(apiCfg.ConnPool)
updUser, err := queries.UpdateUserDetails(r.Context(), database.UpdateUserDetailsParams{
Username: user.Username,
DisplayName: user.DisplayName,
Password: user.Password,
UpdatedAt: time.Now().UTC(),
PvtID: user.PvtID,
})
if err != nil {
slog.Error("could not update user data", "user_id", user.UserID, "user_name", user.Username)
render.RespondFailure(w, http.StatusInsufficientStorage, "could not update at this time")
return
}
render.RespondSuccess(w, http.StatusOK, convertToPublicUser(updUser))
}
func handleDeleteUser(w http.ResponseWriter, r *http.Request) {
user := auth.GetUserData(r)
slog.Info("deleting user details", "user_id", user.UserID, "user_name", user.Username)
apiCfg := apiconf.GetConfig(r)
queries := database.New(apiCfg.ConnPool)
delUser, err := queries.DeleteUserDetails(r.Context(), user.PvtID)
if err != nil {
slog.Error("could not delete user details", "user_id", user.UserID, "user_name", user.Username)
render.RespondFailure(w, http.StatusInternalServerError, "could not delete at this time")
return
}
render.RespondSuccess(w, http.StatusOK, convertToPublicUser(delUser))
}
func UserRouter() *chi.Mux {
router := chi.NewMux()
router.With(auth.Authentication).Group(func(r chi.Router) {
r.Get("/", handleGetUserDetail)
r.Patch("/", handleUpdateUser)
r.Delete("/", handleDeleteUser)
})
router.Post("/", handleCreateUser)
return router
}