pagination all-subscriptions

This commit is contained in:
2024-08-23 10:13:50 +02:00
parent c8b032236b
commit 0f0c896065
4 changed files with 86 additions and 10 deletions

View File

@ -556,11 +556,43 @@ func (app *application) AllSales(w http.ResponseWriter, r *http.Request) {
}
func (app *application) AllSubscriptions(w http.ResponseWriter, r *http.Request) {
allSubscriptions, err := app.DB.GetAllOrders(true)
if err != nil {
app.badRequest(w, r, err)
var payload struct {
PageSize int `json:"page_size"`
CurrentPage int `json:"page"`
}
app.writeJSON(w, http.StatusOK, allSubscriptions)
err := app.readJSON(w, r, &payload)
if err != nil {
app.errorLog.Println(err)
app.badRequest(w, r, err)
return
}
allSales, lastPage, totalRecords, err := app.DB.GetAllOrdersPaginated(
true,
payload.PageSize,
payload.CurrentPage,
)
if err != nil {
app.errorLog.Println(err)
app.badRequest(w, r, err)
return
}
var resp struct {
CurrentPage int `json:"current_page"`
PageSize int `json:"page_size"`
LastPage int `json:"last_page"`
TotalRecords int `json:"total_records"`
Orders []*models.Order `json:"orders"`
}
resp.CurrentPage = payload.CurrentPage
resp.PageSize = payload.PageSize
resp.LastPage = lastPage
resp.TotalRecords = totalRecords
resp.Orders = allSales
app.writeJSON(w, http.StatusOK, resp)
}
func (app *application) GetSale(w http.ResponseWriter, r *http.Request) {

View File

@ -20,11 +20,15 @@ All Subscriptions
<tbody>
</tbody>
</table>
<nav>
<ul id="paginator" class="pagination">
</ul>
</nav>
{{end}}
{{define "js"}}
<script type="module">
import {showTable} from "/static/js/all-subscriptions.js"
showTable({{.API}});
import {showTable, pageSize, currentPage} from "/static/js/all-subscriptions.js"
showTable({{.API}}, pageSize, currentPage);
</script>
{{end}}