public ActionResult Edit(int id) { // Get existing co-op session entity from repository var coop = _unitOfWork.Coops.GetCoop(id); // Security checks on returned coop if (coop == null) { // Co-op session not found return(HttpNotFound()); } if (coop.HostId != User.Identity.GetUserId()) { // Co-op session does not belong to the current user return(new HttpUnauthorizedResult()); } var viewModel = new CoopFormViewModel { Heading = "Edit a Co-op Session", Id = coop.Id, Games = _unitOfWork.Games.GetGames(), Date = coop.DateTime.ToString("d MMM yyyy"), Time = coop.DateTime.ToString("HH:mm"), Game = coop.GameId, Venue = coop.Venue }; return(View("CoopForm", viewModel)); }
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")); }
public ActionResult Create() { var viewModel = new CoopFormViewModel { Heading = "Add a Co-op Session", Games = _unitOfWork.Games.GetGames() }; return(View("CoopForm", viewModel)); }