Compare commits

..

No commits in common. "3f0ddf7138e54aad70329b4914ee9be04198b284" and "591525e97fd62c113a11b3e13131f9781dc75ea2" have entirely different histories.

5 changed files with 4 additions and 178 deletions

View File

@ -724,72 +724,3 @@ 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)
}
func (app *application) DeleteUser(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
userID, _ := strconv.Atoi(id)
err := app.DB.DeleteUser(userID)
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,8 +39,6 @@ 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("/all-users/delete/{id}", app.DeleteUser)
})
mux.Post("/api/forgot-password", app.SendPasswordResetEmail)
mux.Post("/api/reset-password", app.ResetPassword)

View File

@ -41,7 +41,7 @@ Admin User
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password"
<input type="text"
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="password"
<input type="text"
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="/admin/all-users" class="btn btn-warning" id="cancelBtn">Cancel</a>
<a href="javascript:void(0)" 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,13 +69,7 @@ Admin User
{{ define "js" }}
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script type="module">
import {showUser, saveUser, deleteUser} from "/static/js/users.js"
import {showUser} from "/static/js/users.js"
showUser({{.API}}, {{.UserID}});
document.getElementById("saveBtn").addEventListener("click", (evt) => {
saveUser({{.API}}, evt);
});
document.getElementById("deleteBtn").addEventListener("click", () => {
deleteUser({{.API}});
});
</script>
{{ end }}

View File

@ -629,15 +629,5 @@ func (m *DBModel) DeleteUser(id int) error {
if err != nil {
return err
}
stmt = `
DELETE FROM tokens
WHERE id = ?;
`
_, err = m.DB.ExecContext(ctx, stmt, id)
if err != nil {
return err
}
return nil
}

View File

@ -79,90 +79,3 @@ 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) {
Swal.fire("Error" + data.message)
} else {
location.href = "/admin/all-users"
}
});
}
export function deleteUser(api) {
const token = localStorage.getItem("token");
let id = window.location.pathname.split("/").pop();
Swal.fire({
title: "Are you sure?",
text: "You won't be able to undo this!",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Delete user"
}).then((result) => {
if (result.isConfirmed) {
const requestOptions = {
method: 'post',
headers: {
'Accept': `application/json`,
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token,
},
};
fetch(api + `/api/admin/all-users/delete/${id}`, requestOptions)
.then(response => response.json())
.then(function (data) {
console.log(data);
if (!data.ok) {
Swal.fire("Error" + data.message)
} else {
location.href = "/admin/all-users"
}
});
}
});
}