using System.Linq.Dynamic.Core;
|
using CMS.Plugin.WareCmsUtilityApi.Domain.Samples;
|
using CMS.Plugin.WareCmsUtilityApi.EntityFrameworkCore.Extensions;
|
using Microsoft.EntityFrameworkCore;
|
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
|
using Volo.Abp.EntityFrameworkCore;
|
using Volo.Abp.Specifications;
|
|
namespace CMS.Plugin.WareCmsUtilityApi.EntityFrameworkCore.Repositories;
|
|
/// <inheritdoc />
|
public class EfCoreSampleRepository : EfCoreRepository<ICMSPluginDbContext, Sample, Guid>, ISampleRepository
|
{
|
/// <summary>
|
/// Initializes a new instance of the <see cref="EfCoreSampleRepository"/> class.
|
/// </summary>
|
/// <param name="dbContextProvider">The database context provider.</param>
|
public EfCoreSampleRepository(IDbContextProvider<ICMSPluginDbContext> dbContextProvider)
|
: base(dbContextProvider)
|
{
|
}
|
|
/// <inheritdoc />
|
public virtual async Task<Sample> 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<Sample>> GetListAsync(string sorting = null, int maxResultCount = int.MaxValue, int skipCount = 0, string filter = null, Specification<Sample> specification = null, bool includeDetails = false, CancellationToken cancellationToken = default)
|
{
|
specification ??= new SampleSpecification();
|
return await (await GetDbSetAsync())
|
.IncludeDetails(includeDetails)
|
.Where(specification.ToExpression())
|
.WhereIf(!filter.IsNullOrWhiteSpace(), u => u.Name.Contains(filter))
|
.OrderBy(sorting.IsNullOrEmpty() ? nameof(Sample.Sort) : sorting)
|
.PageBy(skipCount, maxResultCount)
|
.ToListAsync(GetCancellationToken(cancellationToken));
|
}
|
|
/// <inheritdoc />
|
public async Task<long> GetCountAsync(string filter = null, Specification<Sample> specification = null, CancellationToken cancellationToken = default)
|
{
|
specification ??= new SampleSpecification();
|
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<Sample>> WithDetailsAsync()
|
{
|
return (await GetQueryableAsync()).IncludeDetails();
|
}
|
}
|