public async Task Handle_GivenInvalidData_ThrowsException()
        {
            // Arrange
            var animal = new AnimalDTO
            {
                Id        = 99,
                UserId    = "QWERTY1234567890",
                Kind      = "Kind_new_value",
                Breed     = "Breed_new_value",
                Gender    = GenderTypes.Male,
                Passport  = "1234567890QWERTY_new_value",
                BirthDate = new DateTime(2001, 01, 01),
                Nickname  = "Nickname_new_value",
                Features  = "Features_new_value",
                IsPublic  = true
            };

            var command = new UpdateAnimalCommand
            {
                Model = animal
            };

            // Act
            var handler = new UpdateAnimalCommand.UpdateAnimalCommandHandler(Context);

            // Assert
            await Should.ThrowAsync <NotFoundException>(() => handler.Handle(command, CancellationToken.None));
        }
예제 #2
0
        public async Task ShouldUpdateTodoItem()
        {
            var userId = await RunAsDefaultUserAsync();

            var createAnimalId = await SendAsync(new CreateAnimalCommand
            {
                Nombre = "KonanUpdate",
                Edad   = 4,
                Genero = "Masculino",
                Raza   = "Perro",
                Comida = "Pepas"
            });

            var command = new UpdateAnimalCommand
            {
                Id     = createAnimalId,
                Nombre = "FiruailsUpdate"
            };

            await SendAsync(command);

            var item = await FindAsync <AnimalEntity>(createAnimalId);

            item.Nombre.Should().Be(command.Nombre);
            item.LastModifiedBy.Should().NotBeNull();
            item.LastModifiedBy.Should().Be(userId);
            item.LastModified.Should().NotBeNull();
            item.LastModified.Should().BeCloseTo(DateTime.Now, 1000);
        }
예제 #3
0
        public async Task <IActionResult> Update([FromBody] UpdateAnimalCommand updateAnimal)
        {
            var result = await Mediator.Send(updateAnimal);

            if (result.Success)
            {
                return(Ok(result.Message));
            }
            return(BadRequest(result.Message));
        }
예제 #4
0
        public async Task<ActionResult> Update(int id, UpdateAnimalCommand command)
        {
            if (id != command.Id)
            {
                return BadRequest();
            }

            await Mediator.Send(command);

            return NoContent();
        }
예제 #5
0
        public void ShouldRequireValidTodoItemId()
        {
            var command = new UpdateAnimalCommand
            {
                Id     = 99,
                Nombre = "Yanko Dog"
            };

            FluentActions.Invoking(() =>
                                   SendAsync(command)).Should().Throw <NotFoundException>();
        }
예제 #6
0
        public async Task <IActionResult> Edit(AnimalViewModel model)
        {
            model = model ?? throw new ArgumentNullException(nameof(model));

            if (ModelState.IsValid)
            {
                var userId = await _identityService.GetUserIdByNameAsync(User.Identity.Name);

                // Создание команды для обновления животного
                var animalDTO = new AnimalDTO
                {
                    Id       = model.Id,
                    UserId   = userId,
                    Nickname = model.Nickname,
                    Passport = model.Passport,
                    Kind     = model.Kind,
                    Breed    = model.Breed,
                    Features = model.Features,
                    IsPublic = model.IsPublic
                };

                var animalCommand = new UpdateAnimalCommand
                {
                    Model = animalDTO
                };

                try
                {
                    await _mediator.Send(animalCommand);
                }
                catch (RequestValidationException failures)
                {
                    foreach (var error in failures.Failures)
                    {
                        ModelState.AddModelError(string.Empty, error.Value[0]);
                    }

                    return(View(model));
                }

                _logger.LogInformation($"{User.Identity.Name} successfully edited animal with id: {model.Id}.");

                return(RedirectToAction("Index", "Profile"));
            }

            return(View(model));
        }
        public async Task Handle_GivenValidData_ShouldUpdatePersistedAnimal()
        {
            // Arrange
            var animal = new AnimalDTO
            {
                Id        = 1,
                UserId    = "QWERTY1234567890_One",
                Kind      = "Kind_new_value",
                Breed     = "Breed_new_value",
                Gender    = GenderTypes.Male,
                Passport  = "1234567890QWERTY_new_value",
                BirthDate = new DateTime(1999, 01, 01),
                Nickname  = "Nickname_new_value",
                Features  = "Features_new_value",
                IsPublic  = true
            };

            var command = new UpdateAnimalCommand
            {
                Model = animal
            };

            // Act
            var handler = new UpdateAnimalCommand.UpdateAnimalCommandHandler(Context);

            await handler.Handle(command, CancellationToken.None);

            var entity = Context.Animals.Find(animal.Id);

            // Assert
            entity.ShouldNotBeNull();

            entity.UserId.ShouldBe(command.Model.UserId);
            entity.Kind.ShouldBe(command.Model.Kind);
            entity.Breed.ShouldBe(command.Model.Breed);
            entity.Passport.ShouldBe(command.Model.Passport);
            entity.Nickname.ShouldBe(command.Model.Nickname);
            entity.Features.ShouldBe(command.Model.Features);
            entity.IsPublic.ShouldBe(command.Model.IsPublic);

            entity.Gender.ShouldNotBe(command.Model.Gender);
            entity.BirthDate.ShouldNotBe(command.Model.BirthDate);
        }
예제 #8
0
        public async Task <UpdateAnimalResponse> Handle(UpdateAnimalRequest request, CancellationToken cancellationToken)
        {
            var animal  = _mapper.Map <Animal>(request);
            var command = new UpdateAnimalCommand()
            {
                Parameter = animal
            };
            var animalFromDb = await _commandExecutor.Executor(command);

            if (animal == null)
            {
                return(new UpdateAnimalResponse()
                {
                    Error = new ErrorModel(ErrorType.NotFound)
                });
            }

            return(new UpdateAnimalResponse()
            {
                Data = _mapper.Map <Domain.Models.Animal>(animalFromDb)
            });
        }
예제 #9
0
        public async Task Animal_UpdateCommand_Success()
        {
            //Arrange
            UpdateAnimalCommand command = new UpdateAnimalCommand();

            command.AnimalName = "test";

            _animalRepository.Setup(x => x.GetAsync(It.IsAny <Expression <Func <Animal, bool> > >()))
            .ReturnsAsync(new Animal()
            {
                AnimalId = 1, AnimalName = "deneme"
            });

            _animalRepository.Setup(x => x.Update(It.IsAny <Animal>()))
            .Returns(new Animal());

            UpdateAnimalCommandHandler handler = new UpdateAnimalCommandHandler(_animalRepository.Object);
            var x = await handler.Handle(command, new System.Threading.CancellationToken());

            _animalRepository.Verify(x => x.SaveChangesAsync());
            Assert.That(x.Success, Is.True);
            Assert.That(x.Message, Is.EqualTo(Messages.Updated));
        }
예제 #10
0
        public async Task <Either <ExceptionResponse, UpdateAnimalCommandResponse> > Handle(UpdateAnimalCommand request,
                                                                                            CancellationToken cancellationToken)
        {
            var commandParams = request.AnimalPayload;

            DeviceAssignedToAnimal deviceDto;

            Data.Domains.Animal.Animal existingAnimal = null;
            using (var context = _animalContextFactory.CreateDbContext(new string[0]))
            {
                deviceDto = await GetDeviceByCompositeKey(context, commandParams.DeviceIdentifier)
                            .Match(some => some, () => throw new InvalidOperationException("Device not found."));

                existingAnimal = deviceDto.Animal;
                if (existingAnimal == null)
                {
                    return(ExceptionResponse.With(
                               ErrorMessage: $"animal/{request.AnimalPayload.DeviceCompositeKey}",
                               HttpStatusCode: HttpStatusCode.NotFound));
                }

                existingAnimal.LastModifiedRequestID = request.RequestId;

                await context.SaveChangesAsync(request.RequestId);
            }
            return(new UpdateAnimalCommandResponse(existingAnimal));
        }