public async Task <AddProductToPercentDiscountCommandResponse> Handle(AddProductToPercentDiscountCommand command) { var product = await _productRepository.AsQuery() .SingleOrDefaultAsync(p => p.Id == command.ProductId); if (product == null) { throw new DomainException("محصولی یافت نشد"); } var discount = await _percentRepository.AsQuery() .SingleOrDefaultAsync(p => p.Id == command.PercentDiscount); if (discount == null) { throw new DomainException("تخفیف یافت نشد"); } if (discount.ProductDiscounts.Count >= discount.MaxProductCount) { throw new DomainException("حداکثر تعداد محصول در این تخفیف تکمیل شده است"); } if (discount.ProductDiscounts.Any(p => p.Product.Id == command.ProductId)) { throw new DomainException("این محصول در این تخفیف موجود می باشد"); } var userInfo = new UserInfo(command.UserInfoCommand.UserId, command.UserInfoCommand.FirstName, command.UserInfoCommand.LastName); var productPercentDiscounts = new ProductDiscount(Guid.NewGuid(), product, userInfo); DomainEventDispatcher.Raise(new CreatePercentDiscountEvent(discount.Id, command.ProductId, discount.Title, userInfo, discount.FromDate, discount.ToDate, discount.Percent, discount.FromTime, discount.ToTime, DiscountType.PercentDiscount)); discount.ProductDiscounts.Add(productPercentDiscounts); return(new AddProductToPercentDiscountCommandResponse()); }
public async Task <IHttpActionResult> Post(AddProductToPercentDiscountCommand command) { command.UserInfoCommand = new UserInfoCommandItem(UserId, FirstName, LastName); var response = await Bus.Send <AddProductToPercentDiscountCommand, AddProductToPercentDiscountCommandResponse>( command); return(Ok(response)); }