parent
1fec305555
commit
fb6e2f2aaa
@ -0,0 +1,25 @@
|
||||
using System.Linq.Expressions;
|
||||
using Shared.Entities;
|
||||
|
||||
namespace Shared.Infrastructure;
|
||||
|
||||
public interface IRepository<T> where T : EntityBase
|
||||
{
|
||||
IEnumerable<T> GetAll(params Expression<Func<T, object>>[] includes);
|
||||
|
||||
Task<IEnumerable<T>> GetAllAsync(Expression<Func<T, bool>>? expression = null, CancellationToken cancellationToken = default, params Expression<Func<T, object>>[] includes);
|
||||
|
||||
Task<T?> GetByIdAsync(object id, params Expression<Func<T, object>>[] includes);
|
||||
|
||||
Task InsertAsync(T obj);
|
||||
|
||||
void Update(T obj);
|
||||
|
||||
void Delete(object id);
|
||||
|
||||
//Task<PaginatedResult<T>> GetPaginatedListAsync(int pageNumber, int pageSize, string[]? orderBy = null, Expression<Func<T, bool>>? expression = null, CancellationToken cancellationToken = default, params Expression<Func<T, object>>[] includes);
|
||||
|
||||
Task<int> CountAsync(Expression<Func<T, bool>>? expression = null, CancellationToken cancellationToken = default);
|
||||
|
||||
Task<bool> ExistsAsync(Expression<Func<T, bool>> expression, CancellationToken cancellationToken = default);
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using TrainingSvc.Data;
|
||||
using TrainingSvc.Entities;
|
||||
|
||||
namespace TrainingSvc.Repositories;
|
||||
|
||||
public class ExerciceInstanceRepository : GenericRepository<ExerciceInstance>, IExerciceInstanceRepository
|
||||
{
|
||||
public ExerciceInstanceRepository(TrainingDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using CatalogService.Entities;
|
||||
using TrainingSvc.Data;
|
||||
|
||||
namespace TrainingSvc.Repositories;
|
||||
|
||||
public class ExerciceTemplateRepository : GenericRepository<ExerciceTemplate>, IExerciceTemplateRepository
|
||||
{
|
||||
public ExerciceTemplateRepository(TrainingDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,114 @@
|
||||
using System.Linq.Dynamic.Core;
|
||||
using System.Linq.Expressions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Shared.Entities;
|
||||
using Shared.Infrastructure;
|
||||
using TrainingSvc.Data;
|
||||
|
||||
namespace TrainingSvc.Repositories;
|
||||
|
||||
|
||||
public class GenericRepository<T> : IRepository<T> where T : EntityBase
|
||||
{
|
||||
protected readonly TrainingDbContext context;
|
||||
|
||||
public GenericRepository(TrainingDbContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public IEnumerable<T> GetAll(params Expression<Func<T, object>>[] includes)
|
||||
{
|
||||
IQueryable<T> query = this.context.Set<T>();
|
||||
|
||||
query = includes.Aggregate(query, (current, include) => current.Include(include));
|
||||
|
||||
return query.ToList<T>();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<T>> GetAllAsync(Expression<Func<T, bool>>? expression = null, CancellationToken cancellationToken = default, params Expression<Func<T, object>>[] includes)
|
||||
{
|
||||
IQueryable<T> query = this.context.Set<T>();
|
||||
query = includes.Aggregate(query, (current, include) => current.Include(include));
|
||||
if (expression != null) query = query.Where(expression);
|
||||
|
||||
return await query.ToDynamicListAsync<T>(cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
public virtual async Task<T?> GetByIdAsync(object id, params Expression<Func<T, object>>[] includes)
|
||||
{
|
||||
IQueryable<T> query = this.context.Set<T>();
|
||||
|
||||
query = query.Where(entity => entity.Id.Equals(id));
|
||||
|
||||
query = includes.Aggregate(query, (current, include) => current.Include(include));
|
||||
|
||||
return await query.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task InsertAsync(T obj)
|
||||
{
|
||||
_ = await this.context.Set<T>()
|
||||
.AddAsync(obj);
|
||||
}
|
||||
|
||||
public void Update(T obj)
|
||||
{
|
||||
_ = this.context.Set<T>().Attach(obj);
|
||||
this.context.Entry(obj).State = EntityState.Modified;
|
||||
}
|
||||
|
||||
public void Delete(object id)
|
||||
{
|
||||
var existing = this.context
|
||||
.Set<T>()
|
||||
.Find(id);
|
||||
|
||||
_ = this.context.Set<T>().Remove(existing!);
|
||||
}
|
||||
|
||||
/*public async Task<PaginatedResult<T>> GetPaginatedListAsync(
|
||||
int pageNumber,
|
||||
int pageSize,
|
||||
string[]? orderBy = null,
|
||||
Expression<Func<T, bool>>? expression = null,
|
||||
CancellationToken cancellationToken = default,
|
||||
params Expression<Func<T, object>>[] includes)
|
||||
{
|
||||
IQueryable<T> query = this.context.Set<T>();
|
||||
|
||||
query = includes.Aggregate(query, (current, include) => current.Include(include));
|
||||
|
||||
if (expression != null) query = query.Where(expression);
|
||||
|
||||
var ordering = orderBy?.Any() == true ? string.Join(",", orderBy) : null;
|
||||
|
||||
query = !string.IsNullOrWhiteSpace(ordering) ? query.OrderBy(ordering) : query.OrderBy(a => a.Id);
|
||||
|
||||
var count = await query
|
||||
.AsNoTracking()
|
||||
.CountAsync(cancellationToken: cancellationToken);
|
||||
|
||||
var items = await query
|
||||
.Skip(pageNumber * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToDynamicListAsync<T>(cancellationToken: cancellationToken);
|
||||
|
||||
return new PaginatedResult<T>(items, count, pageNumber, pageSize);
|
||||
}*/
|
||||
|
||||
public async Task<int> CountAsync(Expression<Func<T, bool>>? expression = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
IQueryable<T> query = this.context.Set<T>();
|
||||
if (expression != null) query = query.Where(expression);
|
||||
|
||||
return await query
|
||||
.AsNoTracking()
|
||||
.CountAsync(cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<bool> ExistsAsync(Expression<Func<T, bool>> expression, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await this.context.Set<T>().AnyAsync(expression, cancellationToken: cancellationToken);
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
using CatalogService.Entities;
|
||||
using Shared.Infrastructure;
|
||||
using TrainingSvc.Entities;
|
||||
|
||||
namespace TrainingSvc.Repositories;
|
||||
|
||||
public interface IExerciceInstanceRepository : IRepository<ExerciceInstance>
|
||||
{
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
using CatalogService.Entities;
|
||||
using Shared.Infrastructure;
|
||||
|
||||
namespace TrainingSvc.Repositories;
|
||||
|
||||
public interface IExerciceTemplateRepository : IRepository<ExerciceTemplate>
|
||||
{
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
using Shared.Infrastructure;
|
||||
using TrainingSvc.Entities;
|
||||
|
||||
namespace TrainingSvc.Repositories;
|
||||
|
||||
public interface ISessionRepository : IRepository<Session>
|
||||
{
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
using Shared.Infrastructure;
|
||||
using TrainingSvc.Entities;
|
||||
|
||||
namespace TrainingSvc.Repositories;
|
||||
|
||||
public interface ITrainingProgramRepository : IRepository<TrainingProgram>
|
||||
{
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using TrainingSvc.Data;
|
||||
using TrainingSvc.Entities;
|
||||
|
||||
namespace TrainingSvc.Repositories;
|
||||
|
||||
public class SessionRepository : GenericRepository<Session>, ISessionRepository
|
||||
{
|
||||
public SessionRepository(TrainingDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using TrainingSvc.Data;
|
||||
using TrainingSvc.Entities;
|
||||
|
||||
namespace TrainingSvc.Repositories;
|
||||
|
||||
public class TrainingProgramRepository : GenericRepository<TrainingProgram>, ITrainingProgramRepository
|
||||
{
|
||||
public TrainingProgramRepository(TrainingDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
}
|
Loading…
Reference in new issue