public ActionResult Letters(LetterModel letter) { ClearAlerts("Alert Letters M", 3); // get the list of letters from Session List <LetterModel> letters = getLetters(); // we'll need to validate the data if (letters.Exists(l => l.Name == letter.Name)) { Session.Add("Alert Letters M3", "Letter name is already used. Please change the letter name."); return(View()); } // we need the letter type id to create the letter List <LetterTypeModel> lettertypes = (List <LetterTypeModel>)Session["Letter Types"]; int lettertypeid = lettertypes.Find(lt => lt.Name == letter.LetterType).Id; letter.Id = LetterProcessor.CreateLetter(letter.Name, (int)Session["Language"], lettertypeid, letter.Pronounciation, letter.Description); letters.Add(letter); // lastly, we'll need to clear the values in the field ModelState.SetModelValue("Name", new ValueProviderResult("", "", ModelState["Name"].Value.Culture)); ModelState.SetModelValue("Pronounciation", new ValueProviderResult("", "", ModelState["Pronounciation"].Value.Culture)); ModelState.SetModelValue("Description", new ValueProviderResult("", "", ModelState["Description"].Value.Culture)); return(View()); }
public ActionResult RemoveLetter(int id) { // get the list of letters from Session List <LetterModel> letters = getLetters(); letters.RemoveAt(letters.FindIndex(l => l.Id == id)); LetterProcessor.DeleteLetter(id); return(RedirectToAction("Letters", "Language")); }
private List <LetterModel> getLetters() { List <LetterModel> letters = new List <LetterModel>(); // the fail case for this method is if there is no selected language if (Session["Language"] == null) { return(letters); } // check if Session already has a letters list if (Session["Letters"] != null) { letters = (List <LetterModel>)Session["Letters"]; // now, if the list isn't empty return it if (letters.Count != 0) { return(letters); } } // otherwise, Session does not have a letters list object, so we'll instead start checking the database // now load the letters from the database List <LetterDataModel> dmLetters = LetterProcessor.LoadLetters((int)Session["Language"]); foreach (LetterDataModel letter in dmLetters) { letters.Add(new LetterModel { Id = letter.Id, Name = letter.Name, Pronounciation = letter.Pronounciation, Description = letter.Description }); } // now make sure Session has letters list if (Session["Letters"] != null) { Session["Letters"] = letters; } else { Session.Add("Letters", letters); } return(letters); }
// 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 Letter Views #region Letters public ActionResult Letters() { ClearAlerts("Alert Letters 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 letter as well List <LetterTypeModel> lettertypes = getLetterTypes(); // another fail case is if there are no letter types - letters require a letter type if (lettertypes.Count == 0) { return(RedirectToAction("Index", "Language")); } List <string> slettertypes = new List <string>(); foreach (LetterTypeModel lt in lettertypes) { slettertypes.Add(lt.Name); } SelectList selectlettertypes = new SelectList(slettertypes); if (Session["Letter Types SelectList"] != null) { Session["Letter Types SelectList"] = selectlettertypes; } else { Session.Add("Letter Types SelectList", selectlettertypes); } // we need the list of letters List <LetterModel> letters = getLetters(); // check if the list has values - if so, we'll return the view if (letters.Count != 0) { return(View()); } // at this point, we know there are letter types and there are no letters, so next we want to check if consonant and vowel exist as letter types if (lettertypes.Exists(lt => lt.Name == "Consonant") && lettertypes.Exists(lt => lt.Name == "Vowel")) { int consonantid = lettertypes.Find(lt => lt.Name == "Consonant").Id; int vowelid = lettertypes.Find(lt => lt.Name == "Vowel").Id; // create some letters to the database int languageid = (int)Session["Language"]; LetterProcessor.CreateLetter("A", languageid, vowelid, "ae", "Vowel A"); LetterProcessor.CreateLetter("E", languageid, vowelid, "ee", "Vowel E"); LetterProcessor.CreateLetter("I", languageid, vowelid, "ai", "Vowel I"); LetterProcessor.CreateLetter("O", languageid, vowelid, "ou", "Vowel O"); LetterProcessor.CreateLetter("U", languageid, vowelid, "yu", "Vowel U"); LetterProcessor.CreateLetter("B", languageid, consonantid, "b", "Consonant B"); LetterProcessor.CreateLetter("C", languageid, consonantid, "c", "Consonant C"); LetterProcessor.CreateLetter("D", languageid, consonantid, "d", "Consonant D"); LetterProcessor.CreateLetter("F", languageid, consonantid, "f", "Consonant F"); LetterProcessor.CreateLetter("G", languageid, consonantid, "g", "Consonant G"); LetterProcessor.CreateLetter("H", languageid, consonantid, "h", "Consonant H"); LetterProcessor.CreateLetter("J", languageid, consonantid, "j", "Consonant J"); LetterProcessor.CreateLetter("L", languageid, consonantid, "l", "Consonant L"); LetterProcessor.CreateLetter("M", languageid, consonantid, "m", "Consonant M"); LetterProcessor.CreateLetter("N", languageid, consonantid, "n", "Consonant N"); LetterProcessor.CreateLetter("P", languageid, consonantid, "p", "Consonant P"); LetterProcessor.CreateLetter("R", languageid, consonantid, "r", "Consonant R"); LetterProcessor.CreateLetter("S", languageid, consonantid, "s", "Consonant S"); LetterProcessor.CreateLetter("T", languageid, consonantid, "t", "Consonant T"); LetterProcessor.CreateLetter("V", languageid, consonantid, "v", "Consonant V"); LetterProcessor.CreateLetter("W", languageid, consonantid, "w", "Consonant W"); LetterProcessor.CreateLetter("Y", languageid, consonantid, "y", "Consonant Y"); LetterProcessor.CreateLetter("Z", languageid, consonantid, "z", "Consonant Z"); // now load the letters we just created - we do it this way so that we have the correct id List <LetterDataModel> dmLetters = LetterProcessor.LoadLetters(languageid); // now populate letters foreach (LetterDataModel letter in dmLetters) { letters.Add(new LetterModel { Id = letter.Id, Name = letter.Name, Pronounciation = letter.Pronounciation, Description = letter.Description }); } // reassign letters to Session Session["Letters"] = letters; // lastly, add an alert Session.Add("Alert Letters M1", "Default letters have been added as vowels and consonants. Remove if unwanted."); } else { Session.Add("Alert Letters M2", "You have no letters. Please create your own letters based on your letter types."); } // return the view as a table of the letters - note that if we didn't have the consonant and vowel letter types, we're returning an empty list return(View()); }