public ActionResult AllClasses(string letter, string format)
{
using (OdsRepository repository = new OdsRepository(HttpContext))
{
// TODO: Refactor the following code into its own method
// after reconciling the noted differences between AllClasses() and YearQuarter() - 4/27/2012, [email protected]
using (ClassScheduleDb db = new ClassScheduleDb())
{
// force CoursePrefixes to load up front for Subjects (otherwise we get an error trying to load after db has been disposed)
db.ContextOptions.LazyLoadingEnabled = false;
IList<Subject> subjects = db.Subjects.Include("CoursePrefixes").ToList();
IList<char> subjectLetters = subjects.Select(s => s.Title.First()).Distinct().ToList();
if (letter != null)
{
subjects = subjects.Where(s => s.Title.StartsWith(letter, StringComparison.OrdinalIgnoreCase)).ToList();
}
#if DEBUG
Debug.Print("======= Subject list =======");
foreach (Subject subject in subjects)
{
Debug.Print("{0} ({1})", subject.Title, subject.CoursePrefixes.Select(p => p.CoursePrefixID).ToArray().Mash(", "));
}
Debug.Print("===== END Subject list =====");
#endif
// Construct the model
AllClassesModel model = new AllClassesModel
{
CurrentQuarter = repository.CurrentYearQuarter,
NavigationQuarters = Helpers.GetYearQuarterListForMenus(repository),
Subjects = subjects.Select(s => new SubjectModel
{
Title = s.Title,
Slug = s.Slug,
CoursePrefixes = s.CoursePrefixes.Select(p => p.CoursePrefixID).ToList()
}).ToList(),
LettersList = subjectLetters,
ViewingLetter = String.IsNullOrEmpty(letter) ? (char?)null : letter.First()
};
if (format == "json")
{
// NOTE: AllowGet exposes the potential for JSON Hijacking (see http://haacked.com/archive/2009/06/25/json-hijacking.aspx)
// but is not an issue here because we are receiving and returning public (e.g. non-sensitive) data
return Json(model, JsonRequestBehavior.AllowGet);
}
// set up all the ancillary data we'll need to display the View
SetCommonViewBagVars(repository, string.Empty, letter);
ViewBag.LinkParams = Helpers.getLinkParams(Request);
return View(model);
}
}
}