Esempio n. 1
0
 public ActionResult <IEnumerable <Hero> > GetHeroes()
 {
     using (var db = new TourOfHeroesContext())
     {
         //  Return all Heroes
         return(db.Hero.ToList());
     }
 }
Esempio n. 2
0
        //public void DeleteHero(int id)
        public void DeleteHero([FromBody] Hero hero)
        {
            using (var db = new TourOfHeroesContext())
            {
                db.Hero.Remove(hero);

                db.SaveChanges();
            }
        }
Esempio n. 3
0
 public ActionResult <IEnumerable <Hero> > SearchHeroes(string term)
 {
     using (var db = new TourOfHeroesContext())
     {
         //  Search for Heroes matching Term
         return(db.Hero
                .Where(x => x.Name.Contains(term))
                .ToList());
     }
 }
Esempio n. 4
0
 public ActionResult <Hero> GetHero(int id)
 {
     using (var db = new TourOfHeroesContext())
     {
         //  Get Hero with Matching Id
         return(db.Hero
                .Where(x => x.Id == id)
                .FirstOrDefault());
     }
 }
Esempio n. 5
0
        public ActionResult <Hero> SaveHero([FromBody] Hero hero)
        {
            using (var db = new TourOfHeroesContext())
            {
                //  Determine state based off of Hero Id.
                //  Id = 0 is a new Hero.
                db.Entry(hero).State = hero.Id == 0 ? EntityState.Added
                                                    : EntityState.Modified;

                db.SaveChanges();

                return(hero);
            }
        }