2022-12-01 15:56:11 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
2022-12-05 09:36:02 +05:00
|
|
|
|
using Mapster;
|
2022-12-01 15:56:11 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Repository
|
|
|
|
|
{
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2022-12-01 15:56:11 +05:00
|
|
|
|
/// <summary>
|
2024-07-04 11:02:45 +05:00
|
|
|
|
/// CRUD сервис с кешем в оперативке
|
2022-12-01 15:56:11 +05:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto"></typeparam>
|
|
|
|
|
/// <typeparam name="TEntity"></typeparam>
|
2022-12-05 09:36:02 +05:00
|
|
|
|
public class CrudCacheRepositoryBase<TDto, TEntity> : CacheBase<TEntity>, ICrudRepository<TDto>
|
2022-12-01 15:56:11 +05:00
|
|
|
|
where TDto : AsbCloudApp.Data.IId
|
|
|
|
|
where TEntity : class, IId
|
|
|
|
|
{
|
|
|
|
|
protected int KeySelector(TEntity entity) => entity.Id;
|
|
|
|
|
protected readonly ICrudRepository<TDto> crudServiceBase;
|
|
|
|
|
|
|
|
|
|
public CrudCacheRepositoryBase(IAsbCloudDbContext dbContext, IMemoryCache memoryCache)
|
|
|
|
|
: base(dbContext, memoryCache)
|
|
|
|
|
{
|
|
|
|
|
crudServiceBase = new CrudRepositoryBase<TDto, TEntity>(dbContext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CrudCacheRepositoryBase(IAsbCloudDbContext dbContext, IMemoryCache memoryCache, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery)
|
|
|
|
|
: base(dbContext, memoryCache, makeQuery)
|
|
|
|
|
{
|
|
|
|
|
crudServiceBase = new CrudRepositoryBase<TDto, TEntity>(dbContext, makeQuery);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2022-12-05 09:36:02 +05:00
|
|
|
|
public virtual async Task<int> InsertAsync(TDto newItem, CancellationToken token)
|
2022-12-01 15:56:11 +05:00
|
|
|
|
{
|
|
|
|
|
var result = await crudServiceBase.InsertAsync(newItem, token);
|
|
|
|
|
if (result > 0)
|
|
|
|
|
DropCache();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2022-12-05 09:36:02 +05:00
|
|
|
|
public virtual async Task<int> InsertRangeAsync(IEnumerable<TDto> dtos, CancellationToken token)
|
2022-12-01 15:56:11 +05:00
|
|
|
|
{
|
|
|
|
|
var result = await crudServiceBase.InsertRangeAsync(dtos, token);
|
|
|
|
|
if (result > 0)
|
|
|
|
|
DropCache();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 09:36:02 +05:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public virtual async Task<int> UpdateAsync(TDto dto, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var result = await crudServiceBase.UpdateAsync(dto, token);
|
|
|
|
|
if (result > 0)
|
|
|
|
|
DropCache();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-01 15:56:11 +05:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public async Task<IEnumerable<TDto>> GetAllAsync(CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var cache = await GetCacheAsync(token);
|
2022-12-05 09:36:02 +05:00
|
|
|
|
var dtos = cache.Select(Convert);
|
|
|
|
|
return dtos;
|
2022-12-01 15:56:11 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-07-04 11:02:45 +05:00
|
|
|
|
/// Синхронно получить запись по ИД
|
2022-12-01 15:56:11 +05:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public TDto? GetOrDefault(int id)
|
|
|
|
|
{
|
|
|
|
|
var cache = GetCache();
|
2022-12-05 09:36:02 +05:00
|
|
|
|
var cacheItem = cache.FirstOrDefault(d => d.Id == id);
|
|
|
|
|
if (cacheItem is null)
|
|
|
|
|
return default;
|
|
|
|
|
var dto = Convert(cacheItem);
|
|
|
|
|
return dto;
|
2022-12-01 15:56:11 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public async Task<TDto?> GetOrDefaultAsync(int id, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var cache = await GetCacheAsync(token);
|
2022-12-05 09:36:02 +05:00
|
|
|
|
var cacheItem = cache.FirstOrDefault(d => d.Id == id);
|
|
|
|
|
if (cacheItem is null)
|
|
|
|
|
return default;
|
|
|
|
|
var dto = Convert(cacheItem);
|
|
|
|
|
return dto;
|
2022-12-01 15:56:11 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2022-12-05 09:36:02 +05:00
|
|
|
|
public virtual async Task<int> DeleteAsync(int id, CancellationToken token)
|
2022-12-01 15:56:11 +05:00
|
|
|
|
{
|
|
|
|
|
var result = await crudServiceBase.DeleteAsync(id, token);
|
|
|
|
|
if (result > 0)
|
|
|
|
|
DropCache();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2022-12-05 09:36:02 +05:00
|
|
|
|
|
|
|
|
|
protected virtual TDto Convert(TEntity src) => src.Adapt<TDto>();
|
|
|
|
|
|
|
|
|
|
protected virtual TEntity Convert(TDto src) => src.Adapt<TEntity>();
|
2022-12-01 15:56:11 +05:00
|
|
|
|
}
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2022-12-01 15:56:11 +05:00
|
|
|
|
}
|