Beispiel #1
0
        public async Task <IActionResult> PutPokemon([FromRoute] string id, [FromBody] Pokemon pokemon)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != pokemon.Code)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
        public async Task <IActionResult> AddFavorite([FromQuery] int id = 0)
        {
            UserFavorites newFave = new UserFavorites()
            {
                UserId = User.FindFirstValue(ClaimTypes.NameIdentifier), PokemonId = id
            };
            await _context.UserFavorites.AddAsync(newFave);

            await _context.SaveChangesAsync();

            return(Redirect("/Pokemon/PokemonResults?id=" + id));
        }
Beispiel #3
0
        public async Task <IActionResult> Post(Pokemon poke)
        {
            if (ModelState.IsValid)
            {
                _db.Pokemon.Add(poke);
                await _db.SaveChangesAsync();

                return(await Task.FromResult(Ok(poke)));
            }
            return(await Task.FromResult(NotFound(poke)));
        }
        public IActionResult FireAndForget2([FromServices] PokemonDbContext context)
        {
            // This uses Task.Run instead of ThreadPool.QueueUserWorkItem. It's mostly equivalent to the FireAndForget1 but since we're using
            // async Task instead of async void, unhandled exceptions won't crash the process. They will however trigger the TaskScheduler.UnobservedTaskException
            // event when exceptions go unhandled.
            Task.Run(async() =>
            {
                await Task.Delay(1000);

                // This closure is capturing the context from the Controller action parameter. This is bad because this work item could run
                // outside of the request scope and the PokemonDbContext is scoped to the request. As a result, this will throw an unhandled ObjectDisposedException.
                context.Pokemon.Add(new Pokemon());
                await context.SaveChangesAsync();
            });

            return(Accepted());
        }
        public IActionResult FireAndForget1([FromServices] PokemonDbContext context)
        {
            // This is an implicit async void method. ThreadPool.QueueUserWorkItem takes an Action, but the compiler allows
            // async void delegates to be used in its place. This is dangerous because unhandled exceptions will bring down the entire server process.
            ThreadPool.QueueUserWorkItem(async state =>
            {
                await Task.Delay(1000);

                // This closure is capturing the context from the Controller action parameter. This is bad because this work item could run
                // outside of the request scope and the PokemonDbContext is scoped to the request. As a result, this will crash the process with
                // and ObjectDisposedException
                context.Pokemon.Add(new Pokemon());
                await context.SaveChangesAsync();
            });

            return(Accepted());
        }
 private async Task SaveChangesToDatabase()
 {
     await dbContext.SaveChangesAsync();
 }
Beispiel #7
0
 public async Task SaveChangesAsync()
 {
     await _pokemonDbContext.SaveChangesAsync();
 }