From 43bdc9e92e095414cbf767a0f00cfa92d749d404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Mon, 20 Dec 2021 15:15:20 +0500 Subject: [PATCH] Add CrudCacheServiceBase --- .../Services/CrudCacheServiceBase.cs | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 AsbCloudInfrastructure/Services/CrudCacheServiceBase.cs diff --git a/AsbCloudInfrastructure/Services/CrudCacheServiceBase.cs b/AsbCloudInfrastructure/Services/CrudCacheServiceBase.cs new file mode 100644 index 00000000..207cb69d --- /dev/null +++ b/AsbCloudInfrastructure/Services/CrudCacheServiceBase.cs @@ -0,0 +1,118 @@ +using AsbCloudApp.Services; +using AsbCloudDb.Model; +using AsbCloudInfrastructure.Services.Cache; +using Mapster; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace AsbCloudInfrastructure.Services +{ + public class CrudCacheServiceBase : ICrudService + where TDto : AsbCloudApp.Data.IId + where TModel : class, AsbCloudDb.Model.IId + { + private CacheTable cache = null; + private readonly IAsbCloudDbContext db; + private readonly CacheDb cacheDb; + + public List Includes { get; } = new (); + + private CacheTable Cache { + get { + if(cache is null) + cache = cacheDb.GetCachedTable((AsbCloudDbContext)db, Includes); + return cache; + } + } + + public CrudCacheServiceBase(IAsbCloudDbContext db, CacheDb cacheDb) + { + this.db = db; + this.cacheDb = cacheDb; + } + + public virtual async Task InsertAsync(TDto newItem, CancellationToken token = default) + { + var entity = Convert(newItem); + var insertedEntity = await Cache.InsertAsync(entity, token) + .ConfigureAwait(false); + return insertedEntity?.Id??-1; + } + + public virtual async Task InsertRangeAsync(IEnumerable dtos, CancellationToken token) + { + var entities = dtos.Select(Convert); + var insertedEntities = await Cache.InsertAsync(entities, token) + .ConfigureAwait(false); + return insertedEntities?.Count()??0; + } + + public virtual async Task> GetAllAsync(CancellationToken token) + { + var entities = await Cache.WhereAsync(token) + .ConfigureAwait(false); + var dtos = entities?.Select(Convert); + return dtos; + } + + public virtual async Task GetAsync(int id, CancellationToken token) + { + var entity = await Cache + .FirstOrDefaultAsync(p => p.Id == id, token) + .ConfigureAwait(false); + if (entity is null) + return default; + var dto = Convert(entity); + return dto; + } + + public virtual async Task UpdateAsync(int id, TDto dto, CancellationToken token) + { + if (dto.Id != id) + { + var exist = await Cache.ContainsAsync(i=>i.Id == dto.Id, token) + .ConfigureAwait(false); + + if (exist) + return -1; + + await Cache.RemoveAsync(i => i.Id == id, token) + .ConfigureAwait(false); + } + + var entity = Convert(dto); + await Cache.UpsertAsync(entity, token) + .ConfigureAwait(false); + return 1; + } + + public virtual async Task DeleteAsync(int id, CancellationToken token) + { + var affected = await Cache.RemoveAsync(p => p.Id == id, token) + .ConfigureAwait(false); + return affected; + } + + public virtual async Task DeleteAsync(IEnumerable ids, CancellationToken token = default) + { + var affected = await Cache.RemoveAsync(p => ids.Contains(p.Id), token) + .ConfigureAwait(false); + return affected; + } + + private TModel Convert(TDto src) + { + var entity = src.Adapt(); + return entity; + } + + private TDto Convert(TModel src) + { + var dto = src.Adapt(); + return dto; + } + } +} \ No newline at end of file