public async Task <IActionResult> Execute(JObject jObj, string subject, string commonId)
        {
            if (jObj == null)
            {
                throw new ArgumentNullException(nameof(jObj));
            }

            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new ArgumentNullException(nameof(subject));
            }


            AddDiscountCommand command = null;

            try
            {
                command = _requestBuilder.GetAddDiscountCommand(jObj);
            }
            catch (ArgumentException ex)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Request, ex.Message);
                return(_controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error));
            }


            var validationResult = await _validator.Validate(command, subject);

            if (!validationResult.IsValid)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Request, validationResult.Message);
                return(_controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error));
            }

            command.CommonId = commonId;
            command.Subject  = subject;
            _commandSender.Send(command);
            return(new OkResult());
        }
Beispiel #2
0
        public async Task <AddDiscountValidationResult> Validate(AddDiscountCommand command, string subject)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            if (command.ProductIds == null || !command.ProductIds.Any())
            {
                return(new AddDiscountValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.DiscountNames.ProductIds)));
            }

            if (command.Value <= 0)
            {
                return(new AddDiscountValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.DiscountNames.Value)));
            }

            if ((command.Validity == DiscountAggregateValidities.Counter || command.Validity == DiscountAggregateValidities.Both) && command.Counter <= 0)
            {
                return(new AddDiscountValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.DiscountNames.Counter)));
            }

            if ((command.Validity == DiscountAggregateValidities.Timer || command.Validity == DiscountAggregateValidities.Both))
            {
                if (command.StartDateTime.Equals(default(DateTime)))
                {
                    return(new AddDiscountValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.DiscountNames.StartDateTime)));
                }

                if (command.EndDateTime.Equals(default(DateTime)))
                {
                    return(new AddDiscountValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.DiscountNames.EndDateTime)));
                }

                if (command.StartDateTime >= command.EndDateTime)
                {
                    return(new AddDiscountValidationResult(ErrorDescriptions.TheStartDateMustBeInferiorToEndDate));
                }
            }

            var products = await _productRepository.Search(new SearchProductsParameter
            {
                ProductIds = command.ProductIds
            });

            if (products.Content == null || products.Content.Count() != command.ProductIds.Count())
            {
                return(new AddDiscountValidationResult(ErrorDescriptions.SomeProductsDontExist));
            }

            if ((command.Validity == DiscountAggregateValidities.Counter || command.Validity == DiscountAggregateValidities.Both) &&
                command.PromotionType == DiscountAggregatePromotions.FixedAmount && products.Content.Any(p => p.Price <= command.Value))
            {
                return(new AddDiscountValidationResult(ErrorDescriptions.TheProductPriceCannotBeInferiorToTheDiscount));
            }

            var shopIds = products.Content.Select(p => p.ShopId).Distinct();

            if (shopIds.Count() != 1)
            {
                return(new AddDiscountValidationResult(ErrorDescriptions.TheProductsShouldBelongsToTheSameShop));
            }

            var shop = await _shopRepository.Get(shopIds.First());

            if (shop.Subject != subject)
            {
                return(new AddDiscountValidationResult(ErrorDescriptions.TheDiscountCannotBeAddedByYou));
            }

            return(new AddDiscountValidationResult());
        }