Create user

This commit is contained in:
vinchent 2024-08-23 21:07:13 +02:00
parent 05db85eca1
commit b183e7bf43
6 changed files with 95 additions and 4 deletions

View File

@ -712,3 +712,15 @@ func (app *application) AllUsers(w http.ResponseWriter, r *http.Request) {
}
app.writeJSON(w, http.StatusOK, allUsers)
}
func (app *application) OneUser(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
userID, _ := strconv.Atoi(id)
user, err := app.DB.GetOneUser(userID)
if err != nil {
app.errorLog.Println(err)
app.badRequest(w, r, err)
return
}
app.writeJSON(w, http.StatusOK, user)
}

View File

@ -38,6 +38,7 @@ func (app *application) routes() http.Handler {
mux.Post("/refund", app.RefundCharge)
mux.Post("/cancel-subscription", app.CancelSubscription)
mux.Post("/all-users", app.AllUsers)
mux.Post("/all-users/{id}", app.OneUser)
})
mux.Post("/api/forgot-password", app.SendPasswordResetEmail)
mux.Post("/api/reset-password", app.ResetPassword)

View File

@ -389,7 +389,7 @@ func (app *application) ShowSale(w http.ResponseWriter, r *http.Request) {
stringMap["refund-badge"] = "Refunded"
intMap := make(map[string]int)
intMap["isRefund"] = 1
intMap["is-refund"] = 1
if err := app.renderTemplate(w, r, "sale", &templateData{
StringMap: stringMap,

View File

@ -22,7 +22,7 @@ All Users
{{ end }}
{{ define "js" }}
<script type="module">
import {showUsers} from "/static/js/all-users.js"
import {showUsers} from "/static/js/users.js"
showUsers({{.API}});
</script>
{{ end }}

View File

@ -5,7 +5,65 @@ Admin User
{{ define "content" }}
<h2 class="mt-5">Admin user</h2>
<hr>
<form method="post"
action=""
name="user_form"
id="user_form"
class="needs-validation"
autocomplete="off"
novalidate>
<div class="mb-3">
<label for="first_name" class="form-label">First Name</label>
<input type="text"
name="first_name"
class="form-control"
required=""
autocomplete="first_name-new">
</div>
<div class="mb-3">
<label for="last_name" class="form-label">Last Name</label>
<input type="text"
name="last_name"
class="form-control"
required=""
autocomplete="last_name-new">
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="text"
name="email"
class="form-control"
required=""
autocomplete="email-new">
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="text"
name="password"
class="form-control"
autocomplete="password-new">
</div>
<div class="mb-3">
<label for="verify_password" class="form-label">Verify Password</label>
<input type="text"
name="verify_password"
class="form-control"
autocomplete="veryfy_password-new">
</div>
<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>
</div>
<div class="float-end">
<a href="javascript:void(0)" class="btn btn-danger" id="deleteBtn">Delete</a>
</div>
<div class="clearfix"></div>
</form>
{{ end }}
{{ define "js" }}
<script type="module">
import {showUser} from "/static/js/users.js"
showUser({{.API}});
</script>
{{ end }}

View File

@ -1,4 +1,3 @@
export function showUsers(api) {
const tbody = document.getElementById("user-table").getElementsByTagName("tbody")[0];
const token = localStorage.getItem("token");
@ -34,3 +33,24 @@ export function showUsers(api) {
}
});
}
export function showUser(api) {
const token = localStorage.getItem("token");
let id = window.location.pathname.split("/").pop();
const requestOptions = {
method: 'post',
headers: {
'Accept': `application/json`,
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token,
},
};
fetch(api + `/api/admin/all-users/${id}`, requestOptions)
.then(response => response.json())
.then(function (data) {
console.log(data);
});
}