public async Task <bool> Handle(UpdateFriendCommand request, CancellationToken cancellationToken) { if (!request.IsValid()) { NotifyValidationErrors(request); return(false); } else if (!_friendRepository.IsExistFriend(request.Id)) { await _Bus.RaiseEvent(new DomainNotification(request.MessageType, "Could not update friend. The informed Id friend does not exist")); return(false); } else { var friend = await _friendRepository.GetById(request.Id); _Mapper.Map <UpdateFriendCommand, Friend>(request, friend); _friendRepository.Update(friend); return(Commit()); } }
public async Task ShouldUpdateFriend() { GenerateMock(); var friendMock = new Mock <Friend>(); friendMock.Setup(x => x.Update(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <byte[]>())); _friendRepositoryMock .Setup(x => x.FindAsync(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(friendMock.Object); _friendRepositoryMock.Setup(x => x.Update(It.IsAny <Friend>())); _friendRepositoryMock.Setup(x => x.UnitOfWork.SaveAsync(It.IsAny <CancellationToken>())); var handler = GetHandler(); var ver = new byte[] { 0x02 }; var command = new UpdateFriendCommand(Guid.NewGuid(), "name", "nickname", "email", ver); await handler.Handle(command, default); friendMock.Verify(x => x.Update( It.Is <string>(f => f.Equals("name")), It.Is <string>(f => f.Equals("nickname")), It.Is <string>(f => f.Equals("email")), It.Is <byte[]>(f => f.Equals(ver)) )); _friendRepositoryMock.Verify(x => x.Update(It.Is <Friend>(f => f.Equals(friendMock.Object)))); _friendRepositoryMock.Verify(x => x.UnitOfWork.SaveAsync(It.IsAny <CancellationToken>())); }
public async Task <IActionResult> Put(Guid id, [FromBody] UpdateFriendCommand command) { if (id == default || id != command.Id) { return(BadRequest()); } await _mediator.Send(command); return(NoContent()); }
public async Task <ActionResult <Object> > Put([FromBody] UpdateFriendCommand Friend) { var updateResult = await _updateFriendCommandHandler.HandleAsync(Friend); var result = new List <Object>(); result.Add(new { Success = updateResult }); return(result); }
public void ShouldValidateWhenUpdateCommandIsValid() { var command = new UpdateFriendCommand() { Id = Guid.NewGuid(), Email = "*****@*****.**", FirstName = "Raphael", LastName = "Teste 001", Phone = "4499845466" }; Assert.True(command.Valid()); }
public async Task ShouldUpdateFriend() { var cmd = new UpdateFriendCommand(_entity.Id, "Bruce Wayne", "Wayne", "*****@*****.**", _entity.Ver); await SendAsync(cmd); var entity = await FindAsync <IFriendRepository, Friend>(_entity.Id); entity.Should().NotBeNull(); entity.Id.Should().Be(_entity.Id); entity.Name.Should().Be("Bruce Wayne"); entity.NickName.Should().Be("Wayne"); entity.Email.Should().Be("*****@*****.**"); }
public void ShouldUpdateFriendWhenCommandIsValid() { var command = new UpdateFriendCommand() { FirstName = "Armando", LastName = "Fagundes", Email = "*****@*****.**", Phone = "11888774545", Id = Guid.NewGuid() }; var result = _handler.Handle(command); Assert.NotEqual(null, result); Assert.True(_handler.Valid); }
public void ShouldThrowMessageFriendNotFound() { GenerateMock(); _friendRepositoryMock .Setup(x => x.FindAsync(It.IsAny <Guid>(), It.IsAny <CancellationToken>())); var handler = GetHandler(); var command = new UpdateFriendCommand(Guid.NewGuid(), "teste", "unit", "test@test", null); FluentActions.Invoking(async() => await handler.Handle(command, default)) .Should() .Throw <NotFoundException>().WithMessage("Friend not found!"); _friendRepositoryMock .Verify(x => x.FindAsync(It.Is <Guid>(f => f.Equals(command.Id)), It.IsAny <CancellationToken>())); }
public ICommandResult Handle(UpdateFriendCommand command) { var friend = _repository.Get(command.Id); var name = new Name(command.FirstName, command.LastName); var email = new Email(command.Email); friend.Alter(name, email, command.Phone); AddNotifications(name.Notifications); AddNotifications(email.Notifications); AddNotifications(friend.Notifications); if (Invalid) { return(ErrorResult()); } _repository.Update(friend); return(SucessResult(friend.Id, name.ToString(), email.Address)); }
public async Task <ICommandResult> Put([FromBody] UpdateFriendCommand command) { var result = _handler.Handle(command); return(await Response(result)); }
public async Task <IActionResult> UpdateFriend([FromForm] UpdateFriendCommand command) { return(Ok(await Mediator.Send(command))); }