edit user

This commit is contained in:
vinchent 2024-08-26 13:37:05 +02:00
parent 591525e97f
commit bced6d7036
4 changed files with 113 additions and 4 deletions

View File

@ -724,3 +724,58 @@ func (app *application) OneUser(w http.ResponseWriter, r *http.Request) {
} }
app.writeJSON(w, http.StatusOK, user) 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)
}

View File

@ -39,6 +39,7 @@ func (app *application) routes() http.Handler {
mux.Post("/cancel-subscription", app.CancelSubscription) mux.Post("/cancel-subscription", app.CancelSubscription)
mux.Post("/all-users", app.AllUsers) mux.Post("/all-users", app.AllUsers)
mux.Post("/all-users/{id}", app.OneUser) 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/forgot-password", app.SendPasswordResetEmail)
mux.Post("/api/reset-password", app.ResetPassword) mux.Post("/api/reset-password", app.ResetPassword)

View File

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

View File

@ -79,3 +79,53 @@ export function showUser(api, userID) {
}) })
} }
export function saveUser(api, event) {
const token = localStorage.getItem("token");
let form = document.getElementById("user_form");
let id = window.location.pathname.split("/").pop();
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
form.classList.add("was-validated");
return;
}
form.classList.add("was-validated");
if (document.getElementById("password").value !== document.getElementById("verify_password").value) {
Swal.fire("Password do not match!");
return
}
let payload = {
id: parseInt(id),
first_name: document.getElementById("first_name").value,
last_name: document.getElementById("last_name").value,
email: document.getElementById("email").value,
password: document.getElementById("password").value,
}
const requestOptions = {
method: 'post',
headers: {
'Accept': `application/json`,
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token,
},
body: JSON.stringify(payload)
}
fetch(api + `/api/admin/all-users/edit/${id}`, requestOptions)
.then(response => response.json())
.then(function (data) {
console.log(data);
if (data.ok === false) {
Swal.fire("Error" + data.message)
} else {
location.href = "/admin/all-users"
}
});
}