public double CalculatePrice(ProductAggregate product, DiscountAggregate discount) { if (product == null) { throw new ArgumentNullException(nameof(product)); } if (discount == null) { throw new ArgumentNullException(nameof(discount)); } double result = product.Price; switch (discount.PromotionType) { case DiscountAggregatePromotions.FixedAmount: result -= discount.Value; break; case DiscountAggregatePromotions.Percentage: result = result - ((result * discount.Value) / 100); break; } return(result); }
public void Enrich(IHalResponseBuilder halResponseBuilder, DiscountAggregate discountAggregate) { if (halResponseBuilder == null) { throw new ArgumentNullException(nameof(halResponseBuilder)); } if (discountAggregate == null) { throw new ArgumentNullException(nameof(discountAggregate)); } halResponseBuilder.AddEmbedded(e => e.AddObject(_responseBuilder.GetDiscount(discountAggregate))); }
public async Task Handle(AddDiscountCommand message) { if (message == null) { throw new ArgumentNullException(nameof(message)); } var products = await _productRepository.Search(new SearchProductsParameter { ProductIds = message.ProductIds }); var record = new DiscountAggregate { Code = Guid.NewGuid().ToString(), Value = message.Value, Counter = message.Counter, CreateDateTime = message.CreateDateTime, EndDateTime = message.EndDateTime, Id = Guid.NewGuid().ToString(), PromotionType = message.PromotionType, StartDateTime = message.StartDateTime, UpdateDateTime = message.UpdateDateTime, Validity = message.Validity, Subject = message.Subject, IsActive = true, IsPrivate = message.IsPrivate }; record.Products = products.Content.Select(p => new DiscountProductAggregate { MoneySaved = _priceCalculator.CalculatePrice(p, record), ProductId = p.Id }); await _discountRepository.Insert(record); _eventPublisher.Publish(new DiscountAddedEvent { Id = record.Id, Seller = message.Subject, CommonId = message.CommonId }); }
public async Task <bool> Update(DiscountAggregate discountAggregate) { if (discountAggregate == null) { throw new ArgumentNullException(nameof(discountAggregate)); } using (var transaction = await _context.Database.BeginTransactionAsync().ConfigureAwait(false)) { try { var discount = await _context.Discounts.FirstOrDefaultAsync(d => d.Id == discountAggregate.Id); if (discount == null) { transaction.Rollback(); return(false); } discount.Counter = discountAggregate.Counter; discount.EndDateTime = discountAggregate.EndDateTime; discount.IsActive = discountAggregate.IsActive; discount.IsPrivate = discountAggregate.IsPrivate; discount.PromotionType = (int)discountAggregate.PromotionType; discount.StartDateTime = discountAggregate.StartDateTime; discount.Subject = discountAggregate.Subject; discount.UpdateDateTime = discountAggregate.UpdateDateTime; discount.Validity = (int)discountAggregate.Validity; discount.Value = discountAggregate.Value; await _context.SaveChangesAsync().ConfigureAwait(false); transaction.Commit(); return(true); } catch { transaction.Rollback(); return(false); } } }
public static Discount ToModel(this DiscountAggregate aggregate) { if (aggregate == null) { throw new ArgumentNullException(nameof(aggregate)); } return(new Discount { Code = aggregate.Code, Counter = aggregate.Counter, CreateDateTime = aggregate.CreateDateTime, EndDateTime = aggregate.EndDateTime, Id = aggregate.Id, PromotionType = (int)aggregate.PromotionType, StartDateTime = aggregate.StartDateTime, UpdateDateTime = aggregate.UpdateDateTime, Validity = (int)aggregate.Validity, Subject = aggregate.Subject }); }
public async Task <bool> Insert(DiscountAggregate discountAggregate) { if (discountAggregate == null) { throw new ArgumentNullException(nameof(discountAggregate)); } using (var transaction = await _context.Database.BeginTransactionAsync().ConfigureAwait(false)) { try { var record = discountAggregate.ToModel(); if (discountAggregate.Products != null) { var productDiscounts = discountAggregate.Products.Select(p => new ProductDiscount { Id = Guid.NewGuid().ToString(), DiscountId = record.Id, ProductId = p.ProductId, MoneySaved = p.MoneySaved }).ToList(); record.Products = productDiscounts; } _context.Discounts.Add(record); await _context.SaveChangesAsync().ConfigureAwait(false); transaction.Commit(); return(true); } catch { transaction.Rollback(); return(false); } } }