v0.30: product pages, categories, sale prices and order status timeline

This commit is contained in:
shop
2026-06-25 17:29:43 +03:00
parent 532351e1c9
commit e000264bb1
26 changed files with 1061 additions and 248 deletions
+15 -3
View File
@@ -2,6 +2,7 @@ package repository
import (
"context"
"database/sql"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
@@ -138,9 +139,12 @@ func (r *CartRepository) ItemCount(ctx context.Context, cartID int) (int, error)
func (r *CartRepository) Items(ctx context.Context, cartID int) ([]models.CartItem, error) {
rows, err := r.pool.Query(ctx, `
SELECT ci.id, ci.cart_id, ci.product_id, ci.quantity,
p.id, p.name, p.description, p.details, p.price, p.image_url, p.category, p.is_active, p.created_at, p.updated_at
p.id, p.name, p.description, p.details, p.price, COALESCE(p.sale_price, 0),
p.image_url, COALESCE(c.name, p.category, 'Разное'), p.category_id,
COALESCE(c.slug, ''), p.is_active, p.created_at, p.updated_at
FROM cart_items ci
JOIN products p ON p.id = ci.product_id
LEFT JOIN categories c ON c.id = p.category_id
WHERE ci.cart_id = $1 AND p.is_active = true
ORDER BY ci.id
`, cartID)
@@ -153,12 +157,18 @@ func (r *CartRepository) Items(ctx context.Context, cartID int) ([]models.CartIt
for rows.Next() {
var ci models.CartItem
var p models.Product
var catID sql.NullInt64
if err := rows.Scan(
&ci.ID, &ci.CartID, &ci.ProductID, &ci.Quantity,
&p.ID, &p.Name, &p.Description, &p.Details, &p.Price, &p.ImageURL, &p.Category, &p.IsActive, &p.CreatedAt, &p.UpdatedAt,
&p.ID, &p.Name, &p.Description, &p.Details, &p.Price, &p.SalePrice,
&p.ImageURL, &p.Category, &catID, &p.CategorySlug, &p.IsActive, &p.CreatedAt, &p.UpdatedAt,
); err != nil {
return nil, err
}
if catID.Valid {
id := int(catID.Int64)
p.CategoryID = &id
}
ci.Product = p
items = append(items, ci)
}
@@ -168,7 +178,9 @@ func (r *CartRepository) Items(ctx context.Context, cartID int) ([]models.CartIt
func (r *CartRepository) Total(ctx context.Context, cartID int) (float64, error) {
var total float64
err := r.pool.QueryRow(ctx, `
SELECT COALESCE(SUM(ci.quantity * p.price), 0)
SELECT COALESCE(SUM(ci.quantity * CASE
WHEN p.sale_price > 0 AND p.sale_price < p.price THEN p.sale_price
ELSE p.price END), 0)
FROM cart_items ci JOIN products p ON p.id = ci.product_id
WHERE ci.cart_id = $1
`, cartID).Scan(&total)