< Summary

Information
Class: Infrastructure.Repository.NorthwindRepository
Assembly: Infrastructure
File(s): /home/runner/work/Northwind-Api/Northwind-Api/src/Infrastructure/Repository/NorthwindRepository.cs
Line coverage
60%
Covered lines: 30
Uncovered lines: 20
Coverable lines: 50
Total lines: 72
Line coverage: 60%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
FetchEmployees()100%11100%
FetchCustomers()100%11100%
FetchEmployeeById()100%11100%
FetchCustomerById()100%11100%
GetById()100%210%
Add()100%210%
Update()100%210%
Delete()100%210%
SaveChangesAsync()100%210%

File(s)

/home/runner/work/Northwind-Api/Northwind-Api/src/Infrastructure/Repository/NorthwindRepository.cs

#LineLine coverage
 1using Infrastructure.Context;
 2using Infrastructure.Entity;
 3using Microsoft.EntityFrameworkCore;
 4using Microsoft.Extensions.Logging;
 5
 6namespace Infrastructure.Repository;
 7public class NorthwindRepository {
 8  private readonly ILogger<NorthwindRepository> _logger;
 9  private readonly IDbContextFactory<NorthwindContext> _factory;
 10
 811  public NorthwindRepository(ILogger<NorthwindRepository> logger, IDbContextFactory<NorthwindContext> factory) {
 412    _logger = logger;
 413    _factory = factory;
 414  }
 15
 116  public async Task<employee[]> FetchEmployees(CancellationToken ct) {
 117    var ctx = await _factory.CreateDbContextAsync(ct);
 118    var result = await ctx.employees.ToArrayAsync(ct);
 119    _logger.LogTrace("Found {Count} {Type}", result.Length, typeof(employee)); //TODO adjust log from non reflection
 120    return result;
 121  }
 22
 123  public async Task<customer[]> FetchCustomers(CancellationToken ct) {
 124    var ctx = await _factory.CreateDbContextAsync(ct);
 125    var result = await ctx.customers.ToArrayAsync(ct);
 126    _logger.LogTrace("Found {Count} {Type}", result.Length, typeof(customer)); //TODO adjust log from non reflection
 127    return result;
 128  }
 29
 130  public async Task<employee?> FetchEmployeeById(short id, CancellationToken ct) {
 131    var ctx = await _factory.CreateDbContextAsync(ct);
 132    var result = await ctx.employees
 133      .AsNoTracking()
 134      .SingleOrDefaultAsync(e => e.employee_id == id, ct);
 35
 136    return result;
 137  }
 38
 139  public async Task<customer?> FetchCustomerById(string id, CancellationToken ct) {
 140    var ctx = await _factory.CreateDbContextAsync(ct);
 141    var result = await ctx.customers
 142      .AsNoTracking()
 143      .SingleOrDefaultAsync(c => c.customer_id == id, ct);
 44
 145    return result;
 146  }
 47
 048  public async Task<T?> GetById<T>(int id, CancellationToken ct) where T : class {
 049    var ctx = await _factory.CreateDbContextAsync(ct);
 050    return await ctx.Set<T>().FindAsync([id], cancellationToken: ct);
 051  }
 52
 053  public async Task Add<T>(T entity, CancellationToken ct) where T : class {
 054    var ctx = await _factory.CreateDbContextAsync(ct);
 055    await ctx.Set<T>().AddAsync(entity, ct);
 056  }
 57
 058  public async Task Update<T>(T entity, CancellationToken ct) where T : class {
 059    var ctx = await _factory.CreateDbContextAsync(ct);
 060    ctx.Set<T>().Update(entity);
 061  }
 62
 063  public async Task Delete<T>(T entity, CancellationToken ct) where T : class {
 064    var ctx = await _factory.CreateDbContextAsync(ct);
 065    ctx.Set<T>().Remove(entity);
 066  }
 67
 068  public async Task SaveChangesAsync(CancellationToken ct) {
 069    var ctx = await _factory.CreateDbContextAsync(ct);
 070    await ctx.SaveChangesAsync(ct);
 071  }
 72}