| | 1 | | using Infrastructure.Context; |
| | 2 | | using Infrastructure.Entity; |
| | 3 | | using Microsoft.EntityFrameworkCore; |
| | 4 | | using Microsoft.Extensions.Logging; |
| | 5 | |
|
| | 6 | | namespace Infrastructure.Repository; |
| | 7 | | public class NorthwindRepository { |
| | 8 | | private readonly ILogger<NorthwindRepository> _logger; |
| | 9 | | private readonly IDbContextFactory<NorthwindContext> _factory; |
| | 10 | |
|
| 8 | 11 | | public NorthwindRepository(ILogger<NorthwindRepository> logger, IDbContextFactory<NorthwindContext> factory) { |
| 4 | 12 | | _logger = logger; |
| 4 | 13 | | _factory = factory; |
| 4 | 14 | | } |
| | 15 | |
|
| 1 | 16 | | public async Task<employee[]> FetchEmployees(CancellationToken ct) { |
| 1 | 17 | | var ctx = await _factory.CreateDbContextAsync(ct); |
| 1 | 18 | | var result = await ctx.employees.ToArrayAsync(ct); |
| 1 | 19 | | _logger.LogTrace("Found {Count} {Type}", result.Length, typeof(employee)); //TODO adjust log from non reflection |
| 1 | 20 | | return result; |
| 1 | 21 | | } |
| | 22 | |
|
| 1 | 23 | | public async Task<customer[]> FetchCustomers(CancellationToken ct) { |
| 1 | 24 | | var ctx = await _factory.CreateDbContextAsync(ct); |
| 1 | 25 | | var result = await ctx.customers.ToArrayAsync(ct); |
| 1 | 26 | | _logger.LogTrace("Found {Count} {Type}", result.Length, typeof(customer)); //TODO adjust log from non reflection |
| 1 | 27 | | return result; |
| 1 | 28 | | } |
| | 29 | |
|
| 1 | 30 | | public async Task<employee?> FetchEmployeeById(short id, CancellationToken ct) { |
| 1 | 31 | | var ctx = await _factory.CreateDbContextAsync(ct); |
| 1 | 32 | | var result = await ctx.employees |
| 1 | 33 | | .AsNoTracking() |
| 1 | 34 | | .SingleOrDefaultAsync(e => e.employee_id == id, ct); |
| | 35 | |
|
| 1 | 36 | | return result; |
| 1 | 37 | | } |
| | 38 | |
|
| 1 | 39 | | public async Task<customer?> FetchCustomerById(string id, CancellationToken ct) { |
| 1 | 40 | | var ctx = await _factory.CreateDbContextAsync(ct); |
| 1 | 41 | | var result = await ctx.customers |
| 1 | 42 | | .AsNoTracking() |
| 1 | 43 | | .SingleOrDefaultAsync(c => c.customer_id == id, ct); |
| | 44 | |
|
| 1 | 45 | | return result; |
| 1 | 46 | | } |
| | 47 | |
|
| 0 | 48 | | public async Task<T?> GetById<T>(int id, CancellationToken ct) where T : class { |
| 0 | 49 | | var ctx = await _factory.CreateDbContextAsync(ct); |
| 0 | 50 | | return await ctx.Set<T>().FindAsync([id], cancellationToken: ct); |
| 0 | 51 | | } |
| | 52 | |
|
| 0 | 53 | | public async Task Add<T>(T entity, CancellationToken ct) where T : class { |
| 0 | 54 | | var ctx = await _factory.CreateDbContextAsync(ct); |
| 0 | 55 | | await ctx.Set<T>().AddAsync(entity, ct); |
| 0 | 56 | | } |
| | 57 | |
|
| 0 | 58 | | public async Task Update<T>(T entity, CancellationToken ct) where T : class { |
| 0 | 59 | | var ctx = await _factory.CreateDbContextAsync(ct); |
| 0 | 60 | | ctx.Set<T>().Update(entity); |
| 0 | 61 | | } |
| | 62 | |
|
| 0 | 63 | | public async Task Delete<T>(T entity, CancellationToken ct) where T : class { |
| 0 | 64 | | var ctx = await _factory.CreateDbContextAsync(ct); |
| 0 | 65 | | ctx.Set<T>().Remove(entity); |
| 0 | 66 | | } |
| | 67 | |
|
| 0 | 68 | | public async Task SaveChangesAsync(CancellationToken ct) { |
| 0 | 69 | | var ctx = await _factory.CreateDbContextAsync(ct); |
| 0 | 70 | | await ctx.SaveChangesAsync(ct); |
| 0 | 71 | | } |
| | 72 | | } |