Files
xDataService/Providers/XIntKeyGenerator.cs
2026-05-09 22:21:40 +03:30

45 lines
1.1 KiB
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
{
/// <summary>
/// Default Int Key Generator ...
/// </summary>
/// <typeparam name="TEntity"></typeparam>
public class XIntKeyGenerator<TEntity> : XKeyGenerator<TEntity, int>, IXKeyGenerator<TEntity, int>
where TEntity : XBaseEntity<int>
{
public override 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 override bool IsEmpty(int id)
{
//
var result = id <= 0;
//
return result;
}
}
}