117 lines
3.9 KiB
C#
117 lines
3.9 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> GetPokemonByPokemonIdAsync(int id)
|
|
{
|
|
return await _context.Pokemons.FirstOrDefaultAsync(p => p.PokemonId == id);
|
|
}
|
|
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();
|
|
}
|
|
|
|
public async Task<int?> GetPreviousPokemonIdAsync(int currentPokemonId)
|
|
{
|
|
// Get the previous Pokémon's PokemonId
|
|
var prevPokemonId = await _context.Pokemons
|
|
.Where(p => p.PokemonId < currentPokemonId)
|
|
.OrderByDescending(p => p.PokemonId)
|
|
.Select(p => (int?)p.PokemonId)
|
|
.FirstOrDefaultAsync();
|
|
|
|
// If no previous PokemonId is found, wrap around to the last one
|
|
if (prevPokemonId == null)
|
|
{
|
|
prevPokemonId = await _context.Pokemons
|
|
.OrderByDescending(p => p.PokemonId) // Get the last PokemonId
|
|
.Select(p => (int?)p.PokemonId)
|
|
.FirstOrDefaultAsync();
|
|
}
|
|
|
|
return prevPokemonId;
|
|
}
|
|
|
|
public async Task<int?> GetNextPokemonIdAsync(int currentPokemonId)
|
|
{
|
|
// Get the next Pokémon's PokemonId
|
|
var nextPokemonId = await _context.Pokemons
|
|
.Where(p => p.PokemonId > currentPokemonId)
|
|
.OrderBy(p => p.PokemonId)
|
|
.Select(p => (int?)p.PokemonId)
|
|
.FirstOrDefaultAsync();
|
|
|
|
// If no next PokemonId is found, wrap around to the first one
|
|
if (nextPokemonId == null)
|
|
{
|
|
nextPokemonId = await _context.Pokemons
|
|
.OrderBy(p => p.PokemonId) // Get the first PokemonId
|
|
.Select(p => (int?)p.PokemonId)
|
|
.FirstOrDefaultAsync();
|
|
}
|
|
|
|
return nextPokemonId;
|
|
}
|
|
|
|
public async Task<List<int>> GetAllPokemonIdsAsync()
|
|
{
|
|
return await _context.Pokemons
|
|
.OrderBy(p => p.PokemonId) // Ensure it's ordered by PokemonId
|
|
.Select(p => p.PokemonId)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<int?> GetVariationPokemonIdAsync(int pokemonId)
|
|
{
|
|
// Find a variation for the given PokemonId (where IsVariation is true)
|
|
var variation = await _context.Pokemons
|
|
.Where(p => p.PokemonId == pokemonId && p.IsVariation)
|
|
.FirstOrDefaultAsync();
|
|
|
|
// Return the Id of the variation, or null if no variation is found
|
|
return variation?.Id;
|
|
}
|
|
|
|
|
|
}
|
|
}
|