193 lines
6.5 KiB
C#
193 lines
6.5 KiB
C#
using KoogleApp.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace KoogleApp.Data.Repository
|
|
{
|
|
//public interface IPlayerExpenseRepository
|
|
//{
|
|
// Task<List<PlayerExpense>> GetByDataItemIdAsync(int playerId);
|
|
// Task<PlayerExpense?> GetByIdAsync(int id);
|
|
// Task<PlayerExpense> CreateAsync(PlayerExpense position);
|
|
// Task<PlayerExpense> UpdateAsync(PlayerExpense position);
|
|
// Task<bool> DeleteAsync(int id);
|
|
// Task<List<PlayerExpense>> CreateRangeAsync(List<PlayerExpense> positions);
|
|
//}
|
|
|
|
//public class InMemoryPlayerExpenseRepository : IPlayerExpenseRepository
|
|
//{
|
|
// private readonly List<PlayerExpense> _positions;
|
|
// private readonly SemaphoreSlim _lock = new(1, 1);
|
|
|
|
// public InMemoryPlayerExpenseRepository(IPlayerRepository playerRepo)
|
|
// {
|
|
// // In echter Implementierung würde man eine gemeinsame Storage-Klasse nutzen
|
|
// _positions = new List<PlayerExpense>();
|
|
// }
|
|
|
|
// public async Task<List<PlayerExpense>> GetByDataItemIdAsync(int playerId)
|
|
// {
|
|
// await _lock.WaitAsync();
|
|
// try
|
|
// {
|
|
// return _positions.Where(p => p.PlayerId == playerId)
|
|
// .OrderBy(p => p.CreatedAt)
|
|
// .ToList();
|
|
// }
|
|
// finally
|
|
// {
|
|
// _lock.Release();
|
|
// }
|
|
// }
|
|
|
|
// public async Task<PlayerExpense?> GetByIdAsync(int id)
|
|
// {
|
|
// await _lock.WaitAsync();
|
|
// try
|
|
// {
|
|
// return _positions.FirstOrDefault(p => p.Id == id);
|
|
// }
|
|
// finally
|
|
// {
|
|
// _lock.Release();
|
|
// }
|
|
// }
|
|
|
|
// public async Task<PlayerExpense> CreateAsync(PlayerExpense position)
|
|
// {
|
|
// await _lock.WaitAsync();
|
|
// try
|
|
// {
|
|
// position.Id = _positions.Any() ? _positions.Max(p => p.Id) + 1 : 1;
|
|
// _positions.Add(position);
|
|
// return position;
|
|
// }
|
|
// finally
|
|
// {
|
|
// _lock.Release();
|
|
// }
|
|
// }
|
|
|
|
// public async Task<PlayerExpense> UpdateAsync(PlayerExpense position)
|
|
// {
|
|
// await _lock.WaitAsync();
|
|
// try
|
|
// {
|
|
// var existing = _positions.FirstOrDefault(p => p.Id == position.Id);
|
|
// if (existing == null)
|
|
// throw new KeyNotFoundException($"Position with ID {position.Id} not found");
|
|
|
|
// existing.Name = position.Name;
|
|
// //existing.Quantity = position.Quantity;
|
|
// //existing.UnitPrice = position.UnitPrice;
|
|
// //existing.Unit = position.Unit;
|
|
// //existing.SortOrder = position.SortOrder;
|
|
|
|
// return existing;
|
|
// }
|
|
// finally
|
|
// {
|
|
// _lock.Release();
|
|
// }
|
|
// }
|
|
|
|
// public async Task<bool> DeleteAsync(int id)
|
|
// {
|
|
// await _lock.WaitAsync();
|
|
// try
|
|
// {
|
|
// var position = _positions.FirstOrDefault(p => p.Id == id);
|
|
// if (position == null) return false;
|
|
|
|
// _positions.Remove(position);
|
|
// return true;
|
|
// }
|
|
// finally
|
|
// {
|
|
// _lock.Release();
|
|
// }
|
|
// }
|
|
|
|
// public async Task<List<PlayerExpense>> CreateRangeAsync(List<PlayerExpense> positions)
|
|
// {
|
|
// await _lock.WaitAsync();
|
|
// try
|
|
// {
|
|
// foreach (var pos in positions)
|
|
// {
|
|
// pos.Id = _positions.Any() ? _positions.Max(p => p.Id) + 1 : 1;
|
|
// _positions.Add(pos);
|
|
// }
|
|
// return positions;
|
|
// }
|
|
// finally
|
|
// {
|
|
// _lock.Release();
|
|
// }
|
|
// }
|
|
//}
|
|
|
|
//public class PlayerExpenseRepository : IPlayerExpenseRepository
|
|
//{
|
|
// private readonly IDbContextFactory<AppDbContext> _contextFactory;
|
|
// private readonly ILogger<IPlayerExpenseRepository> _logger;
|
|
|
|
// public PlayerExpenseRepository(
|
|
// IDbContextFactory<AppDbContext> contextFactory,
|
|
// ILogger<IPlayerExpenseRepository> logger)
|
|
// {
|
|
// _contextFactory = contextFactory;
|
|
// _logger = logger;
|
|
// }
|
|
|
|
// public async Task<List<PlayerExpense>> GetByDataItemIdAsync(int playerId)
|
|
// {
|
|
// await using var context = await _contextFactory.CreateDbContextAsync();
|
|
// return await context.PlayerExpenses
|
|
// .Where(p => p.PlayerId == playerId)
|
|
// .OrderBy(p => p.CreatedAt)
|
|
// .ToListAsync();
|
|
// }
|
|
|
|
// public async Task<PlayerExpense?> GetByIdAsync(int id)
|
|
// {
|
|
// await using var context = await _contextFactory.CreateDbContextAsync();
|
|
// return await context.PlayerExpenses.FindAsync(id);
|
|
// }
|
|
|
|
// public async Task<PlayerExpense> CreateAsync(PlayerExpense position)
|
|
// {
|
|
// await using var context = await _contextFactory.CreateDbContextAsync();
|
|
// context.PlayerExpenses.Add(position);
|
|
// await context.SaveChangesAsync();
|
|
// return position;
|
|
// }
|
|
|
|
// public async Task<PlayerExpense> UpdateAsync(PlayerExpense position)
|
|
// {
|
|
// await using var context = await _contextFactory.CreateDbContextAsync();
|
|
// context.PlayerExpenses.Update(position);
|
|
// await context.SaveChangesAsync();
|
|
// return position;
|
|
// }
|
|
|
|
// public async Task<bool> DeleteAsync(int id)
|
|
// {
|
|
// await using var context = await _contextFactory.CreateDbContextAsync();
|
|
// var position = await context.PlayerExpenses.FindAsync(id);
|
|
// if (position == null) return false;
|
|
|
|
// context.PlayerExpenses.Remove(position);
|
|
// await context.SaveChangesAsync();
|
|
// return true;
|
|
// }
|
|
|
|
// public async Task<List<PlayerExpense>> CreateRangeAsync(List<PlayerExpense> positions)
|
|
// {
|
|
// await using var context = await _contextFactory.CreateDbContextAsync();
|
|
// context.PlayerExpenses.AddRange(positions);
|
|
// await context.SaveChangesAsync();
|
|
// return positions;
|
|
// }
|
|
//}
|
|
}
|