public async Task <int> CreateProductCatalogAsync(CreateProductCatalogCommand command, CancellationToken cancellationToken)
        {
            var productCatalog = new ProductCatalog
            {
                Code  = command.Code,
                Name  = command.Name,
                Photo = command.Photo,
                Price = command.Price
            };

            _dbContext.ProductCatalogs.Add(productCatalog);

            try
            {
                await _dbContext.SaveChangesAsync(cancellationToken);
            }
            //The table can have more than one uniqe index
            //in this case we must know where the exception cames from.
            //catch (Exception ex) when (ex.Message.Contains(ProductCatalogConstants.ProductCatalogCodeIndexName)
            //    || (ex.InnerException != null && ex.InnerException.Message.Contains(ProductCatalogConstants.ProductCatalogCodeIndexName)))
            //{
            //    throw new CodeExistsException(nameof(ProductCatalog), command.Code);
            //}

            catch (DbUpdateException ex)
            {
                throw new CodeExistsException(nameof(ProductCatalog), command.Code);
            }
            catch (Exception ex)
            {
                throw;
            }

            return(productCatalog.Id);
        }
예제 #2
0
        public void Validate_InvalidInput_ShouldHaveValidationError()
        {
            var createProductCatalogCommand = new CreateProductCatalogCommand();

            _validationRules.ShouldHaveValidationErrorFor(x => x.Code, createProductCatalogCommand);
            _validationRules.ShouldHaveValidationErrorFor(x => x.Name, createProductCatalogCommand);
            _validationRules.ShouldHaveValidationErrorFor(x => x.Price, createProductCatalogCommand);
        }
예제 #3
0
        public void Validate_InvalidPrice_ShouldHaveValidationError(decimal price)
        {
            var createProductCatalogCommand = new CreateProductCatalogCommand()
            {
                Price = price
            };

            _validationRules.ShouldHaveValidationErrorFor(x => x.Price, createProductCatalogCommand);
        }
        private StringContent GenerateValidDummyCommandContent()
        {
            var price   = (decimal)(new Random().NextDouble() * 100d);
            var command = new CreateProductCatalogCommand
            {
                Code = Guid.NewGuid().ToString(), Name = "Test", Photo = "photo", Price = price
            };

            return(Utilities.GetRequestContent(command));
        }
예제 #5
0
        public void Validate_ValidInput_ShouldPassTheTest()
        {
            var command = new CreateProductCatalogCommand()
            {
                Code = "codeTest", Name = "nameTest", Price = 5.5m, Photo = "photoTest"
            };

            _validationRules.ShouldNotHaveValidationErrorFor(x => x.Code, command);
            _validationRules.ShouldNotHaveValidationErrorFor(x => x.Name, command);
            _validationRules.ShouldNotHaveValidationErrorFor(x => x.Price, command);
            _validationRules.ShouldNotHaveValidationErrorFor(x => x.Photo, command);
        }
 public async Task<ActionResult<int>> Post([FromBody] CreateProductCatalogCommand command)
 {
     var newProductCatalogId = await _mediator.Send(command);
     return CreatedAtAction(nameof(Get), newProductCatalogId);
 }