public async Task <IActionResult> Delete([FromBody] DeleteCustomerCommand command, [FromHeader(Name = "x-requestId")] string requestId) { var commandResult = false; if (Guid.TryParse(requestId, out var guid) && guid != Guid.Empty) { var deleteCustomerCommand = new IdentifiedCommand <DeleteCustomerCommand, bool>(command, guid); _logger.LogInformation( "----- Sending command: {CommandName}: {CommandId} ({@Command})", typeof(DeleteCustomerCommand).Name, nameof(command.Id), command.Id, command); commandResult = await _mediator.Send(deleteCustomerCommand); } if (commandResult) { return(Ok()); } else { return(BadRequest()); } }
public async Task <ResultWrapper <DeleteCustomerOutput> > Handle(DeleteCustomerCommand request, CancellationToken cancellationToken) { ResultWrapper <DeleteCustomerOutput> result = new ResultWrapper <DeleteCustomerOutput>(); try { var tData = await _dbContext.TUser.FirstOrDefaultAsync(x => x.Id == request.UserId && x.Role == Infrastructure.AppEnums.RoleEnum.Customer); if (tData == null) { result.Status = false; result.Message = "Customer doesn't exists"; return(result); } _dbContext.TUser.Remove(tData); await _dbContext.SaveChangesAsync(); result.Status = true; result.Result = new DeleteCustomerOutput() { FireBaseId = tData.FireBaseId }; } catch (Exception ex) { result.Status = false; result.Message = ex.Message; } return(result); }
public async Task <IActionResult> DeleteById(int id) { var customer = new DeleteCustomerCommand(id); var result = await _mediatr.Send(customer); return(result != null ? (IActionResult)Ok(new { Message = "success" }) : NotFound(new { Message = "Customer not found" })); }
public async Task TestSuccess() { // Arrange Mock <IDataRepository <Customer, Guid> > mockRepository = new Mock <IDataRepository <Customer, Guid> >(); Guid id = new Guid("b7c2acaa-ad72-47b3-b858-26357cf14fbb"); Customer customer = new Customer() { Id = id }; mockRepository.Setup(m => m.FindAsync(id, default)).Returns(new ValueTask <Customer>(customer)); DeleteCustomerRequest request = new DeleteCustomerRequest() { Id = id }; DeleteCustomerCommand sut = new DeleteCustomerCommand(MockHelpers.GetLogger <DeleteCustomerCommand>(), mockRepository.Object); // Act DeleteCustomerResponse result = await sut.Handle(request, default); // Assert Assert.IsNotNull(result); mockRepository.VerifyAll(); mockRepository.Verify(m => m.FindAsync(id, default), Times.Once); mockRepository.Verify(m => m.Delete(customer), Times.Once); }
public async Task HandleAsync_Valid() { var customerRepositoryMock = new Mock <ICustomerRepository>(); var id = Guid.Parse("926a4480-61f5-416a-a16f-5c722d8463f7"); var command = new DeleteCustomerCommand { Id = id, }; var customerId = new CustomerIdentity(id); var expectedResponse = new DeleteCustomerCommandResponse(); customerRepositoryMock .Setup(e => e.ExistsAsync(customerId)) .ReturnsAsync(true); customerRepositoryMock .Setup(e => e.RemoveAsync(customerId)); var handler = new DeleteCustomerCommandHandler(customerRepositoryMock.Object); var response = await handler.HandleAsync(command); customerRepositoryMock.Verify(e => e.RemoveAsync(customerId), Times.Once()); response.Should().BeEquivalentTo(expectedResponse); }
public static void Execute(this CustomerState state, DeleteCustomerCommand cmd) { if (state.Customers.All(c => c.Id != cmd.Id)) { return; } state.Store.StoreEvent(new CustomerDeletedEvent(cmd.Id)); }
public async Task <ActionResult> Delete([FromRoute] Guid id, DeleteCustomerCommand command, CancellationToken cancellationToken) { command.SetCustomerId(id); await Mediator.Send(command, cancellationToken); return(Ok()); }
public DeleteCustomerHandlerTests() { _customer = _fixture.Create <Entity.Concrete.Customer>(); _result = new Mock <IResult>(); _repository = new Mock <IGenericRepository <Entity.Concrete.Customer> >(); _deleteCommand = _fixture.Create <DeleteCustomerCommand>(); _sut = new DeleteCustomerCommandHandler(_repository.Object); }
public async Task Should_Throws_NotFound_Exception_When_Id_Is_Invalid() { var InvalidId = 0; var command = new DeleteCustomerCommand { Id = InvalidId }; await Should.ThrowAsync <NotFoundException>(() => _sut.Handle(command, default)); }
public async Task <IActionResult> Delete(Guid guid) { var dto = new DeleteCustomerCommand { Guid = guid }; var result = await _mediator.Send(dto); return(Ok(result)); }
public async Task <IActionResult> Delete(int ID) { var command = new DeleteCustomerCommand(ID); var result = await meciater.Send(command); return(result != null ? (IActionResult)Ok(new { Message = "success" }) : NotFound(new { Message = "not found" })); //return Ok(result); //return null; }
public async Task Handle_GivenValidIdAndSomeOrders_ThrowsDeleteFailureException() { var validId = "BREND"; var command = new DeleteCustomerCommand { Id = validId }; await Assert.ThrowsAsync <DeleteFailureException>(() => _sut.Handle(command, CancellationToken.None)); }
public async Task <IActionResult> Delete([FromBody] DeleteCustomerCommand deleteCustomer) { var result = await Mediator.Send(deleteCustomer); if (result.Success) { return(Ok(result.Message)); } return(BadRequest(result.Message)); }
public async Task Handle_GivenInvalidId_ThrowsNotFoundException() { var invalidId = "INVLD"; var command = new DeleteCustomerCommand { Id = invalidId }; await Assert.ThrowsAsync <NotFoundException>(() => _sut.Handle(command, CancellationToken.None)); }
public async Task ShouldDeleteCustomerFromDatabase() { var command = new DeleteCustomerCommand(_context); await command.Execute("JASON"); var entity = await _context.Customers.FindAsync("JASON"); Assert.Null(entity); }
public async Task <IActionResult> DeleteCustomer(long id) { var command = new DeleteCustomerCommand(id); var result = await messages.Dispatch(command); return(result.Match <IActionResult>( (errors) => BadRequest(errors), (valid) => NoContent() )); }
public CustomerViewModel(CustomerDataService CustomerDataService) { _customerDataService = CustomerDataService; Customers = new BindableCollection <Customers>(); addCustomerCommand = new AddCustomerCommand(this); deleteCustomerCommand = new DeleteCustomerCommand(this); EditCustomerCommand = new EditCustomerCommand(this); CustomerConverter = new CustomerConverter(); Load(); }
public async Task Handle_GivenInvalidRequest_ShouldThrowNotFoundException() { // Arrange var command = new DeleteCustomerCommand { Id = 133 }; var sut = new DeleteCustomerCommandHandler(this.deletableEntityRepository); // Act & Assert await Should.ThrowAsync <NotFoundException>(sut.Handle(command, It.IsAny <CancellationToken>())); }
public CustomerMasterDetailsViewModel() { _catalog = new CustomerCatalog(); _customerItemViewModel = new CustomerItemViewModel(new Customer()); _deleteCommand = new DeleteCustomerCommand(_catalog, this); _newCommand = new NewCustomerCommand(_catalog, this); _saveCommand = new SaveCustomerCommand(_catalog); _refreshCommand = new RefreshCustomerCommand(this, _catalog); RefreshCustomerItemViewModelCollection(); _catalog.Load(); }
public IActionResult Delete([FromBody] DeleteCustomerCommand command) { var result = _handler.Handle(command); if (!result.IsValid) { return(BadRequest(result)); } return(Ok(result)); }
public async Task <IActionResult> DeleteCustomer(int?id) { if (id == null) { return(NotFound()); } var request = new DeleteCustomerCommand(id); var response = await _mediator.Send(request); return(View(response)); }
public async Task <ActionResult> DeleteCustomerAsync(Guid id) { var request = new DeleteCustomerCommand { Id = id, }; await _messageBus.SendAsync(request); return(NoContent()); }
public void DeleteCustomerTest_InvalidId_ThrowsCustomerNotFoundException() { var mediator = TestMediatorFactory.BuildMediator(_dbContext); var command = new DeleteCustomerCommand { Id = new Guid("55000000-0000-0000-0000-000000000000") }; Assert.Throws <CustomerNotFoundException>(() => mediator.Send(command).GetAwaiter().GetResult()); }
public async Task <IActionResult> DeleteCustomer(DeleteCustomerCommand model) { FirebaseUser user = HttpContext.GetFirebaseUser(); model.FireBaseId = user.UserId; ResultWrapper <DeleteCustomerOutput> result = new ResultWrapper <DeleteCustomerOutput>(); result = await _mediator.Send(model); return(Ok(result)); }
public CommandResult Handle(DeleteCustomerCommand command) { var result = command.Validate(); if (result.IsValid) { // var customer = new Customer(command.Id, command.Name, command.Email, command.Phone); _customerRepository.Delete(command.Id); } return(result); }
public async Task Should_Delete_Customer_With_Valid_Id_And_Zero_Order() { var validId = 1; var command = new DeleteCustomerCommand { Id = validId }; await _sut.Handle(command, CancellationToken.None); var customer = await ShoppingDbContext.Customers.FindAsync(1); customer.ShouldBeNull(); }
public async Task Handle_GivenExistCustomer_SholudBeWithOutExceptionAsync() { var command = new DeleteCustomerCommandHandler(_context); var existCustomer = new DeleteCustomerCommand() { Id = 1 }; var result = await command.Handle(existCustomer, CancellationToken.None); Assert.IsType <Unit>(result); }
public async Task DeleteCustomer_ShouldDeleteCustomer_GivenCustomer( [Greedy] CustomerController sut, DeleteCustomerCommand command ) { //Act var actionResult = await sut.DeleteCustomer(command); //Assert var noContentResult = actionResult as NoContentResult; noContentResult.Should().NotBeNull(); }
public async Task <bool> Handle(DeleteCustomerCommand request, CancellationToken cancellationToken) { if (!request.IsInValidState()) { NotifyValidationErrors(request); return(await Task.FromResult(false)); } _customerRepository.DeleteCustomer(request.Id); if (await Commit()) { await _bus.RaiseEvent(new DeletedCustomerEvent(request.Id)); } return(await Task.FromResult(true)); }
public async Task Handle_GivenValidIdAndZeroOrders_DeletesCustomer() { var validId = "JASON"; var command = new DeleteCustomerCommand { Id = validId }; await _sut.Handle(command, CancellationToken.None); var customer = await _context.Customers.FindAsync(validId); Assert.Null(customer); }