using DatabaseCore;
using DatabaseCore.Models.Department;
using DepartmentBusinessLogic.BindingModels;
using DepartmentBusinessLogic.Interfaces;
using DepartmentBusinessLogic.ViewModels;
using Microsoft.EntityFrameworkCore;
using ModuleTools.Models;
using System;
using System.Linq;
namespace DepartmentDatabaseImplementation.Implementations
{
///
/// Реализация IAcademicPlanService
///
public class AcademicPlanService :
AbstractGenerticEntityService,
IAcademicPlanService
{
protected override OperationResultModel AdditionalCheckingWhenAdding(DbContext context, AcademicPlanSetBindingModel model) => OperationResultModel.Success(null);
protected override OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, AcademicPlan entity, AcademicPlanGetBindingModel model) => OperationResultModel.Success(null);
protected override IQueryable AdditionalCheckingWhenReadingList(IQueryable query, AcademicPlanGetBindingModel model)
{
if (model.EducationDirectionId.HasValue)
{
query = query.Where(x => x.EducationDirectionId == model.EducationDirectionId.Value);
}
return query;
}
protected override OperationResultModel AdditionalCheckingWhenUpdateing(DbContext context, AcademicPlanSetBindingModel model) => OperationResultModel.Success(null);
protected override void AdditionalDeleting(DbContext context, AcademicPlan entity, AcademicPlanGetBindingModel model)
{
var records = context.Set().Where(x => x.AcademicPlanId == model.Id);
foreach (var record in records)
{
var hours = context.Set().Where(x => x.AcademicPlanRecordId == record.Id);
foreach (var hour in hours)
{
hour.IsDeleted = true;
hour.DateDelete = DateTime.Now;
}
context.SaveChanges();
record.IsDeleted = true;
record.DateDelete = DateTime.Now;
}
context.SaveChanges();
}
protected override AcademicPlan GetUniqueEntity(AcademicPlanSetBindingModel model, DbContext context) => context.Set().FirstOrDefault(x => x.EducationDirectionId == model.EducationDirectionId && x.YearEntrance == model.YearEntrance && x.Id != model.Id);
protected override IQueryable IncludingWhenReading(IQueryable query) => query.Include(x => x.EducationDirection);
protected override IQueryable OrderingWhenReading(IQueryable query) => query.OrderBy(x => x.EducationDirection.Cipher).ThenBy(x => x.YearEntrance);
}
}