DepartmentProject/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/AbstractGenerticEntityService/DisciplineService.cs

55 lines
2.6 KiB
C#

using CoreDatabase;
using CoreDatabase.Models.Department;
using DepartmentContract.BindingModels;
using DepartmentContract.Services.IGenericEntityService;
using DepartmentContract.ViewModels;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using ToolsModule.Enums;
using ToolsModule.Extensions;
using ToolsModule.Models;
namespace DepartmentDatabaseImplementation.Implementations.AbstractGenerticEntityService
{
/// <summary>
/// Реализация IDisciplineService
/// </summary>
public class DisciplineService :
AbstractGenerticEntityService<DisciplineGetBindingModel, DisciplineSetBindingModel, Discipline, DisciplineListViewModel, DisciplineViewModel>,
IDisciplineService
{
protected override OperationResultModel AdditionalCheckingWhenAdding(DbContext context, DisciplineSetBindingModel model) => OperationResultModel.Success(null);
protected override OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, Discipline entity, DisciplineGetBindingModel model)
{
if (context.Set<AcademicPlanRecord>().Any(x => x.DisciplineId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Есть записи учебного плана, относящиеся к этой дисциплине", ResultServiceStatusCode.ExsistItem);
}
return OperationResultModel.Success(null);
}
protected override IQueryable<Discipline> AdditionalCheckingWhenReadingList(IQueryable<Discipline> query, DisciplineGetBindingModel model)
{
if (model.DisciplineName.IsNotEmpty())
{
query = query.Where(x => x.DisciplineName.Contains(model.DisciplineName));
}
if (model.DisciplineBlockId.HasValue)
{
query = query.Where(x => x.DisciplineBlockId == model.DisciplineBlockId.Value);
}
return query;
}
protected override OperationResultModel AdditionalCheckingWhenUpdateing(DbContext context, DisciplineSetBindingModel model) => OperationResultModel.Success(null);
protected override void AdditionalDeleting(DbContext context, Discipline entity, DisciplineGetBindingModel model) { }
protected override Discipline GetUniqueEntity(DisciplineSetBindingModel model, DbContext context) => context.Set<Discipline>().FirstOrDefault(x => x.DisciplineName == model.DisciplineName && x.Id != model.Id);
protected override IQueryable<Discipline> IncludingWhenReading(IQueryable<Discipline> query) => query.Include(x => x.DisciplineBlock);
protected override IQueryable<Discipline> OrderingWhenReading(IQueryable<Discipline> query) => query.OrderBy(x => x.DisciplineBlock.DisciplineBlockOrder).ThenBy(x => x.DisciplineName);
}
}