The IRepositorioJugador interface and RepositorioJugador implementation provide data access operations for managing players. Each player is associated with a team (Equipo) and a position (Posicion).
using Torneo.App.Dominio;namespace Torneo.App.Persistencia{ public interface IRepositorioJugador { public Jugador AddJugador(Jugador jugador, int idEquipo, int idPosicion); public IEnumerable<Jugador> GetAllJugadores(); public Jugador GetJugador(int idJugador); public Jugador UpdateJugador(Jugador jugador, int idEquipo, int idPosicion); public Jugador DeleteJugador(int idJugador); public bool validateDuplicates(Jugador jugador, int idEquipo, int idPosicion); }}
using Microsoft.EntityFrameworkCore;using Torneo.App.Dominio;namespace Torneo.App.Persistencia{ public class RepositorioJugador : IRepositorioJugador { private DataContext _dataContext = new DataContext(); // Implementation methods... }}
Throws exception with message “Jugador not found” if the ID doesn’t exist
Implementation (Lines 54-65)
public Jugador DeleteJugador(int idJugador){ var jugadorEncontrado = _dataContext.Jugadores.Find(idJugador); if (jugadorEncontrado != null) { _dataContext.Jugadores.Remove(jugadorEncontrado); _dataContext.SaveChanges(); } else { Console.WriteLine("No se encontró el Jugador"); } return jugadorEncontrado ?? throw new Exception("Jugador not found");}
Excludes current player when updating (checks jugador.Id != j.Id)
Usage Example:
var nuevoJugador = new Jugador{ Nombre = "Carlos Rodríguez", Numero = 10};if (repositorioJugador.validateDuplicates(nuevoJugador, idEquipo: 5, idPosicion: 3)){ Console.WriteLine("Error: Ya existe un jugador con el mismo nombre, número, equipo y posición");}else{ repositorioJugador.AddJugador(nuevoJugador, idEquipo: 5, idPosicion: 3); Console.WriteLine("Jugador creado exitosamente");}
The duplicate validation checks ALL four fields (name, number, team, position). A player can have the same name or number if they play in a different team or position.
// 1. Create player entityvar jugador = new Jugador{ Nombre = "Carlos Rodríguez", Numero = 10};// 2. Validate duplicatesint equipoId = 5;int posicionId = 3; // Delanteroif (!repositorioJugador.validateDuplicates(jugador, equipoId, posicionId)){ // 3. Add player var resultado = repositorioJugador.AddJugador(jugador, equipoId, posicionId); Console.WriteLine($"Jugador {resultado.Nombre} #{resultado.Numero} agregado al equipo");}else{ Console.WriteLine("Ya existe un jugador con estos datos");}
Transferring a Player to Another Team
// 1. Get playervar jugador = repositorioJugador.GetJugador(15);if (jugador != null){ int nuevoEquipoId = 8; int posicionActual = jugador.Posicion.Id; // 2. Validate transfer if (!repositorioJugador.validateDuplicates(jugador, nuevoEquipoId, posicionActual)) { // 3. Update team var jugadorTransferido = repositorioJugador.UpdateJugador( jugador, idEquipo: nuevoEquipoId, idPosicion: posicionActual ); Console.WriteLine($"{jugador.Nombre} transferido a {jugadorTransferido.Equipo?.Nombre}"); }}
Listing Players by Position
var todosJugadores = repositorioJugador.GetAllJugadores();// Group by positionvar jugadoresPorPosicion = todosJugadores .GroupBy(j => j.Posicion?.Nombre) .OrderBy(g => g.Key);foreach (var grupo in jugadoresPorPosicion){ Console.WriteLine($"\n{grupo.Key}:"); foreach (var jugador in grupo.OrderBy(j => j.Numero)) { Console.WriteLine($" #{jugador.Numero} {jugador.Nombre} - {jugador.Equipo?.Nombre}"); }}
Finding Players by Team
var todosJugadores = repositorioJugador.GetAllJugadores();// Filter by teamint equipoId = 5;var jugadoresEquipo = todosJugadores .Where(j => j.Equipo?.Id == equipoId) .OrderBy(j => j.Numero) .ToList();Console.WriteLine($"Jugadores del equipo (Total: {jugadoresEquipo.Count}):");foreach (var jugador in jugadoresEquipo){ Console.WriteLine($"#{jugador.Numero} {jugador.Nombre} - {jugador.Posicion?.Nombre}");}