// Central View for CRUD relating to shows or seasons // Lists all Shows public IActionResult ManageShows() { // Citation: //https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.controller.redirecttoaction?view=aspnet-mvc-5.2 // Redirect to Action in different Controllers // Ensure user is logged in if (!IsLoggedIn()) { return RedirectToAction("Login", "User"); } List<ShowSeason> Seasons; using (WeWatchContext context = new WeWatchContext()) { List<Show> allShows = new List<Show>(); allShows = context.Show.OrderBy(x => x.Title).ToList(); foreach (Show show in allShows) { Seasons = context.ShowSeason.Where(x => x.ShowID == show.ShowID).OrderBy(x => x.IndividualSeason).ToList(); } ViewBag.AllShows = allShows; } // Citation //https://stackoverflow.com/questions/14497711/set-viewbag-before-redirect // Used for transfering error/success messages between redirects. // Ensure that null coalesing is added to TempData before using the ToString() method to avoid null reference errors ViewBag.messages = TempData["message"]?.ToString(); return View(); }
public IActionResult DeleteProgram(int showID) { if (!IsLoggedIn()) { return RedirectToAction("Login", "User"); } string message; using (WeWatchContext context = new WeWatchContext()) { if (showID == 0) { message = "No program was provided."; } else { Show show = context.Show.Where(x => x.ShowID == showID).SingleOrDefault(); if (show == null) { message = "Cannot delete program. Please refresh and try again."; } else { // Store the title in a temp variable to reference in messages to the user string tempTitle = show.Title; context.Show.Remove(show); context.SaveChanges(); message = $"Successfully deleted {tempTitle}"; } } } TempData["message"] = message; return Redirect("ManageShows"); }
[HttpPost] //method runs on submit to add user to the Db public IActionResult SignUp(string email, string confirmedemail, string password, string confirmedpassword) { if (ValidateEmailPassword(email, password, confirmedemail, confirmedpassword)) { using WeWatchContext context = new WeWatchContext(); if (context.User.Where(x => x.Email.Trim().ToLower() == email.Trim().ToLower()).Count() > 0) { ViewBag.usedemail = "This email has already been used. Please log in."; return(View()); } else { // Creates a random salt value Random r = new Random(); string Salt = Convert.ToString(r.Next()); // Adds the salt value to the password and feeds that to the Hash method User newUser = new User() { Email = email.Trim().ToLower(), HashPassword = Hash(password + Salt), Salt = Salt }; context.User.Add(newUser); context.SaveChanges(); return(Redirect("Login")); } } else { return(View()); } }
// Edits a watcher in the Db public IActionResult EditWatcher(string watcherName, string watcherID) { // Ensure user is logged in if (!IsLoggedIn()) { return(RedirectToAction("Login", "User")); } string message = null; using (WeWatchContext context = new WeWatchContext()) { string tempName; int parsedWatcherID = int.Parse(watcherID); // Search for the watcher Watcher watcher = context.Watcher.Where(x => x.WatcherID == parsedWatcherID).SingleOrDefault(); tempName = watcher.Name; // Validate data if (watcher != null) { if (string.IsNullOrWhiteSpace(watcherName)) { message = "Name must contain characters"; } else if (watcherName.Trim().Length > 50) { message = "Name cannot be more than 50 characters"; } else if (watcher.Name.ToUpper() == watcherName.Trim().ToUpper()) { message = "No changes were detected"; } else if (context.Watcher.Where(x => x.Name.ToUpper() == watcherName.Trim().ToUpper()).Count() != 0) { message = $"{watcherName} already exists"; } // Save to Db else { watcher.Name = watcherName; context.SaveChanges(); message = $"Successfully updated {tempName} to {watcher.Name} "; } } else { message = "Cannot find watcher. Please refresh and try again."; } } TempData["message"] = message; return(RedirectToAction("ManageWatchers")); }
// Decreases the episode number for this specific showcard public IActionResult MinusEpisode(int showCardID) { int userID = GetUserID(); if (userID == 0) { return(RedirectToAction("Login", "User")); } string message = null; ShowCard show = new ShowCard(); using (WeWatchContext context = new WeWatchContext()) { // Get the show that the showcard is referencing show = context.ShowCard.Include(x => x.Show).Where(x => x.ShowCardID == showCardID).Single(); // Get the show season attached to that card ShowSeason currentSeason = context.ShowSeason.Where(x => x.ShowID == show.ShowID && x.ShowSeasonID == show.CurrentSeason).Single(); // Check if we are at the minimum episode of any season if (show.CurrentEpisode == 1) { // Check to see if there is a lower season recorded for this show ShowSeason pastSeason = context.ShowSeason.Include(x => x.Show).Where(x => x.ShowID == show.ShowID && x.IndividualSeason < currentSeason.IndividualSeason).OrderBy(x => x.IndividualSeason).FirstOrDefault(); // Prompt user to add a season to the list if there is no lower season if (pastSeason == null) { message = "Sorry you no season to go back to. Would you like to add a season to the list?"; } // Roll back to the next lowest season, max episode else { show.CurrentEpisode = pastSeason.SeasonEpisodes; show.CurrentSeason = pastSeason.ShowSeasonID; message = "You went back a SEASON"; } } // Can simply minus one episode from the episode number else { show.CurrentEpisode--; message = "Went back an EPISODE"; } context.SaveChanges(); } TempData["message"] = message; return(RedirectToAction("ByShow", new { showID = show.ShowID })); }
// Displays a list of all watchers this specific user has showcards with public IActionResult ByWatcher(int watcherID) { string message = TempData["message"]?.ToString(); int userID = GetUserID(); if (userID == 0) { return(RedirectToAction("Login", "User")); } List <DisplayCard> displayCards = new List <DisplayCard>(); List <Watcher> allUserWatchers = new List <Watcher>(); using (WeWatchContext context = new WeWatchContext()) { if (watcherID == 0) { message = "Unable to find that Watcher."; } else { // A list of all Show Cards with this Specific User and Watcher List <ShowCard> showCardsByWatcher = context.ShowCard.Include(x => x.Watcher).Where(x => x.WatcherID == watcherID && x.UserID == userID).ToList(); // A list of all Watchers that this Specific User has ShowCards with allUserWatchers = context.ShowCard.Include(x => x.Watcher).Where(x => x.UserID == userID).Select(x => x.Watcher).Distinct().ToList(); if (showCardsByWatcher == null) { message = "Looks like you are not watching a show with that Watcher"; } // If this User has ShowCards with this Watcher add them to the list of display cards if (showCardsByWatcher != null) { foreach (ShowCard showCard in showCardsByWatcher) { DisplayCard card = new DisplayCard(showCard.ShowID, showCard.WatcherID) { ShowCardID = showCard.ShowCardID, SeasonID = showCard.CurrentSeason, CurrentEpisode = showCard.CurrentEpisode }; card.ShowCardID = showCard.ShowCardID; displayCards.Add(card); } } } } ViewBag.Message = message; ViewBag.AllUserWatchers = allUserWatchers; ViewBag.DisplayCards = displayCards; return(View()); }
// Create the show card public IActionResult Connect(int showID, int watcherID, int seasonID, int episode) { int userID = GetUserID(); if (userID == 0) { return(RedirectToAction("Login", "User")); } string message = ""; // If we are missing any fields, redirect the user to the CreateCard Action if (showID == 0 || watcherID == 0 || seasonID == 0 || episode == 0) { return(RedirectToAction("CreateCard", new { showID, watcherID, seasonID, episode })); } using (WeWatchContext context = new WeWatchContext()) { // Check to see if this user already has a card with this watcher and this show if (context.ShowCard.Where(x => x.ShowID == showID && x.UserID == userID && x.WatcherID == watcherID).Count() > 0) { message = "You must fill in all fields before you can Watch"; return(Redirect("CreateCard")); } // Create the card with ShowCard newShowCard = new ShowCard() { ShowID = showID, WatcherID = watcherID, CurrentSeason = seasonID, CurrentEpisode = episode, UserID = userID, }; try { context.ShowCard.Add(newShowCard); context.SaveChanges(); message = "Success"; } catch { message = "Something went wrong. Your connection was not saved. Please refresh and try again."; } TempData["showCardID"] = (int)context.ShowCard.Where(x => x.ShowID == showID && x.UserID == userID && x.WatcherID == watcherID).Select(x => x.ShowCardID).Single(); TempData["message"] = message; } return(Redirect("CreateCard")); }
public IActionResult AddSeason(int showID, string newSeason, string newEpisodes) { if (!IsLoggedIn()) { return RedirectToAction("Login", "User"); } string message = null; using (WeWatchContext context = new WeWatchContext()) { Show show = context.Show.Where(x => x.ShowID == showID).SingleOrDefault(); if (!int.TryParse(newSeason, out int parsedNewSeason)) { message = "Seasons must be positive valid numbers."; } else if (parsedNewSeason < 1 || parsedNewSeason > 50) { message = "Season must be between 1 and 50."; } else if (!int.TryParse(newEpisodes, out int parsedNewEpisodes)) { message = "Episodes must be positive valid numbers."; } else if (parsedNewEpisodes < 1 || parsedNewEpisodes > 50) { message = "Episodes must be between 1 and 50."; } else if (context.ShowSeason.Where(x => x.ShowID == showID).Where(x => x.IndividualSeason == parsedNewSeason).SingleOrDefault() != null) { message = $"Season already exists in {show.Title}"; } else { ShowSeason showSeason = new ShowSeason() { ShowID = showID, IndividualSeason = parsedNewSeason, SeasonEpisodes = parsedNewEpisodes }; context.ShowSeason.Add(showSeason); context.SaveChanges(); message = $"Successfully added season { parsedNewSeason } to {show.Title}"; } } TempData["message"] = message; return Redirect("ManageShows"); }
// Verifies wether the users name and password match what is stored in the Db public IActionResult Login(string email, string password) { if (email == null && password == null) { ViewBag.erroremailpassword = "******"; } else if (email == null) { ViewBag.erroremail = "Please enter an email address."; } else if (password == null) { ViewBag.errorpassword = "******"; } if (ModelState.IsValid) { if (email == null) { ViewBag.email = "Please enter an email address."; } else { using WeWatchContext context = new WeWatchContext(); // checking the inputted email against what's in our db User potentialUser = context.User.Where(x => x.Email == email.Trim().ToLower()).SingleOrDefault(); // grab the salt value from that specific user if (potentialUser != null) { string Salt = potentialUser.Salt; // we need to check the password that they have inputted + salt value matches what's in their hashpassword in the db if (Hash(password + Salt) == potentialUser.HashPassword) { HttpContext.Session.SetString("isLoggedIn", "true"); HttpContext.Session.SetInt32("User", potentialUser.UserID); return(RedirectToAction("Shows", "ShowCard")); } ViewBag.errorwronglogin = "******"; return(View()); // change to show page } } } // default view return(View()); }
public IActionResult EditTitle(string title, int showID) { if (!IsLoggedIn()) { return RedirectToAction("Login", "User"); } string message; using (WeWatchContext context = new WeWatchContext()) { if (showID == 0) { message = "No program was provided."; } else if (title == null) { message = "No title was provided."; } else { title = title.Trim(); Show show = context.Show.Where(x => x.ShowID == showID).SingleOrDefault(); if (show == null) { message = "Program not found. Please refresh and try again."; } else if (title.Count() > 50) { message = "Program name cannot be more than 50 characters."; } else if (show.Title.ToUpper() == title.ToUpper()) { message = $"No changes were detect for program title {title}."; } else if (context.Show.Where(x => x.Title.ToUpper() == title.ToUpper()).Count() > 0) { message = $"{title} already exists."; } else { string tempTitle = show.Title; show.Title = title; context.SaveChanges(); message = $"Successfully updated '{tempTitle}' to '{title}'."; } } } TempData["message"] = message; return Redirect("ManageShows"); }
// Display a list of all watchers that this specific user has showcards with public IActionResult Watchers() { int userID = GetUserID(); // If the userId was not found(0) redirect to the login page if (userID == 0) { return(RedirectToAction("Login", "User")); } List <Watcher> allShowCardWatchers = new List <Watcher>(); using (WeWatchContext context = new WeWatchContext()) { allShowCardWatchers = context.ShowCard.Include(x => x.Watcher).Where(x => x.UserID == userID).Select(x => x.Watcher).Distinct().ToList(); } ViewBag.AllShowCardWatchers = allShowCardWatchers; return(View()); }
public IActionResult DeleteSeason(int deleteSeason) { if (!IsLoggedIn()) { return RedirectToAction("Login", "User"); } string messages; using (WeWatchContext context = new WeWatchContext()) { string tempTitle; // Make sure a value was passed for the seasonID if (deleteSeason == 0) { messages = "Cannot delete unknown program. Please refresh and try again."; } else { // Check to see if the passed value matches a seasonID ShowSeason season = context.ShowSeason.Where(x => x.ShowSeasonID == deleteSeason).SingleOrDefault(); if (season == null) { messages = "Cannot find program. Please refresh and try again."; } // Check that if there is a show card with that season referenced as watching else if (context.ShowCard.Where(x => x.CurrentSeason == season.ShowSeasonID).Count() > 0) { messages = "Cannot delete a season with a Show Card reference."; } // We can delete the season else { tempTitle = context.Show.Where(x => x.ShowID == season.ShowID).Select(y => y.Title).SingleOrDefault().ToString(); context.ShowSeason.Remove(season); context.SaveChanges(); messages = $"Successfully deleted Season number {season.IndividualSeason} from {tempTitle}"; } } } // Return messages TempData["message"] = messages; return Redirect("ManageShows"); }
// Central view for CRUD actions on Watcher public IActionResult ManageWatchers() { // Ensure user is logged in if (!IsLoggedIn()) { return(RedirectToAction("Login", "User")); } List <Watcher> watchers; using (WeWatchContext context = new WeWatchContext()) { //List of all watchers in the Db watchers = context.Watcher.OrderBy(x => x.Name).ToList(); } ViewBag.AllWatchers = watchers; ViewBag.Message = TempData["message"]?.ToString(); return(View()); }
public IActionResult AddShow(string title, int indSeason, int episodes) { if (!IsLoggedIn()) { return RedirectToAction("Login", "User"); } string messages; using (WeWatchContext context = new WeWatchContext()) { if (title.Length > 50) { messages = ("Program name cannot be greater than 50 characters."); } else if (context.Show.Where(x => x.Title.ToUpper() == title.ToUpper().Trim()).Count() > 0) { messages = ($"{title} already exists."); } else if (indSeason < 1 || indSeason > 50) { messages = ("Seasons must be between 1 and 50."); } else if (episodes < 1) { messages = ("Episodes must be between 1 and 50."); } else { Show newShow = new Show() { Title = title.Trim() }; context.Show.Add(newShow); context.SaveChanges(); int showID = context.Show.Where(x => x.Title == title.Trim()).Single().ShowID; ShowSeason newSeason = new ShowSeason() { ShowID = showID, IndividualSeason = indSeason, SeasonEpisodes = episodes }; context.ShowSeason.Add(newSeason); context.SaveChanges(); messages = ($"Successfully added {newShow.Title}"); } } TempData["message"] = messages; return Redirect("ManageShows"); }
// Increases the Episode number on a specific showcard public IActionResult AddEpisode(int showCardID) { int userID = GetUserID(); if (userID == 0) { return(RedirectToAction("Login", "User")); } string message = null; ShowCard show = new ShowCard(); using (WeWatchContext context = new WeWatchContext()) { show = context.ShowCard.Include(x => x.Show).Where(x => x.ShowCardID == showCardID).Single(); ShowSeason currentSeason = context.ShowSeason.Where(x => x.ShowID == show.ShowID && x.ShowSeasonID == show.CurrentSeason).Single(); if (show.CurrentEpisode >= currentSeason.SeasonEpisodes) { ShowSeason nextSeason = context.ShowSeason.Include(x => x.Show).Where(x => x.ShowID == show.ShowID && x.IndividualSeason > currentSeason.IndividualSeason).OrderBy(x => x.IndividualSeason).FirstOrDefault(); if (nextSeason == null) { message = "Sorry you have reached the end. Would you like to add another season?"; } else { show.CurrentEpisode = 1; show.CurrentSeason = nextSeason.ShowSeasonID; message = "You started a new Season!"; } } else { show.CurrentEpisode++; message = "Next episode"; } context.SaveChanges(); } TempData["message"] = message; return(RedirectToAction("ByShow", new { showID = show.ShowID })); }
// Displays a list of ShowCards for this specific user and the provided show public IActionResult ByShow(int showID) { string message = TempData["message"]?.ToString(); int userID = GetUserID(); if (userID == 0) { return(RedirectToAction("Login", "User")); } List <DisplayCard> displayCards = new List <DisplayCard>(); List <Show> allUserShows = new List <Show>(); using (WeWatchContext context = new WeWatchContext()) { List <ShowCard> showCardsByShow = context.ShowCard.Include(x => x.Show).Where(x => x.ShowID == showID && x.UserID == userID).ToList(); allUserShows = context.ShowCard.Include(x => x.Show).Where(x => x.UserID == userID).Select(x => x.Show).Distinct().ToList(); if (showCardsByShow != null) { foreach (ShowCard showCard in showCardsByShow) { DisplayCard card = new DisplayCard(showCard.ShowID, showCard.WatcherID) { ShowCardID = showCard.ShowCardID, SeasonID = showCard.CurrentSeason, CurrentEpisode = showCard.CurrentEpisode }; card.ShowCardID = showCard.ShowCardID; displayCards.Add(card); } } } ViewBag.Message = message; ViewBag.AllUserShows = allUserShows; ViewBag.DisplayCards = displayCards; return(View()); }
// Adds a watcher to the Db public IActionResult AddWatcher(string name) { // Ensure user is logged in if (!IsLoggedIn()) { return(RedirectToAction("Login", "User")); } string message = null; using (WeWatchContext context = new WeWatchContext()) { if (string.IsNullOrWhiteSpace(name)) { message = "Please enter a name for the Watcher you want to add."; } else if (name.Trim().Length > 50) { message = "Name cannot be more than 50 characters"; } else if (context.Watcher.Where(x => x.Name.ToUpper() == name.Trim().ToUpper()).Count() > 0) { message = $"You already have {name} is your list"; } else { Watcher newWatcher = new Watcher() { Name = name.Trim() }; context.Watcher.Add(newWatcher); context.SaveChanges(); message = $"Successfully added {name} to your Watcher list."; } } TempData["message"] = message; return(RedirectToAction("ManageWatchers")); }
// Deletes a watcher from the Db public IActionResult DeleteWatcher(int watcherID) { if (!IsLoggedIn()) { return(RedirectToAction("Login", "User")); } string message = null; if (watcherID == 0) { message = "No program was provided."; } else { using WeWatchContext context = new WeWatchContext(); Watcher watcher = context.Watcher.Where(x => x.WatcherID == watcherID).SingleOrDefault(); if (watcher == null) { message = "Cannot delete watcher. Please try again later."; } else { string tempName = watcher.Name; context.Watcher.Remove(watcher); context.SaveChanges(); message = $"Successfully deleted {tempName}"; } } TempData["message"] = message; return(RedirectToAction("ManageWatchers")); }
public IActionResult EditSeason(int editSeason, string season, string episodes) { if (!IsLoggedIn()) { return RedirectToAction("Login", "User"); } using WeWatchContext context = new WeWatchContext(); string message; int tempSeason; int tempEpisodes; if (editSeason == 0) { message = "Cannot edit unknown season. Please refresh and try again"; } else { ShowSeason targetSeason = context.ShowSeason.Where(x => x.ShowSeasonID == editSeason).SingleOrDefault(); if (season == null) { message = "Cannot find that season. Please refresh and try again"; } else { Show show = context.Show.Where(x => x.ShowID == targetSeason.ShowID).SingleOrDefault(); if (show == null) { message = "Cannot find that program. Please refresh and try again."; } else { if (!int.TryParse(season, out int parsedNewSeason)) { message = "Seasons must be positive valid numbers"; } else if (parsedNewSeason < 1 || parsedNewSeason > 50) { message = "Season must be between 1 and 50"; } else if (!int.TryParse(episodes, out int parsedNewEpisodes)) { message = "Episodes must be positive valid numbers"; } else if (parsedNewEpisodes < 1 || parsedNewEpisodes > 50) { message = "Episodes must be between 1 and 50"; } // To avoid giving a false error when applying the no season duplicate constraint if the user is only editting the episodes, we must first check that the new season and the season that they are attempting to submit are not the same. If they are not we can continue to check if there is a duplicate. else if (parsedNewSeason != targetSeason.IndividualSeason && context.ShowSeason.Where(x => x.ShowID == targetSeason.ShowID).Where(x => x.IndividualSeason == parsedNewSeason).SingleOrDefault() != null) { message = $"Season already exists in {show.Title}"; } else if (parsedNewSeason == targetSeason.IndividualSeason && parsedNewEpisodes == targetSeason.SeasonEpisodes) { message = $"No Changes were detected for {show.Title} - Season {targetSeason.IndividualSeason}."; } else if (context.ShowCard.Where(x => x.CurrentSeason == targetSeason.ShowSeasonID).Count() > 0) { message = "Cannot edit a season with a Show Card reference."; } else { tempSeason = targetSeason.IndividualSeason; tempEpisodes = targetSeason.SeasonEpisodes; targetSeason.IndividualSeason = parsedNewSeason; targetSeason.SeasonEpisodes = parsedNewEpisodes; context.SaveChanges(); message = $"Successfully changed {show.Title} from season {tempSeason} having {tempEpisodes} episodes to season {parsedNewSeason} having {parsedNewEpisodes} episodes."; } } } } TempData["message"] = message; return Redirect("ManageShows"); }
// Ensure all the neccessary information to create a show card is received public IActionResult CreateCard(int showID, int watcherID, int seasonID, int episode) { string message = TempData["message"]?.ToString(); int userID = GetUserID(); if (userID == 0) { return(RedirectToAction("Login", "User")); } using WeWatchContext context = new WeWatchContext(); Show selectedShow = new Show(); ShowSeason selectedSeason = new ShowSeason(); Watcher selectedWatcher = new Watcher(); List <ShowSeason> seasons = new List <ShowSeason>(); DisplayCard card = new DisplayCard(); // A show and a watcher must be selected before seasons can be determined if (showID != 0 && watcherID != 0) { card = new DisplayCard(showID, watcherID); // Check to see if this user already has a card with this watcher and this show if (context.ShowCard.Where(x => x.ShowID == card.ShowID && x.WatcherID == card.WatcherID && x.UserID == userID).Count() != 0) { message = $"You are already watching {card.ShowTitle} with {card.WatcherName}"; } // Populate the seasons for this show else { seasons = context.ShowSeason.Where(x => x.ShowID == showID).OrderBy(x => x.IndividualSeason).ToList(); // Populate episodes if season has been selected if (seasonID != 0) { card.SeasonID = seasonID; selectedSeason = context.ShowSeason.Where(x => x.ShowSeasonID == seasonID).Single(); card.CurrentEpisode = episode; } } } // If we were passed a ShowCardID from another action, create a displayCard if (TempData["showCardID"] != null) { int showCardID = int.Parse(TempData["showCardID"].ToString()); ShowCard show = context.ShowCard.Where(x => x.ShowCardID == showCardID).Single(); card = new DisplayCard(show.ShowID, show.WatcherID) { SeasonID = show.CurrentSeason, CurrentEpisode = show.CurrentEpisode }; } ViewBag.Card = card; List <Show> allShows = context.Show.OrderBy(x => x.Title).ToList(); List <Watcher> allWatchers = context.Watcher.OrderBy(x => x.Name).ToList(); ViewBag.Message = message; ViewBag.AllShows = allShows; ViewBag.AllWatchers = allWatchers; ViewBag.Seasons = seasons; return(View()); }