first commit
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
// IPlayerRepository.cs
|
||||
|
||||
using Dotnet.Samples.AspNetCore.WebApi.Models;
|
||||
|
||||
namespace Dotnet.Samples.AspNetCore.WebApi.Repositories;
|
||||
|
||||
/// <summary>
|
||||
/// Provides specialized repository operations for Player entities.
|
||||
/// </summary>
|
||||
public interface IPlayerRepository : IRepository<Player>
|
||||
{
|
||||
/// <summary>
|
||||
/// Finds a Player in the repository by their Squad Number.
|
||||
/// </summary>
|
||||
/// <param name="squadNumber">The Squad Number of the Player to retrieve.</param>
|
||||
/// <returns>
|
||||
/// A Task representing the asynchronous operation,containing the Player
|
||||
/// if found, or null if no Player with the specified Squad Number exists.
|
||||
/// </returns>
|
||||
Task<Player?> FindBySquadNumberAsync(int squadNumber);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
namespace Dotnet.Samples.AspNetCore.WebApi.Repositories;
|
||||
|
||||
/// <summary>
|
||||
/// Provides generic repository operations for entities of type <typeparamref name="T"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The entity type managed by this repository.</typeparam>
|
||||
public interface IRepository<T>
|
||||
where T : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a new entity to the repository.
|
||||
/// </summary>
|
||||
/// <param name="entity">The entity to create.</param>
|
||||
/// <returns>A Task representing the asynchronous operation.</returns>
|
||||
Task AddAsync(T entity);
|
||||
|
||||
/// <summary>
|
||||
/// Gets all entities from the repository.
|
||||
/// </summary>
|
||||
/// <returns>A Task representing the asynchronous operation,
|
||||
/// containing a list of all entities.</returns>
|
||||
Task<List<T>> GetAllAsync();
|
||||
|
||||
/// <summary>
|
||||
/// Finds an entity on the repository by its unique identifier.
|
||||
/// </summary>
|
||||
/// <param name="id">The unique identifier of the entity to retrieve.</param>
|
||||
/// <returns>
|
||||
/// A ValueTask representing the asynchronous operation, containing the entity if found,
|
||||
/// or null if no entity with the specified ID exists.
|
||||
/// </returns>
|
||||
ValueTask<T?> FindByIdAsync(Guid id);
|
||||
|
||||
/// <summary>
|
||||
/// Updates an existing entity in the repository.
|
||||
/// </summary>
|
||||
/// <param name="entity">The entity with updated values.</param>
|
||||
/// <returns>A Task representing the asynchronous operation.</returns>
|
||||
Task UpdateAsync(T entity);
|
||||
|
||||
/// <summary>
|
||||
/// Removes an entity from the repository by its unique identifier.
|
||||
/// </summary>
|
||||
/// <param name="id">The unique identifier of the entity to remove.</param>
|
||||
/// <returns>A Task representing the asynchronous operation.</returns>
|
||||
Task RemoveAsync(Guid id);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using Dotnet.Samples.AspNetCore.WebApi.Data;
|
||||
using Dotnet.Samples.AspNetCore.WebApi.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Dotnet.Samples.AspNetCore.WebApi.Repositories;
|
||||
|
||||
public sealed class PlayerRepository(PlayerDbContext dbContext)
|
||||
: Repository<Player>(dbContext),
|
||||
IPlayerRepository
|
||||
{
|
||||
public async Task<Player?> FindBySquadNumberAsync(int squadNumber) =>
|
||||
await _dbSet.FirstOrDefaultAsync(p => p.SquadNumber == squadNumber);
|
||||
|
||||
public async Task<bool> SquadNumberExistsAsync(int squadNumber)
|
||||
{
|
||||
return await dbContext.Players.AnyAsync(p => p.SquadNumber == squadNumber);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Dotnet.Samples.AspNetCore.WebApi.Repositories;
|
||||
|
||||
public class Repository<T>(DbContext dbContext) : IRepository<T>
|
||||
where T : class
|
||||
{
|
||||
protected readonly DbSet<T> _dbSet = dbContext.Set<T>();
|
||||
|
||||
public async Task AddAsync(T entity)
|
||||
{
|
||||
await _dbSet.AddAsync(entity);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<List<T>> GetAllAsync() => await _dbSet.AsNoTracking().ToListAsync();
|
||||
|
||||
public async ValueTask<T?> FindByIdAsync(Guid id) => await _dbSet.FindAsync(id);
|
||||
|
||||
public async Task UpdateAsync(T entity)
|
||||
{
|
||||
_dbSet.Update(entity);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task RemoveAsync(Guid id)
|
||||
{
|
||||
var entity = await _dbSet.FindAsync(id);
|
||||
if (entity != null)
|
||||
{
|
||||
_dbSet.Remove(entity);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user