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
{
///
/// Реализация IEnrollmentYearService
///
public class EnrollmentYearService :
AbstractGenerticEntityService,
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 AdditionalCheckingWhenReadingList(IQueryable query, EnrollmentYearGetBindingModel model)
{
if (model.AcademicPlanId.HasValue)
{
query = query.Where(x => x.AcademicPlanId == model.AcademicPlanId.Value);
}
if (model.EducationDirectionId.HasValue)
{
query = query.Where(x => x.AcademicPlan.EducationDirectionId == model.EducationDirectionId.Value);
}
if (model.Year.HasValue)
{
query = query.Where(x => x.YearEntrance == model.Year.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().Where(x => x.EnrollmentYearId == model.Id);
foreach (var record in records)
{
record.EnrollmentYearId = null;
}
context.SaveChanges();
}
protected override EnrollmentYear GetUniqueEntity(EnrollmentYearSetBindingModel model, IQueryable query) => query.FirstOrDefault(x => x.AcademicPlanId == model.AcademicPlanId && x.YearEntrance == model.YearEntrance && x.Id != model.Id);
protected override IQueryable IncludingWhenReading(IQueryable query) => query.Include(x => x.AcademicPlan).Include(x => x.AcademicPlan.EducationDirection);
protected override IQueryable OrderingWhenReading(IQueryable query) => query.OrderBy(x => x.AcademicPlan.EducationDirection.Cipher).ThenBy(x => x.YearEntrance);
protected override bool AdditionalCheckForSingleGet(EnrollmentYearGetBindingModel model)
{
if (model.EducationDirectionId.HasValue && model.Year.HasValue)
{
return true;
}
return base.AdditionalCheckForSingleGet(model);
}
protected override EnrollmentYear GetSingleRecord(IQueryable list, EnrollmentYearGetBindingModel model)
{
if (model.EducationDirectionId.HasValue && model.Year.HasValue)
{
return list.FirstOrDefault(x => x.AcademicPlan.EducationDirectionId == model.EducationDirectionId && x.YearEntrance == model.Year);
}
return base.GetSingleRecord(list, model);
}
}
}