//GET api/<controller> public IEnumerable <Models.HeroApi> GetAllHeroes() { using (var context = new HeroesDalEntities()) { return(MapFromDAL(context.Heroes.ToList())); } }
// GET api/<controller>/5 public IHttpActionResult GetHero(int id) { using (var context = new HeroesDalEntities()) { var hero = context.Heroes.ToList().FirstOrDefault(p => p.Id == id); if (hero == null) { return(NotFound()); } return(Ok(MapFromDAL(hero))); } }
// Get Searched Heroes public IHttpActionResult SearchHeroes(string term) { using (var context = new HeroesDalEntities()) { var hero = (from h in context.Heroes where h.Name.Contains(term) select h).ToList(); if (hero == null) { return(NotFound()); } return(Ok(MapFromDAL(hero))); } }
//// PUT api/<controller>/5 //public void Put(int id, [FromBody]string value) //{ //} // DELETE api/<controller>/5 public IHttpActionResult Delete(int id) { using (var context = new HeroesDalEntities()) { var hero = context.Heroes.ToList().FirstOrDefault(p => p.Id == id); if (hero == null) { return(NotFound()); } else { context.Heroes.Remove(hero); context.SaveChanges(); return(Ok(MapFromDAL(hero))); } } }
public IHttpActionResult PostHero([FromBody] Models.HeroApi heroApi) { using (var context = new HeroesDalEntities()) { Hero heroDB = null; if (heroApi.id == 0) //Add hero { heroDB = new Hero(); heroDB = MapToDAL(heroApi, heroDB); context.Heroes.Add(heroDB); } else { heroDB = context.Heroes.ToList().FirstOrDefault((p) => p.Id == heroApi.id); heroDB = MapToDAL(heroApi, heroDB); } context.SaveChanges(); return(Ok(heroDB)); } }