public ActionResult Add(DateTime date)
 {
     var model = new ExerciseEntryModel
     {
         ConsumedDate = date,
         AvailableExerciseTypes = GetAvailableExerciseTypes()
     };
     return View("Entry", model);
 }
        public ActionResult Entry(int id)
        {
            ExerciseEntryModel model = null;

            using (var entities = new DietJournalEntities())
            {
                var result = entities.ExerciseEntries.FirstOrDefault(e => e.Id == id);

                if (result != null && result.UserId == CurrentUserId.Value)
                {
                    model = new ExerciseEntryModel
                    {
                        Id = id,
                        Description = result.Description,
                        ConsumedDate = result.EntryDate,
                        ExerciseType = result.Type.ToString(),
                        AvailableExerciseTypes = GetAvailableExerciseTypes()
                    };
                }
            }

            return EntryView(model);
        }
        public JsonResult Entry(ExerciseEntryModel model)
        {
            if (ModelState.IsValid)
            {
                using (var entities = new DietJournalEntities())
                {
                    ExerciseEntry entry = null;

                    if (model.Id > 0)
                        entry = entities.ExerciseEntries.FirstOrDefault(e => e.Id == model.Id);

                    if (entry == null)
                    {
                        entry = new ExerciseEntry();
                        entities.ExerciseEntries.AddObject(entry);
                    }

                    entry.UserId = CurrentUserId.Value;
                    entry.Description = model.Description;
                    entry.EntryDate = model.ConsumedDate;
                    entry.SavedDate = DateTime.Now;
                    entry.Type = !String.IsNullOrEmpty(model.ExerciseType) ? int.Parse(model.ExerciseType) : 0;

                    if (model.Favorite)
                    {
                        var favoriteModel = new ExerciseFavorite
                        {
                            Type = entry.Type,
                            Description = entry.Description,
                            UserId = CurrentUserId.Value
                        };
                        entities.ExerciseFavorites.AddObject(favoriteModel);
                    }

                    entities.SaveChanges();

                    if (model.EntryValues != null && model.EntryValues.Count > 0)
                    {
                        foreach (var entryValue in model.EntryValues)
                        {
                            ExerciseEntryExerciseTypeValue value = null;

                            if (!String.IsNullOrEmpty(entryValue.Id))
                            {
                                var entryValueId = int.Parse(entryValue.Id);
                                value = entities.ExerciseEntryExerciseTypeValues.FirstOrDefault(v => v.Id == entryValueId);
                            }

                            if (value == null)
                            {
                                value = new ExerciseEntryExerciseTypeValue
                                {
                                    ExerciseTypeId = int.Parse(entryValue.EntryTypeValueId),
                                    ExerciseEntryId = entry.Id
                                };
                                entities.ExerciseEntryExerciseTypeValues.AddObject(value);
                            }

                            value.Value = entryValue.Value;
                        }

                        entities.SaveChanges();
                    }
                }

                return Json(new { IsValid = true });
            }

            return Json(new { IsValid = false, ErrorMessage = "" });
        }
        private ActionResult EntryView(ExerciseEntryModel model)
        {
            ViewBag.Back = model.Id > 0;

            return View("Entry", model);
        }
        public ActionResult SelectFavorite(ExerciseFavoriteSelectionModel model)
        {
            var entryModel = new ExerciseEntryModel
            {
                ConsumedDate = model.ConsumedDate
            };

            if (ModelState.IsValid && !String.IsNullOrEmpty(model.Selection))
            {
                int favoriteId = int.Parse(model.Selection);

                using (var context = new DietJournalEntities())
                {
                    var favorite = context.ExerciseFavorites.FirstOrDefault(f => f.Id == favoriteId);
                    if (favorite != null)
                    {
                        entryModel.ExerciseType = favorite.Type.ToString();
                        entryModel.Description = favorite.Description;
                        entryModel.Favorite = true;
                    }
                }
            }

            entryModel.AvailableExerciseTypes = GetAvailableExerciseTypes();

            return EntryView(entryModel);
        }