DepartmentProject/DepartmentPortal/Common/DatabaseCore/AbstractGenerticEntityService.cs

225 lines
6.8 KiB
C#
Raw Normal View History

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;
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
{
public OperationResultModel Create(S model)
{
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);
context.Set<T>().Add(entity);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<T, E>(entity, true));
}
else
{
if (exsistEntity.IsDeleted)
{
exsistEntity = Mapper.MapToClass(model, exsistEntity, true);
exsistEntity.IsDeleted = false;
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<T, E>(exsistEntity, true));
}
else
{
return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem);
}
}
}
public OperationResultModel Delete(G model)
{
using var context = DatabaseManager.GetContext;
using var transaction = context.Database.BeginTransaction();
try
{
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);
}
var result = AdditionalCheckingWhenDeleting(context, entity, model);
if (!result.IsSucceeded)
{
return result;
}
entity.IsDeleted = true;
entity.DateDelete = DateTime.Now;
context.SaveChanges();
AdditionalDeleting(context, entity, model);
transaction.Commit();
}
catch (Exception)
{
transaction.Rollback();
throw;
}
return OperationResultModel.Success(true);
}
public OperationResultModel Read(G model)
{
int countPages = 0;
using var context = DatabaseManager.GetContext;
// для одной записи
if (model.Id.HasValue)
{
2021-04-12 16:57:29 +04:00
var entity = IncludingWhenReading(context.Set<T>().AsQueryable()).FirstOrDefault(x => x.Id == model.Id.Value);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
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);
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);
}
query = IncludingWhenReading(query);
var result = new L
{
MaxCount = countPages,
List = query.Select(x => Mapper.MapToClass<T, E>(x, model.HaveRight)).ToList()
};
return OperationResultModel.Success(result);
}
public OperationResultModel Update(S model)
{
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);
context.SaveChanges();
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>
protected abstract OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, T entity, G model);
/// <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>
protected abstract void AdditionalDeleting(DbContext context, T entity, G model);
/// <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);
}
}