public DiaryEntry Parse(DiaryEntryModel model) { try { var entity = new DiaryEntry(); if (model.Quantity != default(double)) { entity.Quantity = model.Quantity; } if (!string.IsNullOrWhiteSpace(model.MeasureUrl)) { var uri = new Uri(model.MeasureUrl); var measureId = int.Parse(uri.Segments.Last()); var measure = _repository.GetMeasure(measureId); entity.Measure = measure; entity.FoodItem = measure.Food; } return(entity); } catch { return(null); } }
public DiaryEntryModel Create(DiaryEntry diaryEntry) { var selfHref = _urlHelper.Link(DIARY_ENTRIES_ROUTE, new { diaryid = diaryEntry.Diary.CurrentDate.ToString(DIARY_DATE_FORMAT), entryid = diaryEntry.Id }); var selfLink = CreateLink(selfHref, SELF_REL); var model = new DiaryEntryModel { Links = new List <LinkModel> { selfLink }, FoodDescription = diaryEntry.FoodItem.Description, MeasureDescription = diaryEntry.Measure.Description, MeasureUrl = _urlHelper.Link(MEASURES_ROUTE, new { foodid = diaryEntry.FoodItem.Id, measureid = diaryEntry.Measure.Id }), Quantity = diaryEntry.Quantity }; return(model); }