- Go API server with PostgreSQL + Redis - AI floor replacement (OpenRouter Gemini) - Product database (10 brands, 3539 products) - Recommendation engine, calculator, articles - Redis async queue + worker pool - Hot product caching, brand view tracking - JWT auth, favorites, projects - Docker deployment ready Co-Authored-By: Claude <noreply@anthropic.com>
80 lines
2.2 KiB
Go
80 lines
2.2 KiB
Go
package repository
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"floorvisualizer/internal/model"
|
|
)
|
|
|
|
func AddFavorite(d *sql.DB, userID string, f model.Favorite) error {
|
|
_, err := d.Exec(`
|
|
INSERT INTO favorites (user_id, sku, brand, series_name, style_name,
|
|
main_image_url, category, material, color_tone, price_per_sqft)
|
|
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)
|
|
ON CONFLICT (user_id, sku) DO NOTHING
|
|
`, userID, f.SKU, f.Brand, f.SeriesName, f.StyleName,
|
|
f.MainImageURL, f.Category, f.Material, f.ColorTone, f.PricePerSqft)
|
|
return err
|
|
}
|
|
|
|
func RemoveFavorite(d *sql.DB, userID, sku string) error {
|
|
_, err := d.Exec(`DELETE FROM favorites WHERE user_id = $1 AND sku = $2`, userID, sku)
|
|
return err
|
|
}
|
|
|
|
// GetFavoritedSKUs returns a set of SKUs favorited by the user.
|
|
// Returns empty set if userID is empty.
|
|
func GetFavoritedSKUs(d *sql.DB, userID string, skus []string) (map[string]bool, error) {
|
|
result := map[string]bool{}
|
|
if userID == "" || len(skus) == 0 {
|
|
return result, nil
|
|
}
|
|
// Build IN clause
|
|
placeholders := make([]string, len(skus))
|
|
args := make([]interface{}, 0, len(skus)+1)
|
|
args = append(args, userID)
|
|
for i, sku := range skus {
|
|
placeholders[i] = fmt.Sprintf("$%d", i+2)
|
|
args = append(args, sku)
|
|
}
|
|
query := fmt.Sprintf(
|
|
"SELECT sku FROM favorites WHERE user_id = $1 AND sku IN (%s)",
|
|
strings.Join(placeholders, ","),
|
|
)
|
|
rows, err := d.Query(query, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var sku string
|
|
rows.Scan(&sku)
|
|
result[sku] = true
|
|
}
|
|
return result, rows.Err()
|
|
}
|
|
|
|
func ListFavorites(d *sql.DB, userID string) ([]model.Favorite, error) {
|
|
rows, err := d.Query(`
|
|
SELECT id, user_id, sku, brand, series_name, style_name,
|
|
main_image_url, category, material, color_tone, price_per_sqft, created_at
|
|
FROM favorites WHERE user_id = $1 ORDER BY created_at DESC
|
|
`, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []model.Favorite
|
|
for rows.Next() {
|
|
var f model.Favorite
|
|
if err := rows.Scan(&f.ID, &f.UserID, &f.SKU, &f.Brand, &f.SeriesName, &f.StyleName,
|
|
&f.MainImageURL, &f.Category, &f.Material, &f.ColorTone, &f.PricePerSqft, &f.CreatedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, f)
|
|
}
|
|
return out, rows.Err()
|
|
}
|