public ActionResult RemoveWordPattern(int id) { // get the list of word patterns from Session List <WordPatternModel> wordpatterns = getWordPatterns(); wordpatterns.RemoveAt(wordpatterns.FindIndex(wp => wp.Id == id)); WordPatternProcessor.DeleteWordPattern(id); return(RedirectToAction("Index", "Languages")); }
private List <WordPatternModel> getWordPatterns() { List <WordPatternModel> wordpatterns = new List <WordPatternModel>(); // the fail case for this method is if there is no selected language if (Session["Language"] == null) { return(wordpatterns); } // check if Session already has a word patterns list if (Session["Word Patterns"] != null) { wordpatterns = (List <WordPatternModel>)Session["Word Patterns"]; // now, if the list isn't empty return it if (wordpatterns.Count != 0) { return(wordpatterns); } } // otherwise, Session does not have a word patterns list object, so we'll instead start checking the database // now load the word patterns from the database List <WordPatternDataModel> dmWordPatterns = WordPatternProcessor.LoadWordPatterns((int)Session["Language"]); foreach (WordPatternDataModel wordpattern in dmWordPatterns) { wordpatterns.Add(new WordPatternModel { Id = wordpattern.Id, Name = wordpattern.Name, Pattern = wordpattern.Pattern }); } // now make sure Session has word patterns list if (Session["Word Patterns"] != null) { Session["Word Patterns"] = wordpatterns; } else { Session.Add("Word Patterns", wordpatterns); } return(wordpatterns); }
// GET: Language public ActionResult Index() { // this action result is more for redirecting properly // first we need to check if there's a user if (Session["User"] == null) { // if there's no user, we'll need add an alert and also redirect to the login page Session.Add("No User", true); return(RedirectToAction("Login", "User")); } // now we need to check if a language has been selected if (Session["Language"] == null) { // if there's no language, we'll need to add an alert and also redirect to the languages page Session.Add("No Language", true); return(RedirectToAction("Languages", "Language")); } // so at this point, we know there's a user and language selected // we need to make several checks as the word maker doesn't work if we don't have letter types, letters, word patterns or classifications int languageid = Int32.Parse(Session["Language"].ToString()); // check if there are any letter types if (LetterTypeProcessor.getLetterTypesCount(languageid) <= 0) { return(RedirectToAction("LetterTypes", "Language")); } // check if there are any letters if (LetterProcessor.getLettersCount(languageid) <= 0) { return(RedirectToAction("Letters", "Language")); } // check if there are any word patterns if (WordPatternProcessor.getWordPatternsCount(languageid) <= 0) { return(RedirectToAction("WordPatterns", "Language")); } // check if there are any classifications if (ClassificationProcessor.getClassificationsCount(languageid) <= 0) { return(RedirectToAction("Classifications", "Language")); } // otherwise, we'll just go the the words page else { return(RedirectToAction("Words", "Language")); } }
// Action Result methods for Word Pattern Views #region WordPatterns public ActionResult WordPatterns() { ClearAlerts("Alert Word Patterns M", 3); // the fail cases for this page is if there isn't a User or a language isn't selected - redirect to Index if (Session["User"] == null || Session["Language"] == null) { return(RedirectToAction("Index", "Language")); } // we need the list of letter types - this is needed to create a word pattern as well List <LetterTypeModel> lettertypes = getLetterTypes(); // another fail case is if there are no letter types - word patterns require letter types if (lettertypes.Count == 0) { return(RedirectToAction("Index", "Language")); } // this page needs to return a list of word patterns List <WordPatternModel> wordpatterns = getWordPatterns(); // check if the list has values - if so, we'll return the view if (wordpatterns.Count != 0) { return(View()); } // we now know that there are no word patterns, so we'll attempt to create some default word patterns // check specifically for letter types vowel as pattern 'v' and consonant as pattern 'c' if (lettertypes.Exists(lt => lt.Name == "Vowel" && lt.Pattern == 'v') && lettertypes.Exists(lt => lt.Name == "Consonant" && lt.Pattern == 'c')) { int languageid = (int)Session["Language"]; WordPatternProcessor.CreateWordPattern("Pattern 1", "vc", languageid); WordPatternProcessor.CreateWordPattern("Pattern 2", "cv", languageid); WordPatternProcessor.CreateWordPattern("Pattern 3", "vcv", languageid); WordPatternProcessor.CreateWordPattern("Pattern 4", "cvc", languageid); WordPatternProcessor.CreateWordPattern("Pattern 5", "vcvc", languageid); WordPatternProcessor.CreateWordPattern("Pattern 6", "cvcv", languageid); WordPatternProcessor.CreateWordPattern("Pattern 7", "vccv", languageid); WordPatternProcessor.CreateWordPattern("Pattern 8", "cvvc", languageid); WordPatternProcessor.CreateWordPattern("Pattern 9", "cvcvc", languageid); WordPatternProcessor.CreateWordPattern("Pattern 10", "cvvcv", languageid); WordPatternProcessor.CreateWordPattern("Pattern 11", "cvccv", languageid); WordPatternProcessor.CreateWordPattern("Pattern 12", "cvcvv", languageid); WordPatternProcessor.CreateWordPattern("Pattern 13", "vcvcv", languageid); WordPatternProcessor.CreateWordPattern("Pattern 14", "vccvc", languageid); WordPatternProcessor.CreateWordPattern("Pattern 15", "vcvvc", languageid); // load the word patterns from the database and populate the list List <WordPatternDataModel> dmWordPatterns = WordPatternProcessor.LoadWordPatterns(languageid); foreach (WordPatternDataModel wordpattern in dmWordPatterns) { wordpatterns.Add(new WordPatternModel { Id = wordpattern.Id, Name = wordpattern.Name, Pattern = wordpattern.Pattern }); } // reassign word patterns to Session Session["Word Patterns"] = wordpatterns; // lastly, add an alert indicating that word patterns were added by default Session.Add("Alert Word Patterns M1", "Word patterns have been added by default, up to five letters, using vowels and consonants."); } // otherwise just add an alert asking the user to create their own word patterns else { Session.Add("Alert Word Patterns M1", "You have no word patterns - please create your own based on your letter patterns."); } // return the view as a table of the word patterns return(View()); }