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)
|
||||
|
@ -41,7 +41,7 @@ Admin User
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="password" class="form-label">Password</label>
|
||||
<input type="text"
|
||||
<input type="password"
|
||||
name="password"
|
||||
id="password"
|
||||
class="form-control"
|
||||
@ -49,7 +49,7 @@ Admin User
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="verify_password" class="form-label">Verify Password</label>
|
||||
<input type="text"
|
||||
<input type="password"
|
||||
name="verify_password"
|
||||
id="verify_password"
|
||||
class="form-control"
|
||||
@ -58,7 +58,7 @@ Admin User
|
||||
<hr>
|
||||
<div class="float-start">
|
||||
<a href="javascript:void(0)" class="btn btn-primary" id="saveBtn">Save Changes</a>
|
||||
<a href="javascript:void(0)" class="btn btn-warning" id="cancelBtn">Cancel</a>
|
||||
<a href="/admin/all-users" class="btn btn-warning" id="cancelBtn">Cancel</a>
|
||||
</div>
|
||||
<div class="float-end">
|
||||
<a href="javascript:void(0)" class="btn btn-danger d-none" id="deleteBtn">Delete</a>
|
||||
@ -69,7 +69,10 @@ Admin User
|
||||
{{ define "js" }}
|
||||
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
|
||||
<script type="module">
|
||||
import {showUser} from "/static/js/users.js"
|
||||
import {showUser, saveUser} from "/static/js/users.js"
|
||||
showUser({{.API}}, {{.UserID}});
|
||||
document.getElementById("saveBtn").addEventListener("click", (evt) => {
|
||||
saveUser({{.API}}, evt);
|
||||
});
|
||||
</script>
|
||||
{{ end }}
|
||||
|
Reference in New Issue
Block a user