public async Task HandleAsync(CreateRecipCommand command) { if (command == null) { throw ExceptionFactory.CommandIsNull(); } var tmp = _createMapper.Map(command); if (tmp == null) { throw ExceptionFactory.MappingOfRecipeFailed(); } if (tmp.Nutrient.ProteinInfo != null && tmp.Nutrient.ProteinInfo.Name == null) { tmp.Nutrient.ProteinInfo = null; } if (tmp.Nutrient.CarbohydratesInfo != null && tmp.Nutrient.CarbohydratesInfo.Name == null) { tmp.Nutrient.CarbohydratesInfo = null; } if (tmp.Nutrient.EnergyInfo != null && tmp.Nutrient.EnergyInfo.Name == null) { tmp.Nutrient.EnergyInfo = null; } if (tmp.Nutrient.EnergyKcalInfo != null && tmp.Nutrient.EnergyKcalInfo.Name == null) { tmp.Nutrient.EnergyKcalInfo = null; } if (tmp.Nutrient.FatInfo != null && tmp.Nutrient.FatInfo.Name == null) { tmp.Nutrient.FatInfo = null; } if (tmp.Nutrient.FiberInfo != null && tmp.Nutrient.FiberInfo.Name == null) { tmp.Nutrient.FiberInfo = null; } if (tmp.Nutrient.SaltInfo != null && tmp.Nutrient.SaltInfo.Name == null) { tmp.Nutrient.SaltInfo = null; } if (tmp.Nutrient.SaturatedFatInfo != null && tmp.Nutrient.SaturatedFatInfo.Name == null) { tmp.Nutrient.SaturatedFatInfo = null; } if (tmp.Nutrient.SodiumInfo != null && tmp.Nutrient.SodiumInfo.Name == null) { tmp.Nutrient.SodiumInfo = null; } if (tmp.Nutrient.SugarsInfo != null && tmp.Nutrient.SugarsInfo.Name == null) { tmp.Nutrient.SugarsInfo = null; } _unitOfWork.Foodrepos.AddRecip(tmp); _eventStore.AddEvents(_foodEventFactory.GetFoodCreatedEvent(tmp)); await _unitOfWork.SaveAsync(); }
public async Task HandleAsync(UpdateCacheCommand command) { if (command == null) { throw ExceptionFactory.CommandIsNull(); } List <RecipModel> listOfRecipModel = new List <RecipModel>(); RootobjectModel rootobject = GetRootobject(GetFoodrepoProducts($"https://www.foodrepo.org/api/v3/products?page%5Bnumber%5D=1&page%5Bsize%5D={ command.PageSize }").Result, command.PageNumer); listOfRecipModel.AddRange(rootobject.Data); for (int i = 0; i < 3; i++) { rootobject = GetRootobject(GetFoodrepoProducts(rootobject.Links.Next).Result, command.PageNumer); listOfRecipModel.AddRange(rootobject.Data); } var tmpListOfRecips = (from RecipModel model in listOfRecipModel select _recipMapper.Map(model)).ToList(); var listOfRecip = _unitOfWork.Foodrepos.GetAllAsyncRecip().Result; foreach (Recip recip in listOfRecip) { if (tmpListOfRecips.Select(x => x.RepoId).ToList().Contains(recip.RepoId)) { tmpListOfRecips.Remove(tmpListOfRecips.Where(x => x.RepoId == recip.RepoId).First()); } } if (tmpListOfRecips.Count > 0 || listOfRecip.Count == 0) { try { _unitOfWork.Foodrepos.AddRangeRecip(tmpListOfRecips); _eventStore.AddEvents(_foodrepoEventFactory.GetFoodrepoUpdatedEvent(tmpListOfRecips)); await _unitOfWork.SaveAsync(); } catch (Exception ex) { _logger.LogError(ex.Message); } } }
public async Task HandleAsync(UpdateRecipCommand command) { if (command == null) { throw ExceptionFactory.CommandIsNull(); } var tmp = _updateMapper.Map(command); if (tmp == null) { throw ExceptionFactory.MappingOfRecipeFailed(); } var recip = _unitOfWork.Foodrepos.GetAsyncRecipById(command.Id).Result; if (recip == null) { return; } tmp.Nutrient.Id = recip.Nutrient.Id; if (recip.Nutrient.ProteinInfo != null) { tmp.Nutrient.ProteinInfo.Id = recip.Nutrient.ProteinInfo.Id; } if (recip.Nutrient.CarbohydratesInfo != null) { tmp.Nutrient.CarbohydratesInfo.Id = recip.Nutrient.CarbohydratesInfo.Id; } if (recip.Nutrient.EnergyInfo != null) { tmp.Nutrient.EnergyInfo.Id = recip.Nutrient.EnergyInfo.Id; } if (recip.Nutrient.EnergyKcalInfo != null) { tmp.Nutrient.EnergyKcalInfo.Id = recip.Nutrient.EnergyKcalInfo.Id; } if (recip.Nutrient.FatInfo != null) { tmp.Nutrient.FatInfo.Id = recip.Nutrient.FatInfo.Id; } if (recip.Nutrient.FiberInfo != null) { tmp.Nutrient.FiberInfo.Id = recip.Nutrient.FiberInfo.Id; } if (recip.Nutrient.SaltInfo != null) { tmp.Nutrient.SaltInfo.Id = recip.Nutrient.SaltInfo.Id; } if (recip.Nutrient.SaturatedFatInfo != null) { tmp.Nutrient.SaturatedFatInfo.Id = recip.Nutrient.SaturatedFatInfo.Id; } if (recip.Nutrient.SodiumInfo != null) { tmp.Nutrient.SodiumInfo.Id = recip.Nutrient.SodiumInfo.Id; } if (recip.Nutrient.SugarsInfo != null) { tmp.Nutrient.SugarsInfo.Id = recip.Nutrient.SugarsInfo.Id; } _unitOfWork.Foodrepos.UpdateRecip(tmp); _eventStore.AddEvents(_foodEventFactory.GetFoodUpdatedEvent(tmp)); await _unitOfWork.SaveAsync(); }