using AsbCloudApp.Data; using AsbCloudApp.Services; using AsbCloudDb.Model; using AsbCloudInfrastructure.Services.Cache; using Mapster; using Microsoft.EntityFrameworkCore; using System; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace AsbCloudInfrastructure.Services { public class _CachedCrudService : ICachedCrudService where TModel : class, AsbCloudDb.Model.IId where Tdto : AsbCloudApp.Data.IId { private readonly CacheTable cache; public _CachedCrudService(IAsbCloudDbContext db, Cache.CacheDb cacheDb) { cache = cacheDb.GetCachedTable((DbContext)db); } public virtual async Task> GetAsync(int skip = 0, int take = 32, CancellationToken token = default) { var count = cache.Count(); var result = new PaginationContainer { Skip = skip, Take = take, Count = count }; if (count <= skip) return result; var items = await cache.WhereAsync(token).ConfigureAwait(false); result.Items.AddRange(items.OrderBy(i => i.Id).Skip(skip).Take(take).Select(i => Convert(i))); return result; } public virtual Task GetAsync(int id, CancellationToken token = default) { throw new NotImplementedException(); } public virtual Tdto Convert(TModel src) => src.Adapt(); public virtual TModel Convert(Tdto src) => src.Adapt(); } }