public async Task <CommandResult> Handle(UpdateFoodCommand request, CancellationToken cancellationToken) { CustomFood customFood = await _foodRepository.GetCustomByIdAsync(request.FoodId, _currentProfileId); if (customFood == null) { return(FailureDueToCustomFoodNotFound()); } CustomFoodTable customFoodTable = await _foodTableRepository.GetCustomByIdAsync(request.FoodTableId, _currentProfileId); if (customFoodTable == null) { return(FailureDueToCustomFoodTableNotFound()); } customFood.Update( request.Name, request.Description, request.FoodTableId, _mapper.Map <MacronutrientTable>(request.Macronutrients), _mapper.Map <MicronutrientTable>(request.Micronutrients), new FoodUnit(request.UnitType, request.DefaultGramsQuantityMultiplier) ); await _foodRepository.UpdateAsync(customFood); return(await CommitAndPublishDefaultAsync()); }
public async Task <CommandResult> Handle(RegisterFoodTableCommand request, CancellationToken cancellationToken) { CustomFoodTable customFoodTable = new CustomFoodTable( _currentProfileId, request.Name, request.Description ); await _foodTableRepository.RegisterAsync(customFoodTable); return(await CommitAndPublishDefaultAsync()); }
public async Task <CommandResult> Handle(RemoveFoodTableCommand request, CancellationToken cancellationToken) { CustomFoodTable customFoodTable = await _foodTableRepository.GetCustomByIdAsync(request.FoodTableId, _currentProfileId); if (customFoodTable == null) { return(FailureDueToCustomFoodTableNotFound()); } await _foodTableRepository.RemoveAsync(customFoodTable); return(await CommitAndPublishDefaultAsync()); }
public async Task ShouldRemoveFoodTable() { Guid profileId = Guid.NewGuid(); CustomFoodTable existingFoodTable = new CustomFoodTable(profileId, "Bacon inicial", "Bacon antes de sofrer os testes"); bool isRemoved = false; Mock <IFoodTableRepository> foodTableRepository = new Mock <IFoodTableRepository>(); foodTableRepository.Setup(repo => repo.GetCustomByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>())) .ReturnsAsync(() => { if (!isRemoved) { return(existingFoodTable); } return(null); }); foodTableRepository.Setup(repo => repo.RemoveAsync(It.IsAny <CustomFoodTable>())) .Callback((CustomFoodTable foodTable) => { isRemoved = true; }); RemoveFoodTableCommand command = new RemoveFoodTableCommand { 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(Guid.NewGuid(), profileId); Assert.IsTrue(commandResult.Success); Assert.IsNull(updatedInsertedFoodTable); }
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> GetCustomByIdAsync(Guid id) { CustomFoodTable customFoodTable = await _foodTableRepository.GetCustomByIdAsync(id, _currentProfileId); return(CreateResponse(customFoodTable)); }