public async Task <IActionResult> PostPromotion([FromBody] AddPromotionDTO promotion) { if (_context.Promotion.FirstOrDefault(p => p.PromotionCode == promotion.PromotionCode) != null) { return(StatusCode(409)); } var promo = new Promotion { PromotionName = promotion.PromotionName, PromotionStatus = promotion.PromotionStatus, PromotionDescription = promotion.PromotionDescription, PromotionCode = promotion.PromotionCode, TargetApply = promotion.TargetApply, StartDate = promotion.StartDate, EndDate = promotion.EndDate, PercentOff = promotion.PercentOff }; _context.Promotion.Add(promo); if (await _context.SaveChangesAsync() > 0) { if (promotion.TargetApply == "Brand") { var promoBrand = new PromotionBrand { BrandId = promotion.PromotionBrand, PromotionId = promo.PromotionId }; _context.PromotionBrand.Add(promoBrand); if (await _context.SaveChangesAsync() > 0) { return(StatusCode(201)); } } if (promotion.TargetApply == "Category") { var promoCategory = new PromotionCategory() { CategoryId = promotion.PromotionCategory, PromotionId = promo.PromotionId }; _context.PromotionCategory.Add(promoCategory); if (await _context.SaveChangesAsync() > 0) { return(StatusCode(201)); } } return(StatusCode(201)); } return(StatusCode(424)); }
public async void AddPromotionToProduct() { AddPromotionDTO addPromotionDTO = new AddPromotionDTO(); addPromotionDTO.Amount = decimal.Parse(DiscountAmount); var results = new List <ValidationResult>(); bool isValid = Validator.TryValidateObject( addPromotionDTO, new ValidationContext(addPromotionDTO, null, null), results, false); if (isValid) { var en = double.Parse(End.ToString()); var dateend = DateTime.Now.AddMinutes(en); Promotion = new Promotion { End = dateend, Start = DateTime.Now, Amount = decimal.Parse(DiscountAmount), RequiredAmount = RequiredAmount, PromotionType = PromotionType }; var promotion = await _catalogDataService.AddPromotionToProduct(Product, Promotion); if (promotion != null) { ProductDetailsViewModel.Promotions.Add(new PromotionModel(promotion)); try { await _notificationService.SendPromotionNotification(Product, promotion); } catch (Exception e) { } } } }