public FoodModel Get(int id) { return(_modelFactory.Create(_repo.GetFood(id))); }
/// <summary> /// Parses DiaryEntryModel into DiaryEntry /// </summary> /// <param name="entryModel">Requires the presence of the either the MeasureUrl, FoodUrl or Quantity values</param> /// <returns>Returns null if parse failed</returns> public DiaryEntry Parse(DiaryEntryModel entryModel) { try { DiaryEntry entry = new DiaryEntry(); bool hasValue = false; if (!string.IsNullOrWhiteSpace(entryModel.MeasureUrl)) { Uri uri = new Uri(entryModel.MeasureUrl); int measuredId = int.Parse(uri.Segments.Last()); Measure measure = _repo.GetMeasure(measuredId); if (measure == null) { return(null); } entry.Measure = measure; entry.FoodItem = measure.Food; hasValue = true; } if (!string.IsNullOrWhiteSpace(entryModel.FoodUrl) && string.IsNullOrWhiteSpace(entryModel.MeasureUrl)) { Uri uri = new Uri(entryModel.FoodUrl); int foodId = int.Parse(uri.Segments.Last()); Food food = _repo.GetFood(foodId); if (food == null) { return(null); } entry.FoodItem = food; hasValue = true; } if (entryModel.Quantity != default(double)) { entry.Quantity = entryModel.Quantity; hasValue = true; } // When a DiaryEntryModel was passed without any meaningful values if (!hasValue) { return(null); } return(entry); } catch { return(null); } }