public ActionResult DeletePost(int?id) { using (var context = new RotatingChoresContext()) { var doer = GetDoerById(id, context); if (doer != null) { if (IsGroupObject(doer.GroupId)) { try { context.ChoreDoers.Remove(doer); context.SaveChanges(); TempData["Message"] = "Chore doer successfully deleted!"; return(RedirectToAction("Index", "Doer")); } catch (System.Data.Entity.Infrastructure.DbUpdateException) { TempData["FailureMessage"] = "Member must not have chores assigned or be the last to complete a chore."; return(RedirectToAction("Index", "Chores")); } } return(InvalidGroup()); } return(DoerNotFound()); } }
//Methods for this controller //************************************************************ //Returns a view with ChoreDoerModel if all checks pass private ActionResult ViewDoerById(int?id) { //Check for Id if (id == null) { NullIdEncountered(); } using (var context = new RotatingChoresContext()) { var doer = GetDoerById(id, context); //Check for Doer in the database if (doer != null) { //Check that ChoreDoer is part of user's group if (IsGroupObject(doer.GroupId)) { var userGroup = GetUserGroup(context); var doerModel = ChoreDoerModel.ConvertFromDoer(doer); doerModel.AddChoresList(doer, userGroup); return(View(doerModel)); } return(InvalidGroup()); } return(DoerNotFound()); } }
public ActionResult Index() { if (User.Identity.IsAuthenticated) { var id = User.Identity.GetUserId(); using (var context = new RotatingChoresContext()) using (var userManager = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>()) { string email = userManager.GetEmail(id); var choreDoer = context.ChoreDoers.FirstOrDefault(c => c.Email == email); if (choreDoer == null) { var model = new ChoreDoerModel(); model.Email = email; TempData["Message"] = "You have not set up your Chore Doer Profile. Select Max Difficulty 'None' to be excluded from chores."; return(RedirectToAction("Add", "Doer", model)); } else { ViewBag.Message = "Looks like you're in the system!"; } } return(View()); } ViewBag.Message = "Sign In to start keeping track of chores!"; return(View()); }
protected Group GetUserGroup(RotatingChoresContext context) { var userGroupId = User.Identity.GetGroupId(); Group userGroup = context.Groups.First(g => g.GroupId == userGroupId); return(userGroup); }
public ActionResult MarkCompletePost(int?id) { using (var context = new RotatingChoresContext()) { var chore = GetChoreById(id, context); if (chore != null && IsGroupObject(chore.GroupId)) { var group = GetUserGroup(context); //Working with the model var choreModel = ChoreModel.ConvertFromChore(chore); //Change LastCompleted etc on model choreModel.MarkComplete(); //Use model to update chore choreModel.UpdateChore(context, chore); //Assign chore to next available person. choreModel.AdvanceChore(chore, group); context.SaveChanges(); TempData["Message"] = "Chore marked complete! Good Job!"; return(RedirectToAction("Index")); } TempData["FailureMessage"] = "There was an issuse completing the chore."; return(RedirectToAction("Index")); } }
//The following are the methods used in this controller. //***************************************************************************************** //Returns a view with a ChoreModel if all checks pass. private ActionResult ViewChoreById(int?id) { //Check for existing Id if (id == null) { return(NullIdEncountered()); } using (var context = new RotatingChoresContext()) { var chore = GetChoreById(id, context); //Check for chore in database if (chore != null) { //Check if Chore is in user's group if (IsGroupObject(chore.GroupId)) { var choreModel = ChoreModel.ConvertFromChore(chore); SetGroupMemberSelectList(context); return(View(choreModel)); } return(InvalidGroup()); } return(ChoreNotFound()); } }
public ActionResult Add() { using (var context = new RotatingChoresContext()) { SetGroupMemberSelectList(context); return(View()); } }
private static ChoreDoer GetDoerById(int?id, RotatingChoresContext context) { if (id == null) { return(null); } return(context.ChoreDoers.SingleOrDefault(c => c.ChoreDoerId == id)); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Name, Email = model.Email }; //Check the existing ChoreDoers for the email from the user. using (var context = new RotatingChoresContext()) { var check = context.ChoreDoers.SingleOrDefault(d => d.Email.ToLower() == user.Email.ToLower()); if (check == null) { //If no ChoreDoer found by user's email, create new group - var newGroup = context.Groups.Create(); //Add to the databse and save for auto increment value context.Groups.Add(newGroup); context.SaveChanges(); //And set to new GroupId user.GroupId = newGroup.GroupId; } else { //If user email is found, set user GroupId to matching ChoreDoer GroupId. user.GroupId = check.GroupId; } } var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { //await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Confirm your account"); ViewBag.Message = "Check your email and confirm your account, you must be confirmed " + "before you can log in."; return(View("Info")); // return RedirectToAction("Index", "Home"); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
//This method uses the ChoreModel to update the given chore in the given context. //Be sure to call SaveChanges() after updating. public void UpdateChore(RotatingChoresContext context, Chore chore) { ChoreDoer assignedTo = context.ChoreDoers.SingleOrDefault(c => c.ChoreDoerId == AssignedToId); chore.AssignedTo = assignedTo; chore.Description = Description; chore.Name = Name; chore.Difficulty = (ChoreBase.DifficultyLevel)Difficulty; if (LastCompletedById != null) { chore.LastCompleted = LastCompleted; ChoreDoer last = context.ChoreDoers.SingleOrDefault(c => c.ChoreDoerId == LastCompletedById); chore.LastCompletedBy = last; } }
private void SetGroupMemberSelectList(RotatingChoresContext context) { var returnList = new List <ChoreDoerModel>(); Group userGroup = GetUserGroup(context); if (userGroup.Members.Count() > 0) { foreach (var member in userGroup.Members) { returnList.Add(ChoreDoerModel.ConvertFromDoer(member)); } } var selectList = new SelectList(returnList, "ChoreDoerId", "Name"); ViewBag.GroupMembers = selectList; }
public ActionResult DeletePost(int id) { using (var context = new RotatingChoresContext()) { var chore = GetChoreById(id, context); if (chore != null && IsGroupObject(chore.GroupId)) { context.Chores.Remove(chore); context.SaveChanges(); TempData["Message"] = "Chore successfully deleted!"; return(RedirectToAction("Index")); } TempData["FailureMessage"] = "There was an issuse deleting the chore."; return(RedirectToAction("Index")); } }
public ActionResult Edit(ChoreModel model) { using (var context = new RotatingChoresContext()) { var chore = model.GetRepresentedChore(context); model.UpdateChore(context, chore); ValidateAssignTo(chore); if (ModelState.IsValid && IsGroupObject(chore.GroupId)) { context.SaveChanges(); TempData["Message"] = "Chore has been updated!"; return(RedirectToAction("Index")); } SetGroupMemberSelectList(context); return(View(model)); } }
// GET: Doer public ActionResult Index() { using (var context = new RotatingChoresContext()) { var group = GetUserGroup(context); var modelList = new List <ChoreDoerModel>(); if (group.Members.Count > 0) { foreach (var doer in group.Members) { var doerModel = ChoreDoerModel.ConvertFromDoer(doer); doerModel.AddChoresList(doer, group); modelList.Add(doerModel); } return(View(modelList)); } TempData["Message"] = "No group members. Add one now!"; return(RedirectToAction("Add")); } }
// GET: Chore public ActionResult Index() { using (var context = new RotatingChoresContext()) { var group = GetUserGroup(context); //Check to make sure the group is found and has chores if (group != null & group.Chores.Count > 0) { //"Index" model is of type List<ChoreModel> List <ChoreModel> chores = new List <ChoreModel>(); foreach (var chore in group.Chores) { chores.Add(ChoreModel.ConvertFromChore(chore)); } return(View(chores)); } } //If there are no chores in the group redirect to Add page; TempData["Message"] = "No chores yet, add one now!"; return(RedirectToAction("Add")); }
public ActionResult Add(ChoreDoerModel model) { using (var context = new RotatingChoresContext()) { var newDoer = context.ChoreDoers.Create(); newDoer.GroupId = User.Identity.GetGroupId(); model.UpdateDoer(newDoer); try { context.ChoreDoers.Add(newDoer); context.SaveChanges(); TempData["Message"] = "New group member added!"; } catch (Exception e) { TempData["FailureMessage"] = e.Message; return(View(model)); } } return(RedirectToAction("Index")); }
public ActionResult Add(ChoreModel choreModel) { using (var context = new RotatingChoresContext()) { var addingChore = context.Chores.Create(); choreModel.UpdateChore(context, addingChore); ValidateAssignTo(addingChore); if (ModelState.IsValid) { addingChore.GroupId = User.Identity.GetGroupId(); context.Chores.Add(addingChore); context.SaveChanges(); TempData["Message"] = "New chore added!"; return(RedirectToAction("Index")); } //If there is a problem with the model SetGroupMemberSelectList(context); return(View(choreModel)); } }
public ActionResult Edit(ChoreDoerModel doerModel) { using (var context = new RotatingChoresContext()) { var doer = doerModel.GetRepresentedDoer(context); if (doer != null) { var group = GetUserGroup(context); doerModel.AddChoresList(doer, group); ValidateChores(doerModel); if (ModelState.IsValid && IsGroupObject(doer.GroupId)) { doerModel.UpdateDoer(doer); context.SaveChanges(); TempData["Message"] = "Chore doer profile has been updated!"; return(RedirectToAction("Index")); } return(View(doerModel)); } return(DoerNotFound()); } }
public ChoreDoer GetRepresentedDoer(RotatingChoresContext context) { var doer = context.ChoreDoers.SingleOrDefault(d => d.ChoreDoerId == ChoreDoerId); return(doer); }
//Uses the ChoreId of the model to get the represented data entity Chore. public Chore GetRepresentedChore(RotatingChoresContext context) { Chore chore = context.Chores.SingleOrDefault(c => c.ChoreId == ChoreId); return(chore); }