package repository import ( "context" "github.com/jackc/pgx/v5/pgxpool" "shop/internal/models" ) type PriceHistoryRepository struct { pool *pgxpool.Pool } func NewPriceHistoryRepository(pool *pgxpool.Pool) *PriceHistoryRepository { return &PriceHistoryRepository{pool: pool} } func (r *PriceHistoryRepository) Record(ctx context.Context, productID int, price, salePrice float64) error { _, err := r.pool.Exec(ctx, ` INSERT INTO product_price_history (product_id, price, sale_price) VALUES ($1, $2, NULLIF($3, 0)) `, productID, price, salePrice) return err } func (r *PriceHistoryRepository) ByProduct(ctx context.Context, productID int, limit int) ([]models.PriceHistoryEntry, error) { rows, err := r.pool.Query(ctx, ` SELECT id, product_id, price, COALESCE(sale_price, 0), recorded_at FROM product_price_history WHERE product_id = $1 ORDER BY recorded_at DESC LIMIT $2 `, productID, limit) if err != nil { return nil, err } defer rows.Close() var entries []models.PriceHistoryEntry for rows.Next() { var e models.PriceHistoryEntry if err := rows.Scan(&e.ID, &e.ProductID, &e.Price, &e.SalePrice, &e.RecordedAt); err != nil { return nil, err } entries = append(entries, e) } return entries, rows.Err() }