DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/CrudServiceBase.cs

186 lines
6.1 KiB
C#
Raw Normal View History

using AsbCloudApp.Services;
2021-08-10 14:36:35 +05:00
using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore;
2021-09-10 11:28:57 +05:00
using System;
2021-08-10 14:36:35 +05:00
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository
2021-08-10 14:36:35 +05:00
{
/// <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, IId
2021-08-10 14:36:35 +05:00
{
protected readonly IAsbCloudDbContext dbContext;
protected readonly DbSet<TEntity> dbSet;
protected readonly Func<IQueryable<TEntity>> GetQuery;
2021-09-10 11:28:57 +05:00
2021-08-10 14:36:35 +05:00
public CrudServiceBase(IAsbCloudDbContext context)
{
dbContext = context;
dbSet = context.Set<TEntity>();
GetQuery = () => dbSet;
2021-08-10 14:36:35 +05:00
}
public CrudServiceBase(IAsbCloudDbContext dbContext, ISet<string> includes)
2021-09-10 11:28:57 +05:00
{
this.dbContext = dbContext;
dbSet = dbContext.Set<TEntity>();
2022-06-15 14:57:37 +05:00
GetQuery = () =>
{
IQueryable<TEntity> query = dbSet;
foreach (var include in includes)
query = query.Include(include);
return query;
2021-09-10 11:28:57 +05:00
};
}
2021-09-10 11:28:57 +05:00
public CrudServiceBase(IAsbCloudDbContext context, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery)
{
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()
2021-09-10 11:28:57 +05:00
.ToListAsync(token)
.ConfigureAwait(false);
var dtos = entities.Select(Convert).ToList();
return dtos;
2021-09-10 11:28:57 +05:00
}
/// <inheritdoc/>
public virtual async Task<TDto?> GetOrDefaultAsync(int id, CancellationToken token = default)
2021-09-10 11:28:57 +05:00
{
var entity = await GetQuery()
.AsNoTracking()
.FirstOrDefaultAsync(e => e.Id == id, token)
.ConfigureAwait(false);
if (entity == default)
return default;
var dto = Convert(entity);
2021-09-10 11:28:57 +05:00
return dto;
}
/// <inheritdoc/>
public virtual TDto? GetOrDefault(int id)
2021-08-10 14:36:35 +05:00
{
var entity = GetQuery()
.AsNoTracking()
.FirstOrDefault(e => e.Id == id);
if (entity == default)
return default;
2021-09-10 11:28:57 +05:00
var dto = Convert(entity);
2021-08-10 14:36:35 +05:00
return dto;
}
/// <inheritdoc/>
public virtual async Task<int> InsertAsync(TDto item, CancellationToken token = default)
2021-08-10 14:36:35 +05:00
{
2021-09-10 11:28:57 +05:00
var entity = Convert(item);
entity.Id = 0;
2022-06-15 14:57:37 +05:00
var entry = dbSet.Add(entity);
await dbContext.SaveChangesAsync(token);
2022-06-09 17:25:26 +05:00
entry.State = EntityState.Detached;
return entity.Id;
2021-08-10 14:36:35 +05:00
}
/// <inheritdoc/>
2022-06-09 17:25:26 +05:00
public virtual async Task<int> InsertRangeAsync(IEnumerable<TDto> items, CancellationToken token = default)
2021-08-10 14:36:35 +05:00
{
if (!items.Any())
2022-06-09 17:25:26 +05:00
return 0;
2022-04-11 18:00:34 +05:00
var entities = items.Select(i =>
{
var entity = Convert(i);
entity.Id = 0;
return entity;
});
2022-06-09 17:25:26 +05:00
var entries = new List<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry>(items.Count());
foreach (var entity in entities)
{
var entry = dbSet.Add(entity);
entries.Add(entry);
}
var affected = await dbContext.SaveChangesAsync(token);
entries.ForEach(e => e.State = EntityState.Detached);
return affected;
2021-08-10 14:36:35 +05:00
}
/// <inheritdoc/>
public virtual async Task<int> UpdateAsync(TDto item, CancellationToken token = default)
2021-08-10 14:36:35 +05:00
{
var existingEntity = await dbSet
.AsNoTracking()
.FirstOrDefaultAsync(e => e.Id == item.Id, token)
.ConfigureAwait(false);
2022-06-15 14:57:37 +05:00
if (existingEntity is null)
return ICrudService<TDto>.ErrorIdNotFound;
2022-06-15 14:57:37 +05:00
2021-09-10 11:28:57 +05:00
var entity = Convert(item);
var entry = dbSet.Update(entity);
await dbContext.SaveChangesAsync(token);
2022-06-09 17:25:26 +05:00
entry.State = EntityState.Detached;
return entry.Entity.Id;
2021-08-10 14:36:35 +05:00
}
2022-06-15 14:57:37 +05:00
public virtual async Task<int> UpdateRangeAsync(IEnumerable<TDto> dtos, CancellationToken token)
{
var ids = dtos.Select(d => d.Id);
var existingEntities = await dbSet
.AsNoTracking()
.Where(d => ids.Contains(d.Id))
.Select(d => d.Id)
.ToListAsync(token)
.ConfigureAwait(false);
if (ids.Count() > existingEntities.Count)
return ICrudService<TDto>.ErrorIdNotFound;
foreach (var dto in dtos)
{
var entity = Convert(dto);
var entry = dbSet.Update(entity);
}
var affected = await dbContext.SaveChangesAsync(token);
return affected;
}
/// <inheritdoc/>
2021-08-10 14:36:35 +05:00
public virtual Task<int> DeleteAsync(int id, CancellationToken token = default)
{
var entity = dbSet
.AsNoTracking()
.FirstOrDefault(e => e.Id == id);
2021-08-10 14:36:35 +05:00
if (entity == default)
return Task.FromResult(ICrudService<TDto>.ErrorIdNotFound);
2022-06-09 17:25:26 +05:00
var entry = dbSet.Remove(entity);
var affected = dbContext.SaveChangesAsync(token);
entry.State = EntityState.Detached;
return affected;
2021-08-10 14:36:35 +05:00
}
protected virtual TDto Convert(TEntity src) => src.Adapt<TDto>();
2021-09-10 11:28:57 +05:00
protected virtual TEntity Convert(TDto src) => src.Adapt<TEntity>();
2021-08-10 14:36:35 +05:00
}
#nullable disable
2021-08-10 14:36:35 +05:00
}