Creating db method to paginate all orders

This commit is contained in:
2024-08-22 21:48:14 +02:00
parent c697da63b9
commit 63585e31f6
2 changed files with 113 additions and 2 deletions

View File

@ -516,11 +516,35 @@ func (app *application) ResetPassword(w http.ResponseWriter, r *http.Request) {
}
func (app *application) AllSales(w http.ResponseWriter, r *http.Request) {
allSales, err := app.DB.GetAllOrders(false)
var payload struct {
PageSize int `json:"page_size"`
CurrentPage int `json:"page"`
}
err := app.readJSON(w, r, &payload)
if err != nil {
app.badRequest(w, r, err)
}
app.writeJSON(w, http.StatusOK, allSales)
allSales, lastPage, totalRecords, err := app.DB.GetAllOrdersPaginated(false, 2, 1)
if err != nil {
app.badRequest(w, r, err)
}
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 = 1
resp.PageSize = payload.PageSize
resp.LastPage = lastPage
resp.TotalRecords = totalRecords
resp.Orders = allSales
app.writeJSON(w, http.StatusOK, resp)
}
func (app *application) AllSubscriptions(w http.ResponseWriter, r *http.Request) {