// Mapping Data from HeroesWebAPI to HeroesDal Hero class public Hero MapToDAL(Models.HeroApi heroWebApi, Hero hero) { if (hero == null) { hero = new Hero(); } hero.Id = heroWebApi.id; hero.Name = heroWebApi.name; hero.Power = heroWebApi.power; return(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)); } }