public async Task <IActionResult> UpdateAsync(Guid id, [FromBody] UpdateFoodTableCommand command) { bool validCommand = command.Validate(); if (!validCommand) { return(CreateErrorResponse(command.ValidationResult)); } FoodTable foodTable = await _foodTableRepository.GetDefaultByIdAsync(id); if (foodTable == null) { return(NotFound()); } foodTable.Update( command.Name, command.Description ); await _foodTableRepository.UpdateAsync(foodTable); return(await CommitAsync()); }
public async Task ShouldFailToUpdateFoodTableIdNotFound() { UpdateFoodTableCommand command = new UpdateFoodTableCommand { FoodTableId = Guid.NewGuid(), Name = "Bacon de testes", Description = "Registrando um bacon de testes", }; Mock <IFoodTableRepository> foodTableRepository = new Mock <IFoodTableRepository>(); foodTableRepository.Setup(repo => repo.GetCustomByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>())) .ReturnsAsync(() => null); FoodTableCommandHandler handler = new FoodTableCommandHandler( foodTableRepository.Object, GetIdentityService(), GetMediator(), GetUnitOfWork(), GetLogger() ); CommandResult commandResult = await handler.Handle(command, default(CancellationToken)); Assert.IsFalse(commandResult.Success); Assert.AreEqual("Nenhuma categoria de alimentos com esse Id foi encontrada no banco de dados.", commandResult.Notifications.FirstOrDefault().Description); }
public async Task <CommandResult> Handle(UpdateFoodTableCommand request, CancellationToken cancellationToken) { CustomFoodTable customFoodTable = await _foodTableRepository.GetCustomByIdAsync(request.FoodTableId, _currentProfileId); if (customFoodTable == null) { return(FailureDueToCustomFoodTableNotFound()); } customFoodTable.Update( request.Name, request.Description ); await _foodTableRepository.UpdateAsync(customFoodTable); return(await CommitAndPublishDefaultAsync()); }
public async Task ShouldUpdateFoodTable() { Guid profileId = Guid.NewGuid(); CustomFoodTable existingFoodTable = new CustomFoodTable(profileId, "Bacon inicial", "Bacon antes de sofrer os testes"); Mock <IFoodTableRepository> foodTableRepository = new Mock <IFoodTableRepository>(); foodTableRepository.Setup(repo => repo.GetCustomByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>())) .ReturnsAsync(existingFoodTable); foodTableRepository.Setup(repo => repo.UpdateAsync(It.IsAny <CustomFoodTable>())) .Callback((CustomFoodTable foodTable) => { existingFoodTable = new CustomFoodTable(foodTable.ProfileId, foodTable.Name, foodTable.Description); }); UpdateFoodTableCommand command = new UpdateFoodTableCommand { FoodTableId = existingFoodTable.Id, Name = "Bacon após testes", Description = "Registrando um bacon de testes", }; CustomFoodTable insertedFoodTable = await foodTableRepository.Object.GetCustomByIdAsync(existingFoodTable.Id, profileId); Assert.AreEqual("Bacon inicial", insertedFoodTable.Name); FoodTableCommandHandler handler = new FoodTableCommandHandler( foodTableRepository.Object, GetIdentityService(profileId), GetMediator(), GetUnitOfWork(), GetLogger() ); CommandResult commandResult = await handler.Handle(command, default(CancellationToken)); CustomFoodTable updatedInsertedFoodTable = await foodTableRepository.Object.GetCustomByIdAsync(existingFoodTable.Id, profileId); Assert.IsTrue(commandResult.Success); Assert.AreEqual("Bacon após testes", updatedInsertedFoodTable.Name); }
public async Task ShouldFailToUpdateFoodTableUserNotOwner() { Guid profileId = Guid.NewGuid(); CustomFoodTable existingFoodTable = new CustomFoodTable(profileId, "Bacon inicial", "Bacon antes de sofrer os testes"); UpdateFoodTableCommand command = new UpdateFoodTableCommand { FoodTableId = existingFoodTable.Id, Name = "Bacon de testes", Description = "Registrando um bacon de testes", }; Mock <IFoodTableRepository> foodTableRepository = new Mock <IFoodTableRepository>(); foodTableRepository.Setup(repo => repo.GetCustomByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>())) .ReturnsAsync((Guid id, Guid pid) => { if (pid == existingFoodTable.ProfileId && id == existingFoodTable.Id) { return(existingFoodTable); } return(null); }); FoodTableCommandHandler handler = new FoodTableCommandHandler( foodTableRepository.Object, GetIdentityService(), GetMediator(), GetUnitOfWork(), GetLogger() ); CommandResult commandResult = await handler.Handle(command, default(CancellationToken)); Assert.IsFalse(commandResult.Success); Assert.AreEqual("Nenhuma categoria de alimentos com esse Id foi encontrada no banco de dados.", commandResult.Notifications.FirstOrDefault().Description); }
public async Task <IActionResult> UpdateAsync(Guid id, [FromBody] UpdateFoodTableCommand command) { command.FoodTableId = id; return(await CreateCommandResponse(command)); }