all sales

This commit is contained in:
vinchent 2024-08-21 23:18:51 +02:00
parent 21273c22cb
commit f8ee76939e
4 changed files with 85 additions and 11 deletions

View File

@ -520,3 +520,11 @@ func (app *application) ResetPassword(w http.ResponseWriter, r *http.Request) {
resp.Message = "Password reset."
app.writeJSON(w, http.StatusCreated, resp)
}
func (app *application) AllSales(w http.ResponseWriter, r *http.Request) {
allSales, err := app.DB.GetAllOrders()
if err != nil {
app.badRequest(w, r, err)
}
app.writeJSON(w, http.StatusOK, allSales)
}

View File

@ -32,6 +32,7 @@ func (app *application) routes() http.Handler {
// })
mux.Post("/virtual-terminal-succeeded", app.VirtualTerminalPaymentSucceeded)
mux.Post("/all-sales", app.AllSales)
})
mux.Post("/api/forgot-password", app.SendPasswordResetEmail)
mux.Post("/api/reset-password", app.ResetPassword)

View File

@ -1,14 +1,26 @@
{{template "base" .}}
{{define "title"}}
{{ template "base" . }}
{{ define "title" }}
All Subscriptions
{{end}}
{{define "content"}}
{{ end }}
{{ define "content" }}
<h2 class="mt-5">All Sales</h2>
<hr>
{{end}}
{{define "js"}}
{{end}}
<table id="sales-table" class="table table-striped">
<thead>
<tr>
<th>Transaction</th>
<th>Customer</th>
<th>Product</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
{{ end }}
{{ define "js" }}
<script type="module">
import {showTable} from "/static/js/all-sales.js"
showTable({{.API}});
</script>
{{ end }}

53
static/js/all-sales.js Normal file
View File

@ -0,0 +1,53 @@
export function showTable(api) {
let token = localStorage.getItem("token");
let tbody = document.getElementById("sales-table").getElementsByTagName("tbody")[0];
const requestOptions = {
method: 'post',
headers: {
'Accept': `application/json`,
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token,
},
};
fetch(api + "/api/admin/all-sales", requestOptions)
.then(response => response.json())
.then(function (data) {
if (data) {
data.forEach(function (i) {
let newRow = tbody.insertRow();
let newCell = newRow.insertCell();
newCell.innerHTML = `<a href="/admin/sales/${i.id}">Order ${i.id}</a>`;
newCell = newRow.insertCell();
let item = document.createTextNode(i.customer.last_name + ", " + i.customer.first_name);
newCell.appendChild(item)
newCell = newRow.insertCell();
item = document.createTextNode(i.widget.name);
newCell.appendChild(item)
let cur = formatCurrency(i.transaction.amount)
newCell = newRow.insertCell();
item = document.createTextNode(cur);
newCell.appendChild(item)
});
} else {
let newRow = tbody.insertRow();
let newCell = newRow.insertCell();
newCell.setAttribute("colspan", "4");
newCell.innerHTML = "No data available";
}
});
}
function formatCurrency(amount) {
let c = parseFloat(amount/100);
return c.toLocaleString("fr-FR", {
style:"currency",
currency: "EUR",
});
}