feat: 更新商城前后端代码
This commit is contained in:
@@ -240,6 +240,32 @@ func (s *ProductStore) IncrementView(id, fingerprint string) (models.Product, bo
|
||||
return rowToModel(row, nil), true, nil
|
||||
}
|
||||
|
||||
// IncrementViewCount atomically increments view_count by 1 and returns the
|
||||
// updated product (without codes). It is called by the handler when Redis-based
|
||||
// deduplication has already confirmed this is a new view, so no in-memory lock
|
||||
// is needed here.
|
||||
func (s *ProductStore) IncrementViewCount(id string) (models.Product, error) {
|
||||
if err := s.db.Model(&database.ProductRow{}).Where("id = ?", id).
|
||||
UpdateColumn("view_count", gorm.Expr("view_count + 1")).Error; err != nil {
|
||||
return models.Product{}, err
|
||||
}
|
||||
var row database.ProductRow
|
||||
if err := s.db.First(&row, "id = ?", id).Error; err != nil {
|
||||
return models.Product{}, fmt.Errorf("product not found")
|
||||
}
|
||||
return rowToModel(row, nil), nil
|
||||
}
|
||||
|
||||
// GetViewState returns the current product state without modifying any counter.
|
||||
// Used by the handler when a view is detected as duplicate via Redis.
|
||||
func (s *ProductStore) GetViewState(id string) (models.Product, error) {
|
||||
var row database.ProductRow
|
||||
if err := s.db.First(&row, "id = ?", id).Error; err != nil {
|
||||
return models.Product{}, fmt.Errorf("product not found")
|
||||
}
|
||||
return rowToModel(row, nil), nil
|
||||
}
|
||||
|
||||
func (s *ProductStore) Delete(id string) error {
|
||||
if err := s.db.Where("product_id = ?", id).Delete(&database.ProductCodeRow{}).Error; err != nil {
|
||||
return err
|
||||
@@ -272,7 +298,7 @@ func normalizeProduct(item models.Product) models.Product {
|
||||
item.VerificationURL = strings.TrimSpace(item.VerificationURL)
|
||||
item.Codes = sanitizeCodes(item.Codes)
|
||||
item.Quantity = len(item.Codes)
|
||||
if item.DeliveryMode == "" {
|
||||
if item.DeliveryMode == "" || item.DeliveryMode == "manual" {
|
||||
item.DeliveryMode = "auto"
|
||||
}
|
||||
return item
|
||||
|
||||
Reference in New Issue
Block a user