Creating db method to paginate all orders
This commit is contained in:
parent
c697da63b9
commit
63585e31f6
@ -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) {
|
||||
|
@ -361,6 +361,93 @@ func (m *DBModel) GetAllOrders(isRecurring bool) ([]*Order, error) {
|
||||
return orders, nil
|
||||
}
|
||||
|
||||
func (m *DBModel) GetAllOrdersPaginated(
|
||||
isRecurring bool,
|
||||
pageSize, page int,
|
||||
) ([]*Order, int, int, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
orders := []*Order{}
|
||||
|
||||
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
|
||||
w.is_recurring = ?
|
||||
ORDER BY
|
||||
o.created_at DESC
|
||||
LIMIT ? OFFSET ?
|
||||
`
|
||||
|
||||
rows, err := m.DB.QueryContext(ctx, query, isRecurring, pageSize, offset)
|
||||
if err != nil {
|
||||
return nil, 0, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var o Order
|
||||
err = rows.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 nil, 0, 0, err
|
||||
}
|
||||
orders = append(orders, &o)
|
||||
}
|
||||
|
||||
query = `
|
||||
SELECT COUNT(o.id)
|
||||
FROM orders o
|
||||
LEFT JOIN widgets w on (o.widget_id = w.id)
|
||||
WHERE w.is_recurring = ?
|
||||
`
|
||||
|
||||
var totalRecords int
|
||||
countRow := m.DB.QueryRowContext(ctx, query, isRecurring)
|
||||
err = countRow.Scan(&totalRecords)
|
||||
if err != nil {
|
||||
return nil, 0, 0, err
|
||||
}
|
||||
|
||||
lastPage := totalRecords / pageSize
|
||||
|
||||
return orders, lastPage, totalRecords, nil
|
||||
}
|
||||
|
||||
func (m *DBModel) GetOrderByID(ID int) (Order, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
Loading…
Reference in New Issue
Block a user