2021-04-05 10:08:49 +04:00
|
|
|
|
using DatabaseCore.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using ModuleTools.BindingModels;
|
|
|
|
|
using ModuleTools.BusinessLogics;
|
|
|
|
|
using ModuleTools.Enums;
|
|
|
|
|
using ModuleTools.Interfaces;
|
|
|
|
|
using ModuleTools.Models;
|
|
|
|
|
using ModuleTools.ViewModels;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2021-04-14 13:47:48 +04:00
|
|
|
|
using System.Threading.Tasks;
|
2021-04-05 10:08:49 +04:00
|
|
|
|
|
|
|
|
|
namespace DatabaseCore
|
|
|
|
|
{
|
|
|
|
|
public abstract class AbstractGenerticEntityService<G, S, T, L, E> : IGenerticEntityService<G, S>
|
|
|
|
|
where G : GetBindingModel
|
|
|
|
|
where S : SetBindingModel
|
|
|
|
|
where T : BaseEntity
|
|
|
|
|
where L : ListViewModel<E>, new()
|
|
|
|
|
where E : ElementViewModel
|
|
|
|
|
{
|
2021-04-14 13:47:48 +04:00
|
|
|
|
public async Task<OperationResultModel> CreateAsync(S model)
|
2021-04-05 10:08:49 +04:00
|
|
|
|
{
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
|
|
|
|
|
var result = AdditionalCheckingWhenAdding(context, model);
|
|
|
|
|
if (!result.IsSucceeded)
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var exsistEntity = GetUniqueEntity(model, context);
|
|
|
|
|
if (exsistEntity == null)
|
|
|
|
|
{
|
|
|
|
|
var entity = Mapper.MapToClass<S, T>(model, true);
|
2021-04-14 13:47:48 +04:00
|
|
|
|
await context.Set<T>().AddAsync(entity);
|
|
|
|
|
await context.SaveChangesAsync();
|
2021-04-05 10:08:49 +04:00
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<T, E>(entity, true));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (exsistEntity.IsDeleted)
|
|
|
|
|
{
|
|
|
|
|
exsistEntity = Mapper.MapToClass(model, exsistEntity, true);
|
|
|
|
|
exsistEntity.IsDeleted = false;
|
2021-04-14 13:47:48 +04:00
|
|
|
|
await context.SaveChangesAsync();
|
2021-04-05 10:08:49 +04:00
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<T, E>(exsistEntity, true));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 13:47:48 +04:00
|
|
|
|
public async Task<OperationResultModel> DeleteAsync(G model)
|
2021-04-05 10:08:49 +04:00
|
|
|
|
{
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-04-28 17:58:52 +04:00
|
|
|
|
var entity = await context.Set<T>().FirstOrDefaultAsync(x => x.Id == model.Id);
|
2021-04-05 10:08:49 +04:00
|
|
|
|
if (entity == null)
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
else if (entity.IsDeleted)
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 12:25:10 +04:00
|
|
|
|
var result = AdditionalCheckingWhenDeleting(context, entity, model);
|
2021-04-05 10:08:49 +04:00
|
|
|
|
if (!result.IsSucceeded)
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entity.IsDeleted = true;
|
|
|
|
|
entity.DateDelete = DateTime.Now;
|
|
|
|
|
|
2021-04-14 13:47:48 +04:00
|
|
|
|
await context.SaveChangesAsync();
|
2021-04-05 10:08:49 +04:00
|
|
|
|
|
2021-04-05 12:25:10 +04:00
|
|
|
|
AdditionalDeleting(context, entity, model);
|
2021-04-05 10:08:49 +04:00
|
|
|
|
|
2021-04-14 13:47:48 +04:00
|
|
|
|
await transaction.CommitAsync();
|
2021-04-05 10:08:49 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
2021-04-14 13:47:48 +04:00
|
|
|
|
await transaction.RollbackAsync();
|
2021-04-05 10:08:49 +04:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return OperationResultModel.Success(true);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 17:58:52 +04:00
|
|
|
|
public async Task<OperationResultModel> RestoreAsync(G model)
|
|
|
|
|
{
|
|
|
|
|
if (model.Id.HasValue || AdditionalCheckForSingleGet(model))
|
|
|
|
|
{
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
var entity = model.Id.HasValue ? IncludingWhenReading(context.Set<T>().AsQueryable()).FirstOrDefault(x => x.Id == model.Id.Value)
|
|
|
|
|
: GetSingleRecord(context.Set<T>().AsQueryable(), model);
|
|
|
|
|
if (entity == null)
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
else if (!entity.IsDeleted)
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент не был удален", ResultServiceStatusCode.ExsistItem);
|
|
|
|
|
}
|
|
|
|
|
entity.IsDeleted = false;
|
|
|
|
|
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<T, E>(entity, true));
|
|
|
|
|
}
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 13:47:48 +04:00
|
|
|
|
public async Task<OperationResultModel> ReadAsync(G model)
|
2021-04-05 10:08:49 +04:00
|
|
|
|
{
|
|
|
|
|
int countPages = 0;
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
|
|
|
|
|
// для одной записи
|
2021-04-27 17:09:03 +04:00
|
|
|
|
if (model.Id.HasValue || AdditionalCheckForSingleGet(model))
|
2021-04-05 10:08:49 +04:00
|
|
|
|
{
|
2021-04-27 17:09:03 +04:00
|
|
|
|
var entity = model.Id.HasValue ? IncludingWhenReading(context.Set<T>().AsQueryable()).FirstOrDefault(x => x.Id == model.Id.Value)
|
|
|
|
|
: GetSingleRecord(context.Set<T>().AsQueryable(), model);
|
2021-04-05 10:08:49 +04:00
|
|
|
|
if (entity == null)
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|
|
|
|
}
|
2021-04-28 17:58:52 +04:00
|
|
|
|
if (entity.IsDeleted)
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент удален", ResultServiceStatusCode.WasDelete);
|
|
|
|
|
}
|
2021-04-05 10:08:49 +04:00
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<T, E>(entity, model.HaveRight));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var query = context.Set<T>().Where(x => !x.IsDeleted).AsQueryable();
|
|
|
|
|
|
|
|
|
|
query = AdditionalCheckingWhenReadingList(query, model);
|
|
|
|
|
|
|
|
|
|
query = OrderingWhenReading(query);
|
|
|
|
|
|
|
|
|
|
query = IncludingWhenReading(query);
|
|
|
|
|
|
2021-04-14 13:47:48 +04:00
|
|
|
|
return await Task.Run(() =>
|
2021-04-05 10:08:49 +04:00
|
|
|
|
{
|
|
|
|
|
|
2021-04-14 13:47:48 +04:00
|
|
|
|
if (model.PageNumber.HasValue && model.PageSize.HasValue)
|
|
|
|
|
{
|
|
|
|
|
countPages = (int)Math.Ceiling((double)query.Count() / model.PageSize.Value);
|
|
|
|
|
query = query
|
|
|
|
|
.Skip(model.PageSize.Value * model.PageNumber.Value)
|
|
|
|
|
.Take(model.PageSize.Value);
|
|
|
|
|
}
|
|
|
|
|
var result = new L
|
|
|
|
|
{
|
|
|
|
|
MaxCount = countPages,
|
|
|
|
|
List = query.Select(x => Mapper.MapToClass<T, E>(x, model.HaveRight)).ToList()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return OperationResultModel.Success(result);
|
|
|
|
|
});
|
2021-04-05 10:08:49 +04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 13:47:48 +04:00
|
|
|
|
public async Task<OperationResultModel> UpdateAsync(S model)
|
2021-04-05 10:08:49 +04:00
|
|
|
|
{
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
|
|
|
|
|
var result = AdditionalCheckingWhenUpdateing(context, model);
|
|
|
|
|
if (!result.IsSucceeded)
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var exsistEntity = GetUniqueEntity(model, context);
|
|
|
|
|
if (exsistEntity != null)
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Существует запись с такими значениями", ResultServiceStatusCode.ExsistItem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var entity = context.Set<T>().FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (entity == null)
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
else if (entity.IsDeleted)
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
|
|
|
|
|
}
|
|
|
|
|
entity = Mapper.MapToClass(model, entity, true);
|
|
|
|
|
|
2021-04-14 13:47:48 +04:00
|
|
|
|
await context.SaveChangesAsync();
|
2021-04-05 10:08:49 +04:00
|
|
|
|
|
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<T, E>(entity, true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Поиск записи с уникальными значениями
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
protected abstract T GetUniqueEntity(S model, DbContext context);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возможные дополнительные проверки при добавлении
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
protected abstract OperationResultModel AdditionalCheckingWhenAdding(DbContext context, S model);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возможные дополнительные проверки при удалении
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
2021-04-05 12:25:10 +04:00
|
|
|
|
protected abstract OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, T entity, G model);
|
2021-04-05 10:08:49 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление дополнительных фильтров
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="query"></param>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
protected abstract IQueryable<T> AdditionalCheckingWhenReadingList(IQueryable<T> query, G model);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возможные дополнительные проверки модели при изменении
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
protected abstract OperationResultModel AdditionalCheckingWhenUpdateing(DbContext context, S model);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Дополнительные удаления зависимых сущностей
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
2021-04-05 12:25:10 +04:00
|
|
|
|
protected abstract void AdditionalDeleting(DbContext context, T entity, G model);
|
2021-04-05 10:08:49 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Установка сортировок
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="query"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
protected abstract IQueryable<T> OrderingWhenReading(IQueryable<T> query);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление Include
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="query"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
protected abstract IQueryable<T> IncludingWhenReading(IQueryable<T> query);
|
2021-04-27 17:09:03 +04:00
|
|
|
|
|
2021-04-29 18:10:30 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Дополнительыне проверки, если требуется получать единичную запись но не по id
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
2021-04-27 17:09:03 +04:00
|
|
|
|
protected virtual bool AdditionalCheckForSingleGet(G model) => false;
|
|
|
|
|
|
2021-04-29 18:10:30 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение единичной записи но не по id
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="list"></param>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
2021-04-27 17:09:03 +04:00
|
|
|
|
protected virtual T GetSingleRecord(IQueryable<T> list, G model) => null;
|
2021-04-05 10:08:49 +04:00
|
|
|
|
}
|
|
|
|
|
}
|