fix campaign rules
This commit is contained in:
@@ -30,24 +30,7 @@ func NewCampaignRepository(db *gorm.DB) CampaignRepository {
|
||||
}
|
||||
|
||||
func (r *campaignRepository) Create(ctx context.Context, campaign *entities.Campaign) error {
|
||||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
// Create campaign first
|
||||
if err := tx.Create(campaign).Error; err != nil {
|
||||
return fmt.Errorf("failed to create campaign: %w", err)
|
||||
}
|
||||
|
||||
// Create campaign rules
|
||||
if len(campaign.Rules) > 0 {
|
||||
for i := range campaign.Rules {
|
||||
campaign.Rules[i].CampaignID = campaign.ID
|
||||
}
|
||||
if err := tx.Create(&campaign.Rules).Error; err != nil {
|
||||
return fmt.Errorf("failed to create campaign rules: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
return r.db.WithContext(ctx).Create(campaign).Error
|
||||
}
|
||||
|
||||
func (r *campaignRepository) GetByID(ctx context.Context, id string) (*entities.Campaign, error) {
|
||||
@@ -114,29 +97,7 @@ func (r *campaignRepository) List(ctx context.Context, req *entities.ListCampaig
|
||||
}
|
||||
|
||||
func (r *campaignRepository) Update(ctx context.Context, campaign *entities.Campaign) error {
|
||||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
// Update campaign
|
||||
if err := tx.Save(campaign).Error; err != nil {
|
||||
return fmt.Errorf("failed to update campaign: %w", err)
|
||||
}
|
||||
|
||||
// Delete existing rules
|
||||
if err := tx.Where("campaign_id = ?", campaign.ID).Delete(&entities.CampaignRule{}).Error; err != nil {
|
||||
return fmt.Errorf("failed to delete existing campaign rules: %w", err)
|
||||
}
|
||||
|
||||
// Create new rules
|
||||
if len(campaign.Rules) > 0 {
|
||||
for i := range campaign.Rules {
|
||||
campaign.Rules[i].CampaignID = campaign.ID
|
||||
}
|
||||
if err := tx.Create(&campaign.Rules).Error; err != nil {
|
||||
return fmt.Errorf("failed to create campaign rules: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
return r.db.WithContext(ctx).Save(campaign).Error
|
||||
}
|
||||
|
||||
func (r *campaignRepository) Delete(ctx context.Context, id string) error {
|
||||
|
||||
Reference in New Issue
Block a user