public IActionResult PutManufacturer(Guid manufacturerId, [FromBody] PutManufactuer model)
        {
            if (ModelState.IsValid)
            {
                var command = new UpdateManufacturer(manufacturerId, model.Name);
                commandBus.AddCommand(command);
                commandBus.InvokeCommandsQueue();

                return(GetManufacturer(command.Id));
            }

            return(BadRequest(ModelState));
        }
        public void ValidatorThrowsException_HandlerThrowsException()
        {
            var manufacturerId            = InsertManufacturerToDatabase();
            var updateManufacturerCommand = new UpdateManufacturer(manufacturerId, "UpdatedName");

            A.CallTo(() => updateManufacturerValidator.Validate(updateManufacturerCommand)).Throws <Exception>();

            Assert.ThrowsAny <Exception>(() => updateManufacturerHandler.Handle(updateManufacturerCommand));
            A.CallTo(() => updateManufacturerValidator.Validate(updateManufacturerCommand)).MustHaveHappened(Repeated.Exactly.Once);

            context.SaveChanges();
            var result = context.Manufacturer.FirstOrDefault(m => m.Id == updateManufacturerCommand.Id);

            Assert.NotNull(result);
            Assert.Equal(manufacturerId, result.Id);
            Assert.Equal(expectedManufacturerName, result.Name);
        }
        public void CommandValid_ManufacturerUpdated()
        {
            var manufacturerId            = InsertManufacturerToDatabase();
            var updateManufacturerCommand = new UpdateManufacturer(manufacturerId, "UpdatedName");

            A.CallTo(() => updateManufacturerValidator.Validate(updateManufacturerCommand)).DoesNothing();

            updateManufacturerHandler.Handle(updateManufacturerCommand);

            context.SaveChanges();
            var result = context.Manufacturer.FirstOrDefault(m => m.Id == updateManufacturerCommand.Id);

            A.CallTo(() => updateManufacturerValidator.Validate(updateManufacturerCommand)).MustHaveHappened(Repeated.Exactly.Once);
            Assert.NotNull(result);
            Assert.Equal(result.Id, updateManufacturerCommand.Id);
            Assert.Equal(result.Name, updateManufacturerCommand.Name);
        }
Ejemplo n.º 4
0
        public object Update(int id, UpdateManufacturer updateManufacturer, IMapper mapper)
        {
            var manufacturer = _context.Manufacturers.AsNoTracking()
                               .Where(p => p.Id == id)
                               .FirstOrDefault();

            if (manufacturer == null)
            {
                var error = new ErrorResponse();
                error.ErrorMessages.Add(MessageHelper.RecordToBeUpdatedNotFound);

                return(error);
            }
            else
            {
                var mappedManufacturer = mapper.Map(updateManufacturer, manufacturer);
                mappedManufacturer.DateUpdated = DateTime.Now;

                _context.Update(mappedManufacturer);
                _context.SaveChanges();

                return(new SuccessResponse());
            }
        }
Ejemplo n.º 5
0
        public IActionResult Update(int id, [FromBody] UpdateManufacturer manufacturer)
        {
            var response = _service.Update(id, manufacturer, _mapper);

            return(Ok(response));
        }