Пример #1
0
        public async Task <ActionResult <MonsterPostDTo> > PostMonster(MonsterPostDTo monster)
        {
            Monster newMonster = new Monster {
                Id         = monster.Id,
                AttackType = monster.AttackType,
                Rarity     = monster.Rarity,
                Name       = monster.Name,
                Type       = monster.Type
            };

            _context.Monsters.Add(newMonster);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (MonsterExists(monster.Id))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetMonster", new { id = monster.Id }, monster));
        }
Пример #2
0
        public async Task <IActionResult> PutMonster(int id, MonsterPostDTo monster)
        {
            if (id != monster.Id)
            {
                return(BadRequest());
            }

            Monster newMonster = new Monster {
                Id         = monster.Id,
                AttackType = monster.AttackType,
                Rarity     = monster.Rarity,
                Name       = monster.Name,
                Type       = monster.Type
            };

            _context.Entry(newMonster).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MonsterExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }