This commit is contained in:
vinchent 2024-08-21 23:43:16 +02:00
parent 8fb1844dcd
commit 1984024394
8 changed files with 117 additions and 1 deletions

View File

@ -536,3 +536,14 @@ func (app *application) AllSubscriptions(w http.ResponseWriter, r *http.Request)
}
app.writeJSON(w, http.StatusOK, allSubscriptions)
}
func (app *application) GetSale(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
orderID, _ := strconv.Atoi(id)
order, err := app.DB.GetOrderByID(orderID)
if err != nil {
app.badRequest(w, r, err)
}
app.writeJSON(w, http.StatusOK, order)
}

View File

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

View File

@ -379,3 +379,9 @@ func (app *application) AllSubscriptions(w http.ResponseWriter, r *http.Request)
app.errorLog.Println(err)
}
}
func (app *application) ShowSale(w http.ResponseWriter, r *http.Request) {
if err := app.renderTemplate(w, r, "sale", &templateData{}); err != nil {
app.errorLog.Println(err)
}
}

View File

@ -17,6 +17,7 @@ func (app *application) routes() http.Handler {
mux.Get("/virtual-terminal", app.VirtualTerminal)
mux.Get("/all-sales", app.AllSales)
mux.Get("/all-subscriptions", app.AllSubscriptions)
mux.Get("/sales/{id}", app.ShowSale)
})
// mux.Post("/virtual-terminal-payment-succeeded", app.VirtualTerminalPaymentSucceeded)

View File

@ -23,7 +23,7 @@ All Subscriptions
{{define "js"}}
<script type="module">
import {showTable} from "/static/js/all-sales.js"
import {showTable} from "/static/js/all-subscriptions.js"
showTable({{.API}});
</script>
{{end}}

View File

@ -0,0 +1,16 @@
{{template "base" .}}
{{define "title"}}
Sale
{{end}}
{{define "content"}}
<h2 class="mt-5">Sale</h2>
<hr>
{{end}}
{{define "js"}}
<script type="module">
import {showInfo} from "/static/js/sale.js"
showInfo({{.API}});
</script>
{{end}}

View File

@ -360,3 +360,56 @@ func (m *DBModel) GetAllOrders(isRecurring bool) ([]*Order, error) {
}
return orders, nil
}
func (m *DBModel) GetOrderByID(ID int) (Order, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `
SELECT
o.id, o.widget_id, o.transaction_id, o.customer_id,
o.status_id, o.quantity, o.amount, o.created_at, o.updated_at,
w.id, w.name, t.id, t.amount, t.currency, t.last_four,
t.expiry_month, t.expiry_year, t.payment_intent, t.bank_return_code,
c.id, c.first_name, c.last_name, c.email
FROM orders o
LEFT JOIN widgets w on (o.widget_id = w.id)
LEFT JOIN transactions t on (o.transaction_id = t.id)
LEFT JOIN customers c on (o.customer_id = c.id)
WHERE
o.id = ?
`
var o Order
row := m.DB.QueryRowContext(ctx, query, ID)
err := row.Scan(
&o.ID,
&o.WidgetID,
&o.TransactionID,
&o.CustomerID,
&o.StatusID,
&o.Quantity,
&o.Amount,
&o.CreatedAt,
&o.UpdatedAt,
&o.Widget.ID,
&o.Widget.Name,
&o.Transaction.ID,
&o.Transaction.Amount,
&o.Transaction.Currency,
&o.Transaction.LastFour,
&o.Transaction.ExpiryMonth,
&o.Transaction.ExpiryYear,
&o.Transaction.PaymentIntent,
&o.Transaction.BankReturnCode,
&o.Customer.ID,
&o.Customer.FirstName,
&o.Customer.LastName,
&o.Customer.Email,
)
if err != nil {
return o, err
}
return o, nil
}

28
static/js/sale.js Normal file
View File

@ -0,0 +1,28 @@
export function showInfo(api) {
let 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/get-sale/" + id, requestOptions)
.then(response => response.json())
.then(function (data) {
console.log(data);
});
}
function formatCurrency(amount) {
let c = parseFloat(amount/100);
return c.toLocaleString("fr-FR", {
style:"currency",
currency: "EUR",
});
}