public async Task <ProductOption> CreateProductOptionAsync(Guid productId, CreateProductOptionDto createProductOptionDto)
        {
            if (await _productRepository.GetByIdAsync(productId) == null)
            {
                throw new NotFoundException(Constants.Constants.ProductNotFound, HttpStatusCode.NotFound.ToString());
            }

            var validationResults = createProductOptionDto.Validate();
            var validations       = validationResults.ToList();

            if (validations.Any())
            {
                throw new ValidationException(validations);
            }

            var productOption = new ProductOption
            {
                Id          = Guid.NewGuid(),
                Description = createProductOptionDto.Description,
                Name        = createProductOptionDto.Name,
                ProductId   = productId
            };

            return(await _repository.CreateAsync(productOption));
        }
        public async Task <IActionResult> CreateOption(Guid productId, [FromBody] CreateProductOptionDto option)
        {
            var createdOption = await _productOptionService.CreateProductOptionAsync(productId, option);

            var productOptionDto = _mapper.Map <ProductOption, ProductOptionDto>(createdOption);

            return(CreatedAtRoute("GetProductOption", new { productId = createdOption.ProductId, id = createdOption.Id }, productOptionDto));
        }
Esempio n. 3
0
        public static IEnumerable <ValidationResult> Validate(this CreateProductOptionDto createProductOptionDto)
        {
            if (string.IsNullOrWhiteSpace(createProductOptionDto.Name) ||
                createProductOptionDto.Name.StartsWith("-") ||
                createProductOptionDto.Name.EndsWith("-"))
            {
                yield return(new ValidationResult(nameof(createProductOptionDto.Name), "Invalid product option name", Constants.Constants.ModelInvalidError));
            }

            if (string.IsNullOrWhiteSpace(createProductOptionDto.Description))
            {
                yield return(new ValidationResult(nameof(createProductOptionDto.Description), "Invalid option description", Constants.Constants.ModelInvalidError));
            }
        }