Skip to content

General refactoring #557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions Dfe.Academies.Api.Infrastructure/Repositories/Repository.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using Dfe.Academies.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;

namespace Dfe.Academies.Infrastructure.Repositories
{
#pragma warning disable CS8603 // Possible null reference return, behaviour expected
[ExcludeFromCodeCoverage]
public abstract class Repository<TEntity, TDbContext> : IRepository<TEntity>
where TEntity : class, new()
where TDbContext : DbContext
where TEntity : class, new()
where TDbContext : DbContext
{
/// <summary>
/// The <typeparamref name="TDbContext" />
Expand Down Expand Up @@ -46,15 +48,15 @@ public virtual async Task<ICollection<TEntity>> FetchAsync(
public virtual TEntity Find(params object[] keyValues) => this.DbSet().Find(keyValues);

/// <inheritdoc />
public virtual async Task<TEntity> FindAsync(params object[] keyValues)
public virtual TEntity Find(Expression<Func<TEntity, bool>> predicate)
{
return await this.DbSet().FindAsync(keyValues);
return ((IQueryable<TEntity>)this.DbSet()).FirstOrDefault<TEntity>(predicate);
}

/// <inheritdoc />
public virtual TEntity Find(Expression<Func<TEntity, bool>> predicate)
public virtual async Task<TEntity> FindAsync(params object[] keyValues)
{
return ((IQueryable<TEntity>)this.DbSet()).FirstOrDefault<TEntity>(predicate);
return await this.DbSet().FindAsync(keyValues);
}

/// <inheritdoc />
Expand All @@ -66,27 +68,27 @@ public virtual async Task<TEntity> FindAsync(
}

/// <inheritdoc />
public virtual TEntity Get(params object[] keyValues)
public virtual TEntity Get(Expression<Func<TEntity, bool>> predicate)
{
return this.Find(keyValues) ?? throw new InvalidOperationException(string.Format("Entity type {0} is null for primary key {1}", (object)typeof(TEntity), (object)keyValues));
return ((IQueryable<TEntity>)this.DbSet()).Single<TEntity>(predicate);
}

/// <inheritdoc />
public virtual async Task<TEntity> GetAsync(params object[] keyValues)
public virtual TEntity Get(params object[] keyValues)
{
return await this.FindAsync(keyValues) ?? throw new InvalidOperationException(string.Format("Entity type {0} is null for primary key {1}", (object)typeof(TEntity), (object)keyValues));
return this.Find(keyValues) ?? throw new InvalidOperationException(string.Format("Entity type {0} is null for primary key {1}", (object)typeof(TEntity), (object)keyValues));
}

/// <inheritdoc />
public virtual TEntity Get(Expression<Func<TEntity, bool>> predicate)
public virtual async Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> predicate)
{
return ((IQueryable<TEntity>)this.DbSet()).Single<TEntity>(predicate);
return await EntityFrameworkQueryableExtensions.SingleAsync<TEntity>((IQueryable<TEntity>)this.DbSet(), predicate, new CancellationToken());
}

/// <inheritdoc />
public virtual async Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> predicate)
public virtual async Task<TEntity> GetAsync(params object[] keyValues)
{
return await EntityFrameworkQueryableExtensions.SingleAsync<TEntity>((IQueryable<TEntity>)this.DbSet(), predicate, new CancellationToken());
return await this.FindAsync(keyValues) ?? throw new InvalidOperationException(string.Format("Entity type {0} is null for primary key {1}", (object)typeof(TEntity), (object)keyValues));
}

/// <inheritdoc />
Expand All @@ -100,8 +102,8 @@ public virtual TEntity Add(TEntity entity)
/// <inheritdoc />
public virtual async Task<TEntity> AddAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken))
{
EntityEntry<TEntity> entityEntry = await this.DbContext.AddAsync<TEntity>(entity, cancellationToken);
int num = await this.DbContext.SaveChangesAsync(cancellationToken);
await this.DbContext.AddAsync<TEntity>(entity, cancellationToken);
await this.DbContext.SaveChangesAsync(cancellationToken);
return entity;
}

Expand All @@ -119,7 +121,7 @@ public virtual async Task<IEnumerable<TEntity>> AddRangeAsync(
CancellationToken cancellationToken = default(CancellationToken))
{
await this.DbContext.AddRangeAsync((IEnumerable<object>)entities, cancellationToken);
int num = await this.DbContext.SaveChangesAsync(cancellationToken);
await this.DbContext.SaveChangesAsync(cancellationToken);
return (IEnumerable<TEntity>)entities;
}

Expand All @@ -137,7 +139,7 @@ public virtual async Task<TEntity> RemoveAsync(
CancellationToken cancellationToken = default(CancellationToken))
{
this.DbContext.Remove<TEntity>(entity);
int num = await this.DbContext.SaveChangesAsync(cancellationToken);
await this.DbContext.SaveChangesAsync(cancellationToken);
return entity;
}

Expand All @@ -161,7 +163,7 @@ public virtual async Task<IEnumerable<TEntity>> RemoveRangeAsync(
CancellationToken cancellationToken = default(CancellationToken))
{
this.DbSet().RemoveRange((IEnumerable<TEntity>)entities);
int num = await this.DbContext.SaveChangesAsync(cancellationToken);
await this.DbContext.SaveChangesAsync(cancellationToken);
return (IEnumerable<TEntity>)entities;
}

Expand All @@ -179,8 +181,9 @@ public virtual async Task<TEntity> UpdateAsync(
CancellationToken cancellationToken = default(CancellationToken))
{
this.DbContext.Update<TEntity>(entity);
int num = await this.DbContext.SaveChangesAsync(cancellationToken);
await this.DbContext.SaveChangesAsync(cancellationToken);
return entity;
}
}
#pragma warning restore CS8603 // Possible null reference return
}
31 changes: 0 additions & 31 deletions PersonsApi/Extensions/PredicateBuilder.cs

This file was deleted.

10 changes: 0 additions & 10 deletions PersonsApi/Extensions/StringExtensions.cs

This file was deleted.

7 changes: 1 addition & 6 deletions PersonsApi/Middleware/ExceptionHandlerMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using System;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PersonsApi.ResponseModels;
using System.Net;

public class ExceptionHandlerMiddleware
{
Expand Down
6 changes: 1 addition & 5 deletions PersonsApi/Middleware/UrlDecoderMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.WebUtilities;
using System.Web;

namespace PersonsApi.Middleware
{
Expand Down
14 changes: 1 addition & 13 deletions PersonsApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using PersonsApi;
using PersonsApi.SerilogCustomEnrichers;
using Serilog;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -16,14 +12,6 @@
builder.Host.UseSerilog((context, services, loggerConfiguration) =>
{
var enricher = services.GetRequiredService<ApiUserEnricher>();

// TODO EA
//loggerConfiguration
// .ReadFrom.Configuration(context.Configuration)
// .WriteTo.ApplicationInsights(services.GetRequiredService<TelemetryConfiguration>(), TelemetryConverter.Traces)
// .Enrich.FromLogContext()
// .Enrich.With(enricher)
// .WriteTo.Console();
});

builder.Services.AddApplicationDependencyGroup(builder.Configuration);
Expand Down
21 changes: 0 additions & 21 deletions PersonsApi/ResponseModels/ApiResponseV2.cs

This file was deleted.

10 changes: 0 additions & 10 deletions PersonsApi/ResponseModels/ApiSingleResponseV2.cs

This file was deleted.

9 changes: 0 additions & 9 deletions PersonsApi/ResponseModels/PagingResponse.cs

This file was deleted.

60 changes: 0 additions & 60 deletions PersonsApi/ResponseModels/PagingResponseFactory.cs

This file was deleted.

54 changes: 0 additions & 54 deletions PersonsApi/Services/ApiClient.cs

This file was deleted.

Loading
Loading