public ActionResult Index()
 {
     using (var db = MonstersRepository.Get())
     {
         return(View(new Tuple <IEnumerable <string>, IEnumerable <Monster> >(db.Categories, db.Monsters)));
     }
 }
Beispiel #2
0
 public ActionResult List()
 {
     using (var db = MonstersRepository.Get())
     {
         return(View("List", Tuple.Create(db.Monsters.AsEnumerable(), "")));
     }
 }
Beispiel #3
0
        public ViewResult Search(string query, AdvancedSearchOptions opt)
        {
            var q       = (query ?? "").Trim().ToLower();
            var options = opt ?? new AdvancedSearchOptions();
            IEnumerable <Monster> results;

            if (string.IsNullOrWhiteSpace(query))
            {
                results = null;
            }
            else
            {
                var categories = options.Categories ?? new string[0];
                results = MonstersRepository.Get().Monsters
                          .Where(m =>
                                 (m.Name.ToLower().Contains(q) || m.Description.ToLower().Contains(q)) &&
                                 (!options.IsDangerous || m.IsDangerous) &&
                                 (categories.All(cat => m.Categories.Contains(cat))));
            }

            this.ViewBag.AllCategories = MonstersRepository.Get().Categories;
            return(View("Search", new SearchFormModel()
            {
                SearchString = q, Options = options, Results = results
            }));
        }
Beispiel #4
0
 public ActionResult ListCategory(string category)
 {
     using (var db = MonstersRepository.Get())
     {
         var monstersInCategory = db.Monsters.Where(m => m.Categories.Contains(category));
         return(View("List", Tuple.Create(monstersInCategory, category)));
     }
 }
Beispiel #5
0
 public ActionResult ShowExisting(int id)
 {
     using (var db = MonstersRepository.Get())
     {
         var monster = db.Monsters.FirstOrDefault(m => m.Id == id);
         if (monster == null)
         {
             return(HttpNotFound("Cannot find monster " + id));
         }
         return(MonsterView(monster));
     }
 }
Beispiel #6
0
        public ActionResult Delete(int id)
        {
            using (var db = MonstersRepository.Get())
            {
                var monster = db.Monsters.FirstOrDefault(m => m.Id == id);
                if (monster != null)
                {
                    db.Monsters.Remove(monster);
                }
            }

            return(this.RedirectTo(SiteUrls.MonsterList));
        }
Beispiel #7
0
        public ActionResult SaveNewOrExisting(int?id, Monster monster)
        {
            if (!this.ModelState.IsValid)
            {
                return(MonsterView(monster));
            }

            using (var db = MonstersRepository.Get())
            {
                // 'Add' works here even if it already exists in the collection--SaveChanges fixes things up.
                // (It's not a real application anyway:) Use the correct data access type shenanigans for your app.
                db.Monsters.Add(monster);
                db.SaveChanges();
            }

            return(this.RedirectTo(SiteUrls.MonsterDetail[monster.Id]));
        }
Beispiel #8
0
 public ViewResult MonsterView(Monster monster)
 {
     this.ViewBag.AllCategories = MonstersRepository.Get().Categories;
     return(View("Detail", monster));
 }