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 : IGenerticEntityService where G : GetBindingModel where S : SetBindingModel where T : BaseEntity where L : ListViewModel, 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(model, true); context.Set().Add(entity); context.SaveChanges(); return OperationResultModel.Success(Mapper.MapToClass(entity, true)); } else { if (exsistEntity.IsDeleted) { exsistEntity = Mapper.MapToClass(model, exsistEntity, true); exsistEntity.IsDeleted = false; context.SaveChanges(); return OperationResultModel.Success(Mapper.MapToClass(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().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) { var entity = IncludingWhenReading(context.Set().AsQueryable()).FirstOrDefault(x => x.Id == model.Id.Value); if (entity == null) { return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); } return OperationResultModel.Success(Mapper.MapToClass(entity, model.HaveRight)); } var query = context.Set().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(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().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(entity, true)); } /// /// Поиск записи с уникальными значениями /// /// /// protected abstract T GetUniqueEntity(S model, DbContext context); /// /// Возможные дополнительные проверки при добавлении /// /// /// /// protected abstract OperationResultModel AdditionalCheckingWhenAdding(DbContext context, S model); /// /// Возможные дополнительные проверки при удалении /// /// /// /// protected abstract OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, T entity, G model); /// /// Добавление дополнительных фильтров /// /// /// /// protected abstract IQueryable AdditionalCheckingWhenReadingList(IQueryable query, G model); /// /// Возможные дополнительные проверки модели при изменении /// /// protected abstract OperationResultModel AdditionalCheckingWhenUpdateing(DbContext context, S model); /// /// Дополнительные удаления зависимых сущностей /// /// protected abstract void AdditionalDeleting(DbContext context, T entity, G model); /// /// Установка сортировок /// /// /// protected abstract IQueryable OrderingWhenReading(IQueryable query); /// /// Добавление Include /// /// /// protected abstract IQueryable IncludingWhenReading(IQueryable query); } }