forked from ddrilling/AsbCloudServer
4db67113b4
CrudCacheService Адаптировано для новой схемы кеширования. Убраны extention методы для mapster.
151 lines
4.9 KiB
C#
151 lines
4.9 KiB
C#
using AsbCloudApp.Services;
|
||
using AsbCloudDb.Model;
|
||
using Mapster;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace AsbCloudInfrastructure.Services
|
||
{
|
||
#nullable enable
|
||
/// <summary>
|
||
/// CRUD сервис для работы с БД
|
||
/// </summary>
|
||
/// <typeparam name="TDto"></typeparam>
|
||
/// <typeparam name="TEntity"></typeparam>
|
||
public class CrudServiceBase<TDto, TEntity> : ICrudService<TDto>
|
||
where TDto : AsbCloudApp.Data.IId
|
||
where TEntity : class, AsbCloudDb.Model.IId
|
||
{
|
||
protected readonly IAsbCloudDbContext dbContext;
|
||
protected readonly DbSet<TEntity> dbSet;
|
||
protected readonly Func<IQueryable<TEntity>> GetQuery;
|
||
|
||
public CrudServiceBase(IAsbCloudDbContext context)
|
||
{
|
||
this.dbContext = context;
|
||
dbSet = context.Set<TEntity>();
|
||
GetQuery = () => dbSet;
|
||
}
|
||
|
||
public CrudServiceBase(IAsbCloudDbContext dbContext, ISet<string> includes)
|
||
{
|
||
this.dbContext = dbContext;
|
||
dbSet = dbContext.Set<TEntity>();
|
||
|
||
GetQuery = () => {
|
||
IQueryable<TEntity> query = dbSet;
|
||
foreach (var include in includes)
|
||
query = query.Include(include);
|
||
return query;
|
||
};
|
||
}
|
||
|
||
public CrudServiceBase(IAsbCloudDbContext context, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery)
|
||
{
|
||
this.dbContext = context;
|
||
dbSet = context.Set<TEntity>();
|
||
GetQuery = () => makeQuery(dbSet);
|
||
}
|
||
|
||
/// <inheritdoc/>
|
||
public virtual async Task<IEnumerable<TDto>> GetAllAsync(CancellationToken token = default)
|
||
{
|
||
var entities = await GetQuery()
|
||
//.OrderBy(e => e.Id)
|
||
.AsNoTracking()
|
||
.ToListAsync(token)
|
||
.ConfigureAwait(false);
|
||
var dtos = entities.Select(Convert).ToList();
|
||
return dtos;
|
||
}
|
||
|
||
/// <inheritdoc/>
|
||
public virtual async Task<TDto?> GetAsync(int id, CancellationToken token = default)
|
||
{
|
||
var entity = await GetQuery()
|
||
.AsNoTracking()
|
||
.FirstOrDefaultAsync(e => e.Id == id, token)
|
||
.ConfigureAwait(false);
|
||
if (entity == default)
|
||
return default;
|
||
var dto = Convert(entity);
|
||
return dto;
|
||
}
|
||
|
||
/// <inheritdoc/>
|
||
public virtual TDto? Get(int id)
|
||
{
|
||
var entity = GetQuery()
|
||
.AsNoTracking()
|
||
.FirstOrDefault(e => e.Id == id);
|
||
if (entity == default)
|
||
return default;
|
||
var dto = Convert(entity);
|
||
return dto;
|
||
}
|
||
|
||
/// <inheritdoc/>
|
||
public virtual async Task<int> InsertAsync(TDto item, CancellationToken token = default)
|
||
{
|
||
var entity = Convert(item);
|
||
entity.Id = 0;
|
||
dbSet.Add(entity);
|
||
await dbContext.SaveChangesAsync(token);
|
||
return entity.Id;
|
||
}
|
||
|
||
/// <inheritdoc/>
|
||
public virtual Task<int> InsertRangeAsync(IEnumerable<TDto> items, CancellationToken token = default)
|
||
{
|
||
if (!items.Any())
|
||
return Task.FromResult(0);
|
||
var entities = items.Select(i =>
|
||
{
|
||
var entity = Convert(i);
|
||
entity.Id = 0;
|
||
return entity;
|
||
});
|
||
|
||
dbSet.AddRange(entities);
|
||
return dbContext.SaveChangesAsync(token);
|
||
}
|
||
|
||
/// <inheritdoc/>
|
||
public virtual async Task<int> UpdateAsync(int id, TDto item, CancellationToken token = default)
|
||
{
|
||
var existingEntity = await dbSet
|
||
.AsNoTracking()
|
||
.FirstOrDefaultAsync(e => e.Id == id, token)
|
||
.ConfigureAwait(false);
|
||
if (existingEntity is null)
|
||
return ICrudService<TDto>.ErrorIdNotFound;
|
||
var entity = Convert(item);
|
||
entity.Id = id;
|
||
var entry = dbSet.Update(entity);
|
||
await dbContext.SaveChangesAsync(token);
|
||
return entry.Entity.Id;
|
||
}
|
||
|
||
/// <inheritdoc/>
|
||
public virtual Task<int> DeleteAsync(int id, CancellationToken token = default)
|
||
{
|
||
var entity = dbSet
|
||
.AsNoTracking()
|
||
.FirstOrDefault(e => e.Id == id);
|
||
if (entity == default)
|
||
return Task.FromResult(ICrudService<TDto>.ErrorIdNotFound);
|
||
dbSet.Remove(entity);
|
||
return dbContext.SaveChangesAsync(token);
|
||
}
|
||
|
||
protected virtual TDto Convert(TEntity src) => src.Adapt<TDto>();
|
||
|
||
protected virtual TEntity Convert(TDto src) => src.Adapt<TEntity>();
|
||
}
|
||
#nullable disable
|
||
}
|