Ejemplo n.º 1
0
        public IActionResult Get(Guid id)
        {
            var category = categoryReader.GetByIDs(new[] { id }).SingleOrDefault();

            if (category == null)
            {
                return(NotFound());
            }

            return(Ok(category));
        }
        public string Print(Cart cart)
        {
            if (cart.IsEmpty)
            {
                return("cart is empty!");
            }

            var lineItems          = cart.LineItems.OrderBy(l => l.Product.CategoryID).ToList();
            var distinctCategories = lineItems.Select(l => l.Product.CategoryID).Distinct().ToList();
            var categories         = categoryReader.GetByIDs(distinctCategories)
                                     .ToDictionary(c => c.ID, c => c.Title);

            var sb = new StringBuilder();

            sb.AppendLine(LineItemHeader);
            lineItems.ForEach(l =>
            {
                var line = string.Format(
                    LineItemFormat,
                    categories[l.Product.CategoryID],
                    l.Product.Title,
                    l.Quantity,
                    l.Product.Price,
                    l.TotalAmountAfterDiscounts,
                    l.TotalDiscount);

                sb.AppendLine(line);
            });

            sb.AppendLine(string.Format(CartFormat, cart.TotalAmountAfterDiscounts, cart.DeliveryCost));

            return(sb.ToString());
        }
Ejemplo n.º 3
0
        protected override Task Handle(CreateProductCommand request, CancellationToken cancellationToken)
        {
            var category = categoryReader.GetByIDs(new List <Guid> {
                request.CategoryID
            }).SingleOrDefault();

            if (category == null)
            {
                throw new CategoryNotFoundException(request.CategoryID);
            }

            var product = new Product(request.ID, request.Title, request.Price, request.CategoryID);

            repository.Add(product);

            return(Task.CompletedTask);
        }
        protected override async Task Handle(CreateCampaignCommand request, CancellationToken cancellationToken)
        {
            var category = categoryReader.GetByIDs(new List <Guid> {
                request.CategoryID
            }).SingleOrDefault();

            if (category == null)
            {
                throw new CategoryNotFoundException(request.CategoryID);
            }

            var campaign = new Campaign(request.ID, request.CategoryID, request.MinimumItemCount, request.Type, request.Rate);

            repository.Add(campaign);

            await mediator.Publish(new CampaignCreatedEvent { ID = campaign.ID }, cancellationToken);
        }
        protected override Task Handle(CreateCategoryCommand request, CancellationToken cancellationToken)
        {
            if (request.ParentCategoryID.HasValue)
            {
                var parent = categoryReader.GetByIDs(new List <Guid> {
                    request.ParentCategoryID.Value
                }).SingleOrDefault();
                if (parent == null)
                {
                    throw new CategoryNotFoundException(request.ParentCategoryID.Value);
                }
            }

            var category = new Category(request.ID, request.ParentCategoryID, request.Title);

            repository.Add(category);

            return(Task.CompletedTask);
        }