Пример #1
0
        public override async Task <Result> Handle(NewProduct command, CancellationToken cancellationToken)
        {
            var title = Title.New(command.Title);

            if (title.Failed)
            {
                return(Fail(title.Message));
            }

            var product      = Product.New(title.Value, _policy);
            var priceTypeIds = command.Prices.Select(p => p.PriceTypeId).ToList();
            var priceTypes   = (await _priceTypeRepository.FindAsync(p => priceTypeIds.Contains(p.Id), cancellationToken))
                               .ToDictionary(p => p.Id);

            if (priceTypes.Count != priceTypeIds.Count)
            {
                return(Fail("Some of priceTypes not found"));
            }

            foreach (var priceItem in command.Prices)
            {
                var priceValue = Price.New(priceItem.Price, "$");
                if (priceValue.Failed)
                {
                    return(Fail(priceValue.Message));
                }

                product.Value.AddPrice(priceTypes[priceItem.PriceTypeId], priceValue.Value);
            }

            await _uow.SaveChangesAsync(cancellationToken);

            return(Ok());
        }
        public override async Task <Result> Handle(RemovePriceTypeCommand command, CancellationToken cancellationToken)
        {
            var priceType = await _repository.FindAsync(command.PriceTypeId, cancellationToken);

            if (!priceType.HasValue)
            {
                return(Fail($"PriceType with id:{command.Id} not found"));
            }

            _repository.Remove(priceType.Value);

            await _uow.SaveChangesAsync(cancellationToken);

            return(Ok());
        }