private async Task <Data.Entities.Pokemon> Insert(Data.Entities.Pokemon pokemon) { await _context.Pokemon.AddAsync(pokemon); await _context.SaveChangesAsync(); return(pokemon); }
public async Task <Data.Entities.Pokemon> UpSert(Data.Entities.Pokemon pokemon) { if (pokemon.Id == 0) { return(await Insert(pokemon)); } return(await Update(pokemon)); }
private async Task <Data.Entities.Pokemon> Update(Data.Entities.Pokemon pokemon) { var existingPokemon = await _context.Pokemon .Include(x => x.BaseStat) .Include(x => x.Name) .SingleOrDefaultAsync(x => x.Id == pokemon.Id); if (existingPokemon == null) { _logger.LogError($"Pokemon does not exist with Id :: {pokemon.Id}"); throw new Exception($"Pokemon does not exist with Id :: {pokemon.Id}"); } existingPokemon.Update(pokemon); await _context.SaveChangesAsync(); return(existingPokemon); }
public async Task Verify_Update_Correctly(Data.Entities.Pokemon newEntity) { var result = await Services.PokemonService.UpSert(newEntity); // detach the entity so it does not get updated during second service call PokeContext.Entry(result).State = EntityState.Detached; var dbEntity = await PokeContext.Pokemon .AsNoTracking() .Include(x => x.Name) .Where(x => x.Id == result.Id) .SingleOrDefaultAsync(); Assert.NotNull(dbEntity); dbEntity.Name.English = "Charmander"; var updatedResult = await Services.PokemonService.UpSert(dbEntity); Assert.AreEqual(result.Id, updatedResult.Id); Assert.AreNotEqual(result.Name.English, updatedResult.Name.English); }