public void DeleteFavorite(int id) { using (var context = new DietJournalEntities()) { var favorite = context.SupplementFavorites.FirstOrDefault(f => f.UserId == CurrentUserId && f.Id == id); if (favorite != null) context.SupplementFavorites.DeleteObject(favorite); context.SaveChanges(); } }
public void Delete(int id) { using (var context = new DietJournalEntities()) { var entry = context.ExerciseEntries.FirstOrDefault(e => e.Id == id); if (entry != null && entry.UserId == CurrentUserId.Value) context.ExerciseEntries.DeleteObject(entry); context.SaveChanges(); } }
public JsonResult Add(WaterEntryModel model) { if (ModelState.IsValid) { using (var context = new DietJournalEntities()) { var waterEntry = context.WaterEntries.CreateObject(); context.WaterEntries.AddObject(waterEntry); waterEntry.Ounces = model.Ounces; waterEntry.EntryDate = model.ConsumedDate; waterEntry.SavedDate = DateTime.Now; waterEntry.UserId = CurrentUserId.Value; context.SaveChanges(); } return Json(new { IsValid = true, ReturnUrl = Url.Action("Index", new { date = model.ConsumedDate }) }); } return Json(new { IsValid = false, ErrorMessage = "" }); }
public JsonResult Add(SupplementEntryModel model) { if (ModelState.IsValid) { using (var context = new DietJournalEntities()) { var entry = context.SupplementEntries.CreateObject(); context.SupplementEntries.AddObject(entry); entry.Name = model.Name; if (!String.IsNullOrEmpty(model.Milligrams)) entry.Milligrams = decimal.Parse(model.Milligrams); entry.EntryDate = model.ConsumedDate; entry.SavedDate = DateTime.Now; entry.UserId = CurrentUserId.Value; context.SaveChanges(); } return Json(new { IsValid = true, ReturnUrl = Url.Action("Index", new { date = model.ConsumedDate }) }); } return Json(new { IsValid = false, ErrorMessage = "" }); }
public JsonResult Entry(SupplementEntryModel model) { if (ModelState.IsValid) { using (var entities = new DietJournalEntities()) { SupplementEntry entry = null; if (model.Id > 0) entry = entities.SupplementEntries.FirstOrDefault(e => e.Id == model.Id); if (entry == null) { entry = new SupplementEntry(); entities.SupplementEntries.AddObject(entry); } entry.UserId = CurrentUserId.Value; entry.Name = model.Name; entry.EntryDate = model.ConsumedDate; entry.SavedDate = DateTime.Now; entry.Milligrams = !String.IsNullOrEmpty(model.Milligrams) ? decimal.Parse(model.Milligrams) : 0M; if (model.Favorite) { var favorite = new SupplementFavorite { UserId = CurrentUserId.Value, Milligrams = entry.Milligrams, Name = entry.Name }; entities.SupplementFavorites.AddObject(favorite); } entities.SaveChanges(); } return Json(new { IsValid = true }); } return Json(new { IsValid = false, ErrorMessage = "" }); }
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 = "" }); }
public JsonResult WeightEntry(WeightEntryModel model) { if (ModelState.IsValid) { using (var context = new DietJournalEntities()) { WeightEntry weightEntry = null; if (model.Id > 0) { weightEntry = context.WeightEntries.FirstOrDefault(e => e.Id == model.Id); } if (weightEntry == null) { weightEntry = context.WeightEntries.CreateObject(); context.WeightEntries.AddObject(weightEntry); } weightEntry.Amount = model.Amount; weightEntry.EntryDate = model.ConsumedDate; weightEntry.SavedDate = DateTime.Now; weightEntry.UserId = CurrentUserId.Value; context.SaveChanges(); } return Json(new { IsValid = true, ReturnUrl = Url.Action("Index", new { date = model.ConsumedDate }) }); } return Json(new { IsValid = false, ErrorMessage = "" }); }
public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user MembershipCreateStatus createStatus; var membershipUser = Membership.CreateUser(model.Email, model.Password, model.Email, null, null, true, null, out createStatus); if (createStatus == MembershipCreateStatus.Success) { FormsAuthentication.SetAuthCookie(model.Email, false /* createPersistentCookie */); using (var entities = new DietJournalEntities()) { var settings = entities.ProfileSettings.CreateObject(); settings.UserId = (Guid)membershipUser.ProviderUserKey; if (model.DietPlanId != NoDietPlanValue && model.DietPlanId != SelectDietPlanValue) settings.DietPlanId = model.DietPlanId; entities.ProfileSettings.AddObject(settings); entities.SaveChanges(); } return RedirectToAction("Index", "Home"); } else { ModelState.AddModelError("", ErrorCodeToString(createStatus)); } } model.AvailableDietPlans = GetDietPlanSelectItems(); // If we got this far, something failed, redisplay form return View(model); }
public ActionResult ProfileSettings(ProfileSettingsModel model) { var membership = Membership.GetUser(HttpContext.User.Identity.Name); using (var context = new DietJournalEntities()) { var result = context.ProfileSettings.FirstOrDefault(s => s.UserId == (Guid)membership.ProviderUserKey); if (result == null) { result = context.ProfileSettings.CreateObject(); result.UserId = (Guid)membership.ProviderUserKey; context.ProfileSettings.AddObject(result); } result.FirstName = model.FirstName.Trim(); result.LastName = model.LastName.Trim(); if (!String.IsNullOrEmpty(model.Birthday)) result.BirthDay = DateTime.Parse(model.Birthday); else result.BirthDay = null; result.Gender = model.Gender; result.CaloriesGoal = model.CaloriesGoal; result.CaptureCalories = model.CaptureCalories; result.CaptureCarbs = model.CaptureCarbs; result.CaptureFat = model.CaptureFat; result.CaptureProtein = model.CaptureProtein; result.DietPlanId = model.DietPlanId; result.WeightGoal = model.WeightGoal; context.SaveChanges(); CurrentProfileSettings = result; } return RedirectToAction("Settings", "Journal"); }
public JsonResult Entry(FoodEntryModel model) { if (ModelState.IsValid) { using (var entities = new DietJournalEntities()) { FoodEntry foodEntry = null; if (model.Id > 0) foodEntry = entities.FoodEntries.FirstOrDefault(e => e.Id == model.Id); if (foodEntry == null) { foodEntry = new FoodEntry(); entities.FoodEntries.AddObject(foodEntry); } foodEntry.UserId = CurrentUserId.Value; foodEntry.Title = model.Title; foodEntry.Description = model.Description; foodEntry.Calories = model.Calories; foodEntry.Carbs = model.Carbs; foodEntry.EntryDate = model.ConsumedDate; foodEntry.Fat = model.Fat; foodEntry.Protein = model.Protein; foodEntry.SavedDate = DateTime.Now; foodEntry.MealType = !String.IsNullOrEmpty(model.FoodEntryType) ? int.Parse(model.FoodEntryType) : 0; if (model.Favorite) { var favorite = new FoodFavorite { UserId = CurrentUserId, MealType = foodEntry.MealType, Title = foodEntry.Title, Description = foodEntry.Description, Calories = foodEntry.Calories, Carbs = foodEntry.Carbs, Protein = foodEntry.Protein, Fat = foodEntry.Fat }; entities.FoodFavorites.AddObject(favorite); } entities.SaveChanges(); if (model.EntryValues != null && model.EntryValues.Count > 0) { foreach (var entryValue in model.EntryValues) { FoodEntryMealTypeValue value = null; if (!String.IsNullOrEmpty(entryValue.Id)) { var entryValueId = int.Parse(entryValue.Id); value = entities.FoodEntryMealTypeValues.FirstOrDefault(v => v.Id == entryValueId); } if (value == null) { value = new FoodEntryMealTypeValue { MealTypeValueId = int.Parse(entryValue.EntryTypeValueId), FoodEntryId = foodEntry.Id }; entities.FoodEntryMealTypeValues.AddObject(value); } value.Value = entryValue.Value; } entities.SaveChanges(); } } return Json(new { IsValid = true }); } return Json(new { IsValid = false, ErrorMessage = "" }); }