Exemplo n.º 1
0

        
Exemplo n.º 2
0
        // POST: api/Heroes
        public async Task <IHttpActionResult> Post([FromBody] HeroCreate hero)
        {
            if (hero == null || string.IsNullOrEmpty(hero.HeroName) || string.IsNullOrWhiteSpace(hero.HeroName))
            {
                return(BadRequest(string.Format(GeneralMessages.InvalidData, ModelNames.Hero)));
            }

            using (HeroDbContext context = new HeroDbContext())
            {
                context.Heroes.Add(new Hero(hero));
                await context.SaveChangesAsync();

                return(Ok(new CMessage(string.Format(GeneralMessages.CreateSuccess, ModelNames.Hero))));
            }
        }
Exemplo n.º 3
0
        // PUT: api/Heroes/5
        public async Task <IHttpActionResult> Put(int id, [FromBody] HeroCreate hero)
        {
            if (hero == null || string.IsNullOrEmpty(hero.HeroName) || string.IsNullOrWhiteSpace(hero.HeroName))
            {
                return(BadRequest(string.Format(GeneralMessages.InvalidData, ModelNames.Hero)));
            }

            using (HeroDbContext context = new HeroDbContext())
            {
                Hero findHero = await context.Heroes.FirstOrDefaultAsync(h => h.HeroId == id);

                if (findHero == null)
                {
                    return(BadRequest(string.Format(GeneralMessages.NotFound, ModelNames.Hero, _heroMessages.GetString("HeroId"), id)));
                }

                findHero.HeroName = hero.HeroName;
                await context.SaveChangesAsync();

                return(Ok(new CMessage(string.Format(GeneralMessages.UpdateSuccess, ModelNames.Hero, _heroMessages.GetString("HeroId"), id))));
            }
        }