public ActionResult Create(CoopFormViewModel viewModel) { // Check that model state is valid if (!ModelState.IsValid) { // Set the Games property of view model viewModel.Games = _unitOfWork.Games.GetGames(); return(View("CoopForm", viewModel)); } var coop = new Coop { HostId = User.Identity.GetUserId(), DateTime = viewModel.GetDateTime(), GameId = viewModel.Game, Venue = viewModel.Venue }; // Add the new co-op session _unitOfWork.Coops.Add(coop); // Complete the transaction _unitOfWork.Complete(); return(RedirectToAction("Mine", "Coops")); }
public ActionResult Update(CoopFormViewModel viewModel) { // Check that model state is valid if (!ModelState.IsValid) { // Set the Games property of view model viewModel.Games = _unitOfWork.Games.GetGames(); return(View("CoopForm", viewModel)); } // Get existing Co-op session entity from DB (use eager loading to get coop + all of it's attendees) var coop = _unitOfWork.Coops.GetCoopWithAttendees(viewModel.Id); // Security checks on return coop if (coop == null) { // Coop not found return(HttpNotFound()); } if (coop.HostId != User.Identity.GetUserId()) { // Co-op session does not belong to the current user return(new HttpUnauthorizedResult()); } // Update Coop coop.Modify(viewModel.GetDateTime(), viewModel.Venue, viewModel.Game); // Complete the transaction _unitOfWork.Complete(); return(RedirectToAction("Mine", "Coops")); }