public IActionResult PostModelsCollection(Guid manufacturerId, [FromBody] IEnumerable <PostModelName> modelsCollection)
        {
            if (ModelState.IsValid)
            {
                if (!modelsCollection.Any())
                {
                    return(BadRequest("Empty collection"));
                }

                foreach (var model in modelsCollection)
                {
                    var command = new AddModelName(manufacturerId, model.ModelName);
                    commandBus.AddCommand(command);
                }
                commandBus.InvokeCommandsQueue();


                var result = GetModelDetailsByIdsData(manufacturerId, commandBus.CommandIds);

                return(CreatedAtRoute(
                           "GetModelsByIds",
                           new { ids = string.Join(",", commandBus.CommandIds) },
                           result));
            }
            else
            {
                return(BadRequest(modelsCollection));
            }
        }
        public void ValidatorThrowsException_HandlerThrowsException()
        {
            var manufacturerId      = InsertManufacturerToDatabase();
            var addModelNameCommand = new AddModelName(manufacturerId, expectedModelName);

            A.CallTo(() => addModelNameValidator.Validate(addModelNameCommand)).Throws <Exception>();

            Assert.ThrowsAny <Exception>(() => addModelNameHandler.Handle(addModelNameCommand));

            context.SaveChanges();
            var result = context.ModelName.FirstOrDefault(m => m.Id == addModelNameCommand.Id);

            A.CallTo(() => addModelNameValidator.Validate(addModelNameCommand)).MustHaveHappened(Repeated.Exactly.Once);
            Assert.Null(result);
        }
        public void CommandValid_ModelNameAdded()
        {
            var manufacturerId      = InsertManufacturerToDatabase();
            var addModelNameCommand = new AddModelName(manufacturerId, expectedModelName);

            A.CallTo(() => addModelNameValidator.Validate(addModelNameCommand)).DoesNothing();

            addModelNameHandler.Handle(addModelNameCommand);
            context.SaveChanges();

            var result = context.ModelName.FirstOrDefault(m => m.Id == addModelNameCommand.Id);

            A.CallTo(() => addModelNameValidator.Validate(addModelNameCommand)).MustHaveHappened(Repeated.Exactly.Once);
            Assert.NotNull(result);
            Assert.Equal(addModelNameCommand.Id, result.Id);
            Assert.Equal(expectedModelName, result.Name);
            Assert.Equal(manufacturerId, result.ManufacturerId);
        }
        public IActionResult PostModel(Guid manufacturerId, [FromBody] PostModelName model)
        {
            if (ModelState.IsValid)
            {
                var command = new AddModelName(manufacturerId, model.ModelName);
                commandBus.AddCommand(command);
                commandBus.InvokeCommandsQueue();

                var result = GetModelDetails(manufacturerId, command.Id);

                return(CreatedAtRoute(
                           "GetModel",
                           new { manufacturerId = manufacturerId, modelId = command.Id },
                           result
                           ));
            }

            return(BadRequest(ModelState));
        }