// GET: Home public ActionResult Index(ShowModel show, string networkFilter, string orderBy, bool orderAscending = false) { ShowRepository repo = new ShowRepository(); List<ShowModel> shows; if (ModelState.IsValidField("Title")) shows = repo.Find(x => x.Title.ToLower().Contains(show.Title), 100); else shows = repo.Find(x => x.Title != null, 100); // pretty terrible filter/order solution, fix if it ever starts to be an issue... ViewBag.networksList = new SelectList(shows.GroupBy(x => x.Network).Select(x => x.First().Network)); if (!string.IsNullOrWhiteSpace(networkFilter)) shows = shows.FindAll(x => x.Network == networkFilter); switch (orderBy) { case "title": if (orderAscending) shows = shows.OrderBy(x => x.Title).ToList(); else shows = shows.OrderByDescending(x => x.Title).ToList(); break; case "date": if (orderAscending) shows = shows.OrderBy(x => x.ReleaseDate).ToList(); else shows = shows.OrderByDescending(x => x.ReleaseDate).ToList(); break; case "network": if (orderAscending) shows = shows.OrderBy(x => x.Network).ToList(); else shows = shows.OrderByDescending(x => x.Network).ToList(); break; case "seasons": if (orderAscending) shows = shows.OrderBy(x => x.Seasons).ToList(); else shows = shows.OrderByDescending(x => x.Seasons).ToList(); break; case "episodes": if (orderAscending) shows = shows.OrderBy(x => x.Episodes).ToList(); else shows = shows.OrderByDescending(x => x.Episodes).ToList(); break; } return View(shows); }
public ActionResult Delete(ShowModel show) { if (ModelState.IsValidField("Id")) { if (repo.Get(show.Id) != null) { repo.Delete(show.Id); ViewBag.Success = true; } else return RedirectToAction("Index", "Home"); } return View(show); }
public ActionResult Create(ShowModel show) { if (ModelState.IsValid) { if (repo.Get(show.Id) == null) { repo.Insert(show); ViewBag.Success = true; } else return RedirectToAction("Index", "Home"); } return View(show); }
public ActionResult Create(string title) { /* The title parameter allows user to pass us the value in the URL. As a potential convenience, as it will pre-fill the form. Because we fill out the model with that date, then pass the model to the view, and that will pre-fill the form. */ ShowModel show = new ShowModel { Id = ShowRepository.GenerateId(), Title = title }; return View(show); }
/// <summary> /// Replace an existing show with the given show. /// The show to replace is matched using the Id in the given show. /// </summary> /// <param name="show">The show with the Id and new data to replace.</param> public void Replace(ShowModel show) { collection.ReplaceOne(x => x.Id == show.Id, show); }
/// <summary> /// Insert new show, /// </summary> /// <param name="show">The populated data model to insert. Throws ArgumentNullException on null.</param> public void Insert(ShowModel show) { if (show == null) throw new ArgumentNullException("show"); collection.InsertOne(show); }