Files
xDataService/Providers/XIntKeyGenerator.cs
T
2026-05-04 00:37:06 +03:30

41 lines
1000 B
C#

using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using xCommons.Extensions;
using xDataService.Extensions;
using xDataService.Interfaces;
using xModels.Base;
namespace xDataService.Providers
{
public class XIntKeyGenerator<TEntity> : IXKeyGenerator<TEntity, int>
where TEntity : XBaseEntity<int>
{
public async Task<int> GenerateKey(
IXBaseRepository<TEntity, int> repository,
CancellationToken cancellationToken = default
)
{
//
var items = (await repository.GetAllAsync())
.OrderByDescending(nameof(XBaseEntity<int>.Id));
var last = items
.FirstOrDefault();
//
var result = last.IsNull() ? 1 : last.Id + 1;
//
return result;
}
public bool IsEmpty(int id)
{
//
var result = id <= 0;
//
return result;
}
}
}