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

50 lines
2.5 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.ManagmentEntity;
namespace DepartmentDatabaseImplementation.Implementations.AbstractGenerticEntityService
{
/// <summary>
/// Реализация IEnrollmentYearService
/// </summary>
public class EnrollmentYearService :
AbstractGenerticEntityService<EnrollmentYearGetBindingModel, EnrollmentYearSetBindingModel, EnrollmentYear, EnrollmentYearListViewModel, EnrollmentYearViewModel>,
IEnrollmentYearService
{
protected override OperationResultModel AdditionalCheckingWhenAdding(DbContext context, EnrollmentYearSetBindingModel model) => OperationResultModel.Success(null);
protected override OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, EnrollmentYear entity, EnrollmentYearGetBindingModel model) => OperationResultModel.Success(null);
protected override IQueryable<EnrollmentYear> AdditionalCheckingWhenReadingList(IQueryable<EnrollmentYear> query, EnrollmentYearGetBindingModel model)
{
if (model.AcademicPlanId.HasValue)
{
query = query.Where(x => x.AcademicPlanId == model.AcademicPlanId.Value);
}
return query;
}
protected override OperationResultModel AdditionalCheckingWhenUpdateing(DbContext context, EnrollmentYearSetBindingModel model) => OperationResultModel.Success(null);
protected override void AdditionalDeleting(DbContext context, EnrollmentYear entity, EnrollmentYearGetBindingModel model)
{
var records = context.Set<Student>().Where(x => x.EnrollmentYearId == model.Id);
foreach (var record in records)
{
record.EnrollmentYearId = null;
}
context.SaveChanges();
}
protected override EnrollmentYear GetUniqueEntity(EnrollmentYearSetBindingModel model, DbContext context) => context.Set<EnrollmentYear>().FirstOrDefault(x => x.AcademicPlanId == model.AcademicPlanId && x.YearEntrance == model.YearEntrance && x.Id != model.Id);
protected override IQueryable<EnrollmentYear> IncludingWhenReading(IQueryable<EnrollmentYear> query) => query.Include(x => x.AcademicPlan).Include(x => x.AcademicPlan.EducationDirection);
protected override IQueryable<EnrollmentYear> OrderingWhenReading(IQueryable<EnrollmentYear> query) => query.OrderBy(x => x.AcademicPlan.EducationDirection.Cipher).ThenBy(x => x.YearEntrance);
}
}