exciting-aftermath/Portfolio.Infrastructure/Repositories/PokemonRepository.cs

51 lines
1.4 KiB
C#

using Microsoft.EntityFrameworkCore;
using Portfolio.Domain.Features.Pokemon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Portfolio.Infrastructure.Repositories
{
public class PokemonRepository : IPokemonRepository
{
private readonly ApplicationDbContext _context;
public PokemonRepository(ApplicationDbContext context)
{
_context = context;
}
public async Task<List<Pokemon>> GetAllPokemonsAsync()
{
return await _context.Pokemons.ToListAsync();
}
public async Task<Pokemon> GetPokemonByIdAsync(int id)
{
return await _context.Pokemons.FirstOrDefaultAsync(p => p.Id == id);
}
public async Task AddPokemonAsync(Pokemon pokemon)
{
_context.Pokemons.Add(pokemon);
await _context.SaveChangesAsync();
}
public async Task DeletePokemonAsync(int pokemonId)
{
var pokemon = await _context.Pokemons.FindAsync(pokemonId);
if (pokemon != null)
{
_context.Pokemons.Remove(pokemon);
await _context.SaveChangesAsync();
}
}
public async Task UpdatePokemonAsync(Pokemon pokemon)
{
_context.Pokemons.Update(pokemon);
await _context.SaveChangesAsync();
}
}
}