public void ShouldHaveErrorWhenDescrptionIsNull()
        {
            // Arrange
            var command = new CreateProductOptionCommand(Guid.NewGuid(), "name", null);

            // Act
            var result = validator.Validate(command);

            // Assert
            Assert.IsFalse(result.IsValid);
            Assert.IsTrue(result.Errors.Any(e => e.ErrorMessage == "Description is required."));
        }
        public void ShouldHaveErrorWhenProductIdIsNull()
        {
            // Arrange
            var command = new CreateProductOptionCommand(Guid.Empty, "name", "desc");

            // Act
            var result = validator.Validate(command);

            // Assert
            Assert.IsFalse(result.IsValid);
            Assert.IsTrue(result.Errors.Any(e => e.ErrorMessage == "Product Id is required."));
        }
        public async Task <CommandResponseDto> CreateProductOption(CreateProductOptionCommand command)
        {
            try
            {
                await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                return(CommandResponseDto.Fail(GetErrorMessage(ex)));
            }

            return(CommandResponseDto.Success);
        }
Example #4
0
        public async Task <IHttpActionResult> CreateOption(Guid productId, ProductOptionRequestDto requestDto)
        {
            var command = new CreateProductOptionCommand(productId, requestDto.Name, requestDto.Description);

            var result = await _productOptionService.CreateProductOption(command);

            if (!result.IsSuccess)
            {
                return(BadRequest(result.FailureReason));
            }

            return(Ok());
        }
Example #5
0
        public async Task <IActionResult> CreateProductOption(Guid id, [FromBody] CreateProductOptionCommand command)
        {
            try
            {
                if (id != command.ProductId)
                {
                    return(BadRequest("Product id in route not match product id in payload"));
                }

                return(Ok(await Mediator.Send(command)));
            }
            catch (NotFoundException e)
            {
                return(NotFound(e.Message));
            }
        }
        public ProductOption(Guid id, string name, string?description) : base(id)
        {
            var command = new CreateProductOptionCommand
            {
                ProductOptionId = id,
                Name            = name,
                Description     = description
            };

            var validator = new CreateProductOptionCommandValidator();

            validator.ValidateAndThrow(command);

            Name        = command.Name;
            Description = command.Description;
        }
Example #7
0
        public IHttpActionResult CreateOption(Guid productId, CreateProductOptionCommand command)
        {
            ICommandHandler <CreateProductOptionCommand> commandHandler = CommandHandlerFactory.Create <CreateProductOptionCommand>();

            command.ProductId = productId;
            ICommandResult commandResult = commandHandler.Execute(command);

            if (commandResult.IsCompleted)
            {
                return(Created("", (commandResult as CommandModelResult <ProductOption>).Model));
            }
            else
            {
                return(Content(HttpStatusCode.BadRequest, commandResult.ErrorMessages));
            }
        }