Add user auth, cart, checkout and personal account for guests and registered users
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"shop/internal/models"
|
||||
)
|
||||
|
||||
type OrderRepository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewOrderRepository(pool *pgxpool.Pool) *OrderRepository {
|
||||
return &OrderRepository{pool: pool}
|
||||
}
|
||||
|
||||
func (r *OrderRepository) Create(ctx context.Context, order *models.Order, items []models.CartItem) error {
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO orders (user_id, guest_name, guest_email, guest_phone, address, total, status)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, 'new')
|
||||
RETURNING id, created_at
|
||||
`, order.UserID, order.GuestName, order.GuestEmail, order.GuestPhone, order.Address, order.Total,
|
||||
).Scan(&order.ID, &order.CreatedAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, item := range items {
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO order_items (order_id, product_id, product_name, price, quantity)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
`, order.ID, item.ProductID, item.Product.Name, item.Product.Price, item.Quantity)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (r *OrderRepository) ByUser(ctx context.Context, userID int) ([]models.Order, error) {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT id, user_id, guest_name, guest_email, guest_phone, address, total, status, created_at
|
||||
FROM orders WHERE user_id = $1 ORDER BY created_at DESC
|
||||
`, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var orders []models.Order
|
||||
for rows.Next() {
|
||||
var o models.Order
|
||||
if err := rows.Scan(&o.ID, &o.UserID, &o.GuestName, &o.GuestEmail, &o.GuestPhone, &o.Address, &o.Total, &o.Status, &o.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
o.Items, err = r.items(ctx, o.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
orders = append(orders, o)
|
||||
}
|
||||
return orders, rows.Err()
|
||||
}
|
||||
|
||||
func (r *OrderRepository) items(ctx context.Context, orderID int) ([]models.OrderItem, error) {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT id, order_id, product_id, product_name, price, quantity
|
||||
FROM order_items WHERE order_id = $1
|
||||
`, orderID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var items []models.OrderItem
|
||||
for rows.Next() {
|
||||
var i models.OrderItem
|
||||
if err := rows.Scan(&i.ID, &i.OrderID, &i.ProductID, &i.ProductName, &i.Price, &i.Quantity); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
return items, rows.Err()
|
||||
}
|
||||
Reference in New Issue
Block a user