public async Task Delete(string promoId) { if (!await this.db.PromoDiscounts.AnyAsync(p => p.Id == promoId)) { throw new ArgumentException("Cannot find promo in DB"); } IList <ProductPromoDiscount> productPromoDiscounts = await this.db.ProductPromoDiscounts .Where(p => p.PromoDiscountId == promoId) .ToListAsync(); this.db.ProductPromoDiscounts.RemoveRange(productPromoDiscounts); await this.db.SaveChangesAsync(); PromoDiscount promo = await this.db.PromoDiscounts.FirstOrDefaultAsync(p => p.Id == promoId); this.db.PromoDiscounts.Remove(promo); await this.db.SaveChangesAsync(); }
public async Task Edit(string promoId, PromoDiscountCreateModel data) { if (data.Discount < 0 || data.Discount > 100) { throw new ArgumentException("Discount must be between 0 and 100"); } if (!await this.db.PromoDiscounts.AnyAsync(p => p.Id == promoId)) { throw new ArgumentException("Cannot find promo in DB"); } PromoDiscount promo = await this.db.PromoDiscounts.FirstOrDefaultAsync(p => p.Id == promoId); promo.Name = data.Name; promo.Discount = data.Discount; promo.EndDate = data.EndDate; promo.StartDate = data.StartDate; await this.db.SaveChangesAsync(); }
public async Task <string> Create(PromoDiscountCreateModel data) { if (data.Discount < 0 || data.Discount > 100) { throw new ArgumentException("Discount must be between 0 and 100"); } PromoDiscount discount = new PromoDiscount { Name = data.Name, Discount = data.Discount, StartDate = data.StartDate, EndDate = data.EndDate }; await this.db.PromoDiscounts .AddAsync(discount); await this.db.SaveChangesAsync(); return(discount.Id); }