using System.Linq.Dynamic.Core;
|
using CMS.Plugin.PipeLineLems.Domain.MyTestEntityNames;
|
using CMS.Plugin.PipeLineLems.EntityFrameworkCore.Extensions;
|
using Microsoft.EntityFrameworkCore;
|
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
|
using Volo.Abp.EntityFrameworkCore;
|
using Volo.Abp.Specifications;
|
|
namespace CMS.Plugin.PipeLineLems.EntityFrameworkCore.Repositories;
|
|
/// <inheritdoc />
|
public class EfCoreMyTestEntityNameRepository : EfCoreRepository<ICMSPluginDbContext, MyTestEntityName, Guid>, IMyTestEntityNameRepository
|
{
|
/// <summary>
|
/// Initializes a new instance of the <see cref="EfCoreMyTestEntityNameRepository"/> class.
|
/// </summary>
|
/// <param name="dbContextProvider">The database context provider.</param>
|
public EfCoreMyTestEntityNameRepository(IDbContextProvider<ICMSPluginDbContext> dbContextProvider)
|
: base(dbContextProvider)
|
{
|
}
|
|
/// <inheritdoc />
|
public virtual async Task<MyTestEntityName> FindByNameAsync(string name, CancellationToken cancellationToken = default)
|
{
|
return await (await GetDbSetAsync())
|
.IncludeDetails()
|
.OrderBy(t => t.Sort)
|
.FirstOrDefaultAsync(t => t.Name == name, GetCancellationToken(cancellationToken));
|
}
|
|
/// <inheritdoc />
|
public async Task<bool> NameExistAsync(string name, Guid? id = null)
|
{
|
return await (await GetDbSetAsync()).WhereIf(id.HasValue, p => p.Id != id).AnyAsync(x => x.Name == name);
|
}
|
|
/// <inheritdoc />
|
public async Task<int> GetMaxSortAsync()
|
{
|
var hasAny = await (await GetQueryableAsync()).AnyAsync();
|
if (!hasAny)
|
{
|
return 1;
|
}
|
|
var sort = await (await GetQueryableAsync()).MaxAsync(x => x.Sort);
|
return sort + 1;
|
}
|
|
/// <inheritdoc />
|
public async Task<List<MyTestEntityName>> GetListAsync(string sorting = null, int maxResultCount = int.MaxValue, int skipCount = 0, string filter = null, Specification<MyTestEntityName> specification = null, bool includeDetails = false, CancellationToken cancellationToken = default)
|
{
|
specification ??= new MyTestEntityNameSpecification();
|
return await (await GetDbSetAsync())
|
.IncludeDetails(includeDetails)
|
.Where(specification.ToExpression())
|
.WhereIf(!filter.IsNullOrWhiteSpace(), u => u.Name.Contains(filter))
|
.OrderBy(sorting.IsNullOrEmpty() ? nameof(MyTestEntityName.Sort) : sorting)
|
.PageBy(skipCount, maxResultCount)
|
.ToListAsync(GetCancellationToken(cancellationToken));
|
}
|
|
/// <inheritdoc />
|
public async Task<long> GetCountAsync(string filter = null, Specification<MyTestEntityName> specification = null, CancellationToken cancellationToken = default)
|
{
|
specification ??= new MyTestEntityNameSpecification();
|
return await (await GetQueryableAsync())
|
.Where(specification.ToExpression())
|
.WhereIf(!filter.IsNullOrWhiteSpace(), u => u.Name.Contains(filter))
|
.CountAsync(cancellationToken: GetCancellationToken(cancellationToken));
|
}
|
|
/// <inheritdoc />
|
public override async Task<IQueryable<MyTestEntityName>> WithDetailsAsync()
|
{
|
return (await GetQueryableAsync()).IncludeDetails();
|
}
|
}
|