edit user
This commit is contained in:
@ -724,3 +724,58 @@ func (app *application) OneUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
app.writeJSON(w, http.StatusOK, user)
|
||||
}
|
||||
|
||||
func (app *application) EditUser(w http.ResponseWriter, r *http.Request) {
|
||||
id := chi.URLParam(r, "id")
|
||||
userID, _ := strconv.Atoi(id)
|
||||
|
||||
var user models.User
|
||||
err := app.readJSON(w, r, &user)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
if userID > 0 {
|
||||
err = app.DB.EditUser(user)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
if user.Password != "" {
|
||||
newHash, err := bcrypt.GenerateFromPassword([]byte(user.Password), 12)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = app.DB.UpdatePasswordForUser(user, string(newHash))
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
} else {
|
||||
newHash, err := bcrypt.GenerateFromPassword([]byte(user.Password), 12)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
err = app.DB.AddUser(user, string(newHash))
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var resp jsonResponse
|
||||
resp.OK = true
|
||||
app.writeJSON(w, http.StatusOK, resp)
|
||||
}
|
||||
|
@ -39,6 +39,7 @@ func (app *application) routes() http.Handler {
|
||||
mux.Post("/cancel-subscription", app.CancelSubscription)
|
||||
mux.Post("/all-users", app.AllUsers)
|
||||
mux.Post("/all-users/{id}", app.OneUser)
|
||||
mux.Post("/all-users/edit/{id}", app.EditUser)
|
||||
})
|
||||
mux.Post("/api/forgot-password", app.SendPasswordResetEmail)
|
||||
mux.Post("/api/reset-password", app.ResetPassword)
|
||||
|
Reference in New Issue
Block a user