DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/_CachedCrudService.cs
Фролов 0b1f9683b9 fix jsonSerializer settings;
reafctor TelemetryHub;
Add wellDepth into TelemetryMessage
2021-09-17 16:24:01 +05:00

49 lines
1.5 KiB
C#

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<Tdto, TModel> : ICachedCrudService<Tdto>
where TModel : class, AsbCloudDb.Model.IId
where Tdto : AsbCloudApp.Data.IId
{
private readonly CacheTable<TModel> cache;
public _CachedCrudService(IAsbCloudDbContext db, Cache.CacheDb cacheDb)
{
cache = cacheDb.GetCachedTable<TModel>((DbContext)db);
}
public virtual async Task<PaginationContainer<Tdto>> GetAsync(int skip = 0, int take = 32, CancellationToken token = default)
{
var count = cache.Count();
var result = new PaginationContainer<Tdto> { 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<Tdto> GetAsync(int id, CancellationToken token = default)
{
throw new NotImplementedException();
}
public virtual Tdto Convert(TModel src) => src.Adapt<Tdto>();
public virtual TModel Convert(Tdto src) => src.Adapt<TModel>();
}
}