Creating db method to paginate all orders
This commit is contained in:
		@ -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()
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user