delete users
This commit is contained in:
		@ -779,3 +779,17 @@ func (app *application) EditUser(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -40,6 +40,7 @@ func (app *application) routes() http.Handler {
 | 
			
		||||
		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)
 | 
			
		||||
 | 
			
		||||
@ -69,10 +69,13 @@ Admin User
 | 
			
		||||
{{ define "js" }}
 | 
			
		||||
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
 | 
			
		||||
<script type="module">
 | 
			
		||||
import {showUser, saveUser} from "/static/js/users.js"
 | 
			
		||||
import {showUser, saveUser, deleteUser} 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 }}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user