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

57 lines
2.6 KiB
C#
Raw Normal View History

2022-03-27 14:51:27 +04:00
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>
/// Реализация интерфейса IBasicDepartmentService
/// </summary>
public class BasicDepartmentService :
AbstractGenerticEntityService<BasicDepartmentGetBindingModel, BasicDepartmentSetBindingModel, BasicDepartment, BasicDepartmentListViewModel, BasicDepartmentViewModel>,
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<BasicDepartment> AdditionalCheckingWhenReadingList(IQueryable<BasicDepartment> 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<Student>().Where(x => x.BasicDepartmentId == model.Id);
foreach (var record in studentrecords)
{
record.BasicDepartmentId = null;
}
context.SaveChanges();
var disciplinerecords = context.Set<Discipline>().Where(x => x.BasicDepartmentId == model.Id);
foreach (var record in disciplinerecords)
{
record.BasicDepartmentId = null;
}
context.SaveChanges();
}
protected override BasicDepartment GetUniqueEntity(BasicDepartmentSetBindingModel model, IQueryable<BasicDepartment> query) => query.FirstOrDefault(x => x.BasicDepartmentName == model.BasicDepartmentName && x.Id != model.Id);
2022-03-27 14:51:27 +04:00
protected override IQueryable<BasicDepartment> IncludingWhenReading(IQueryable<BasicDepartment> query) => query.Include(x => x.Lecturer);
protected override IQueryable<BasicDepartment> OrderingWhenReading(IQueryable<BasicDepartment> query) => query.OrderBy(x => x.BasicDepartmentName);
}
}