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
{
///
/// Реализация интерфейса IBasicDepartmentService
///
public class BasicDepartmentService :
AbstractGenerticEntityService,
IBasicDepartmentService
{
protected override OperationResultModel AdditionalCheckingWhenAdding(DbContext context, BasicDepartmentSetBindingModel model) => OperationResultModel.Success(null);
protected override OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, BasicDepartment entity, BasicDepartmentGetBindingModel model) => OperationResultModel.Success(null);
protected override IQueryable AdditionalCheckingWhenReadingList(IQueryable query, BasicDepartmentGetBindingModel model)
{
if (model.LecturerId.HasValue)
{
query = query.Where(x => x.LecturerId == model.LecturerId.Value);
}
return query;
}
protected override OperationResultModel AdditionalCheckingWhenUpdateing(DbContext context, BasicDepartmentSetBindingModel model) => OperationResultModel.Success(null);
protected override void AdditionalDeleting(DbContext context, BasicDepartment entity, BasicDepartmentGetBindingModel model)
{
var studentrecords = context.Set().Where(x => x.BasicDepartmentId == model.Id);
foreach (var record in studentrecords)
{
record.BasicDepartmentId = null;
}
context.SaveChanges();
var disciplinerecords = context.Set().Where(x => x.BasicDepartmentId == model.Id);
foreach (var record in disciplinerecords)
{
record.BasicDepartmentId = null;
}
context.SaveChanges();
}
protected override BasicDepartment GetUniqueEntity(BasicDepartmentSetBindingModel model, IQueryable query) => query.FirstOrDefault(x => x.BasicDepartmentName == model.BasicDepartmentName && x.Id != model.Id);
protected override IQueryable IncludingWhenReading(IQueryable query) => query.Include(x => x.Lecturer);
protected override IQueryable OrderingWhenReading(IQueryable query) => query.OrderBy(x => x.BasicDepartmentName);
}
}