public IActionResult PostEnginesCollection([FromBody] IEnumerable <PostEngine> modelsCollection)
        {
            if (ModelState.IsValid)
            {
                if (!modelsCollection.Any())
                {
                    return(BadRequest("Empty collection"));
                }

                foreach (var model in modelsCollection)
                {
                    var command = new AddEngine(model.Name, model.Power, model.Torque, model.Cylinders, model.Displacement, model.FuelType);
                    commandBus.AddCommand(command);
                }

                commandBus.InvokeCommandsQueue();

                var result = GetEngineDetails(commandBus.CommandIds);
                return(CreatedAtRoute(
                           "GetEnginesByIds",
                           new { ids = string.Join(",", commandBus.CommandIds) },
                           result
                           ));
            }

            return(BadRequest(ModelState));
        }
示例#2
0
        public void ValidatorThrowsException_HandlerThrowsException()
        {
            var addEngineCommand = new AddEngine("Test", 10, 10, 4, 10, FuelType.Diesel);

            A.CallTo(() => addEngineValidator.Validate(addEngineCommand)).Throws <Exception>();

            Assert.ThrowsAny <Exception>(() => addEngineHandler.Handle(addEngineCommand));
            A.CallTo(() => addEngineValidator.Validate(addEngineCommand)).MustHaveHappened(Repeated.Exactly.Once);

            context.SaveChanges();
            var result = context.Engine.FirstOrDefault(e => e.Id == addEngineCommand.Id);

            Assert.Null(result);
        }
        public IActionResult PostEngine([FromBody] PostEngine model)
        {
            if (ModelState.IsValid)
            {
                var command = new AddEngine(model.Name, model.Power, model.Torque, model.Cylinders, model.Displacement, model.FuelType);
                commandBus.AddCommand(command);
                commandBus.InvokeCommandsQueue();

                var result = GetEngineDetails(command.Id);
                return(CreatedAtRoute(
                           "GetEngine",
                           new { engineId = command.Id },
                           result
                           ));
            }

            return(BadRequest(ModelState));
        }
示例#4
0
        public void CommandValid_EngineAdded()
        {
            var addEngineCommand = new AddEngine("Test", 10, 10, 4, 10, FuelType.Diesel);

            A.CallTo(() => addEngineValidator.Validate(addEngineCommand)).DoesNothing();

            addEngineHandler.Handle(addEngineCommand);

            context.SaveChanges();
            var result = context.Engine.FirstOrDefault(e => e.Id == addEngineCommand.Id);

            A.CallTo(() => addEngineValidator.Validate(addEngineCommand)).MustHaveHappened(Repeated.Exactly.Once);
            Assert.NotNull(result);
            Assert.Equal(result.Id, addEngineCommand.Id);
            Assert.Equal(result.Name, addEngineCommand.Name);
            Assert.Equal(result.Power, addEngineCommand.Power);
            Assert.Equal(result.Torque, addEngineCommand.Torque);
            Assert.Equal(result.Cylinders, addEngineCommand.Cylinders);
            Assert.Equal(result.Displacement, addEngineCommand.Displacement);
            Assert.Equal(result.FuelType, addEngineCommand.FuelType);
        }