public async Task <GenericCommandResult <ClientEntity> > Update([FromServices] IClientHandler handler, [FromBody] UpdateClientCommand command) { var result = (GenericCommandResult <ClientEntity>) await handler.HandleAsync(command); return(result); }
public void Invalid_UpdateCommand_Should_Return_False() { var invalidUpdateCommand = new UpdateClientCommand(new Client()); _result = (GenericCommandResult)_handler.Handle(invalidUpdateCommand); Assert.AreEqual(false, _result.Success); }
public async Task <bool> Handle(UpdateClientCommand request, CancellationToken cancellationToken) { if (!request.IsValid()) { NotifyValidationErrors(request); return(false); } var savedClient = await _clientRepository.GetClient(request.OldClientId); if (savedClient == null) { await Bus.RaiseEvent(new DomainNotification("Client", "Client not found")); return(false); } var client = request.Client.ToEntity(); client.Id = savedClient.Id; await _clientRepository.UpdateWithChildrens(client); if (await Commit()) { await Bus.RaiseEvent(new ClientUpdatedEvent(request)); return(true); } return(false); }
public async Task <bool> Handle(UpdateClientCommand request, CancellationToken cancellationToken) { if (!request.IsValid()) { NotifyValidationErrors(request); return(false); } var clientId = request.OriginalClinetId != request.Client.ClientId ? request.OriginalClinetId : request.Client.ClientId; var client = await _clientRepository.FindByClientIdWithNoTrackingAsync(clientId); if (client == null) { await _bus.RaiseEvent(new DomainNotification("key_not_found", $"Client with ClientId {clientId} not found")); return(false); } var entity = request.Client.ToEntity(); entity.Id = client.Id; await _clientRepository.UpdateWithChildrensAsync(entity); if (Commit()) { await _bus.RaiseEvent(new ClientUpdatedEvent(request)); return(true); } return(false); }
public void Valid_UpdateCommand_Should_Return_False() { var validUpdateCommand = new UpdateClientCommand(_validClient); _result = (GenericCommandResult)_handler.Handle(validUpdateCommand); Assert.AreEqual(true, _result.Success); }
public async Task <IActionResult> Put([FromRoute] Guid id, [FromBody] UpdateClientCommand command) { command.Id = id; await _mediator.Send(command); return(Ok()); }
public async Task <bool> Handle(UpdateClientCommand message, CancellationToken cancellationToken) { var client = await _repository.GetById(message.Id) ?? throw new KeyNotFoundException(); message.AllowedScopes = message.AllowedScopes ?? new List <string>(); await client.UpdateSettings( _repository, message.ClientName, message.Description, message.ClientUri, message.LogoUri, message.RequireClientSecret, message.RequireConsent, message.AlwaysIncludeUserClaimsInIdToken, message.AllowAccessTokensViaBrowser, message.IdentityTokenLifetime, message.RedirectUris, message.AllowedCorsOrigins, message.AllowedScopes ); if (message.Enabled) { client.Enable(); } else { client.Disable(); } await _repository.SaveAsync(client); return(true); }
public void Update(UpdateClientCommand model) { MySqlCommand command = _context.CreateCommand(); command.CommandText = GetUpdateClientCommandText(); UpdateClientPopulateParameters(model, command); command.ExecuteNonQuery(); }
public async Task <ICommandResult> Put(Guid id, [FromBody] PutClientDto clientDto) { clientDto.Id = id; UpdateClientCommand cmd = clientDto; var commandResult = await _commandBus.Submit(cmd); return(commandResult); }
public async Task <IActionResult> Put(int id, [FromBody] UpdateClientDto client, CancellationToken cancellationToken) { var command = new UpdateClientCommand { Id = id, Client = client }; await Mediator.Send(command, cancellationToken); return(NoContent()); }
public void Shoud_have_error_when_name_is_empty() { var validator = new UpdateClientCommandValidator(); var model = new UpdateClientCommand { Name = string.Empty }; validator.Validate(model).IsValid.Should().BeFalse(); }
public async Task <ActionResult> UpdateClient([FromBody] ClientViewModel clientViewModel) { var command = new UpdateClientCommand(clientViewModel.Name, clientViewModel.Birthday, clientViewModel.Passport, clientViewModel.StreetAddress, clientViewModel.City, clientViewModel.State, clientViewModel.PostalCode, _user); await _mediatorHandle.PublishCommand(command); return(CustomResponse(clientViewModel)); }
public async Task<IActionResult> Put([FromBody]UpdateClientDto dto, Guid id) { var cmd = new UpdateClientCommand { ClientId = id, DisplayName = dto.DisplayName, RedirectUri = dto.RedirectUri }; var result = await _sagaBus.InvokeAsync<UpdateClientCommand, MessageResult>(cmd); if (result.Succeed) { return Created(Url.Action(), null); } return BadRequest(result.Message); }
public UpdateClientHandlerTests() { _repository = Substitute.For <IClientRepository>(); _handler = new ClientHandler(_repository); _commandWithoutName = _fixture .Build <UpdateClientCommand>() .Without(x => x.Name) .Create(); }
public async Task <ActionResult> UpdateClient([FromRoute] int id, UpdateClientCommand command) { if (id != command.Id) { return(BadRequest()); } await Mediator.Send(command); return(NoContent()); }
public async Task <IActionResult> Put(int id, [FromBody] UpdateClientCommand command) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var response = await this._mediator.Send(command); return(this.Ok()); }
public async Task <ClientDTO> UpdateClientAsync(UpdateClientCommand command, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var client = await GetClientAsync(command.ClientId, cancellationToken); SetClientProperties(client, command); await _context.SaveChangesAsync(cancellationToken); return(client.ToDTO()); }
public async void ShouldThrowNotFoundException() { var updatedClient = new UpdateClientCommand { Id = GConst.InvalidId, CompanyName = GConst.ValidName }; var status = await Record.ExceptionAsync(async() => await sut.Handle(updatedClient, CancellationToken.None)); Assert.NotNull(status); Assert.Equal(string.Format(GConst.NotFoundExceptionMessage, GConst.Client, GConst.InvalidId), status.Message); }
private void SetClientProperties(Client client, UpdateClientCommand command) { if (command.Age.HasValue) { client.Age = command.Age.Value; } if (command.Gender.HasValue) { client.Gender = command.Gender; } }
public async Task <IActionResult> Edit(UpdateClientQuery query, UpdateClientCommand command) { if (ModelState.IsValid == false) { var vm = BuildEditForm(command.Adapt <ClientEntity>()); return(View("FormView", vm)); } await _clientService.HandleAsync(command); return(Redirect(Url.AppUri(nameof(Edit), nameof(ClientController), new UpdateClientQuery(command.ClientId)))); }
public async Task <IActionResult> Put([FromBody] UpdateClientDto dto, Guid id) { var cmd = new UpdateClientCommand { ClientId = id, DisplayName = dto.DisplayName, RedirectUri = dto.RedirectUri }; var result = await _sagaBus.InvokeAsync <UpdateClientCommand, MessageResult>(cmd); if (result.Succeed) { return(Created(Url.Action(), null)); } return(BadRequest(result.Message)); }
public async Task <CommandExecutionResult> HandleAsync(UpdateClientCommand command) { var clientEntity = command.Adapt <ClientEntity>(); var properties = typeof(UpdateClientCommand).GetProperties() .Where(property => property.Name.Equals( nameof(ClientEntity.ClientId), StringComparison.InvariantCultureIgnoreCase) == false) .Select(property => property.Name); await _repository.UpdateAsync(clientEntity, properties); return(CommandExecutionResult.Success); }
public async void ShouldUpdateCorrect() { var updatedClient = new UpdateClientCommand { Id = clientId, CompanyName = GConst.ValidName }; var status = Task.FromResult(await sut.Handle(updatedClient, CancellationToken.None)); var resultId = context.Clients.SingleOrDefault(x => x.CompanyName == GConst.ValidName).Id; Assert.Equal(clientId, resultId); Assert.Equal(GConst.SuccessStatus, status.Status.ToString()); Assert.Equal(GConst.ValidCount, context.Clients.Count()); }
public async Task <ResultServiceVM> Put(Guid id, ClientUpdateInVM clientUpdateInVM) { ResultServiceVM resultServiceVM = new ResultServiceVM(); UpdateClientCommand updateClientCommand = Mapper.Map <UpdateClientCommand>(clientUpdateInVM); updateClientCommand.Id = id; await _clientService.Update(updateClientCommand); resultServiceVM.Messages.AddRange(_notifications.GetNotificationsMessages()); return(resultServiceVM); }
/// <summary> /// Update a client /// </summary> /// <param name="command"></param> /// <returns></returns> public ResultCommand Handle(UpdateClientCommand command) { var result = new ResultCommand(); var client = _repository.GetById(command.Id); client.Update(command.FirstName, command.LastName, command.Email); result.AddNotifications(client); if (result.Valid) { _repository.Update(client); } return(result); }
public async Task <IActionResult> Put([FromBody] UpdateClientCommand command) { try { var client = await _mediator.Send(command); return(Ok()); } catch (KeyNotFoundException ex) { return(NotFound()); } catch (ArgumentException argumentException) { return(BadRequest(argumentException.Message)); } }
private async Task HandleMessage(string message) { _logger.LogInformation($"Handling Message: {message}"); // TODO: A real app would have better error handling for parsing and routing messages using var doc = JsonDocument.Parse(message); var root = doc.RootElement; var eventType = root.GetProperty("EventType"); var entity = root.GetProperty("Entity"); using var scope = _serviceScopeFactory.CreateScope(); var mediator = scope.ServiceProvider.GetRequiredService <IMediator>(); if (eventType.GetString() == "Doctor-Created") { int id = entity.GetProperty("Id").GetInt32(); string name = entity.GetProperty("Name").GetString(); var command = new CreateDoctorCommand { Id = id, Name = name }; await mediator.Send(command); string notification = $"New Doctor {name} added in Clinic Management. "; await _scheduleHub.Clients.All.SendAsync("ReceiveMessage", notification); } if (eventType.GetString() == "Client-Updated") { int id = entity.GetProperty("Id").GetInt32(); string name = entity.GetProperty("Name").GetString(); var command = new UpdateClientCommand { Id = id, Name = name }; await mediator.Send(command); // TODO: Only send notification if changes occurred string notification = $"Client {name} updated in Clinic Management."; await _scheduleHub.Clients.All.SendAsync("ReceiveMessage", notification); } // TODO: Implement other kinds of updates }
public async Task <ICommandResult> HandleAsync(UpdateClientCommand command) { command.Validate(); if (command.Invalid) { return(new GenericCommandResult <ClientEntity>(false, command.Notifications)); } var client = await _repository.GetAsync(command.Id); client.SetName(command.Name); client.SetBirthDate(command.BirthDate); client.SetTypeDocument(command.TypeDocument); client.SetNumberDocument(command.NumberDocument); await _repository.UpdateAsync(client); return(new GenericCommandResult <ClientEntity>(true, client)); }
public async Task <IActionResult> Edit(Guid id, ClientUpdateModel model) { if (ModelState.IsValid) { var updateClientCommand = new UpdateClientCommand(model.Id, model.Name, model.Website, model.Description); var result = await _mediator.Send(updateClientCommand); if (result.IsSuccess) { return(RedirectToAction(nameof(ClientController.Index))); } else { ModelState.AddModelError("", result.Error); } } return(View(model)); }
public async Task ShouldUpdate_A_Client() { var name = "Vader"; var request = new UpdateClientCommand { Name = name, Id = Guid.NewGuid() }; var mapper = PetShopMappingConfiguration.GetPetShopMappings(); var mockRepository = new Mock <IClientRepository>(); mockRepository.Setup(p => p.Update(It.Is <Client>(c => c.Name == name))); var handler = new UpdateClientCommandHandler(mapper, mockRepository.Object); var result = await handler.Handle(request, CancellationToken.None); result.Message.Should().BeEquivalentTo("Client Updated"); mockRepository.Verify(m => m.Update(It.IsAny <Client>()), Times.Once()); }
public async Task Handle(UpdateClientCommand message) { if (!message.IsValid()) { NotifyValidationErrors(message); return; } var client = new Client(message.Id.ToString(), message.Name, message.Email, message.Code, message.Document, message.Phone); var existing = await _clientRepository.FirstOrDefaultAsync(c => c.Email == message.Email); if (existing != null && (existing.id != client.id)) { await _bus.RaiseEvent(new DomainNotification(message.MessageType, "O e-mail desse cliente já foi cadastrado.")); return; } await _clientRepository.AddOrUpdateAsync(client); }