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

93 lines
3.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using CoreDatabase;
using CoreDatabase.Models.Department;
using DepartmentContract.BindingModels;
using DepartmentContract.Services.IGenericEntityService;
using DepartmentContract.ViewModels;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using ToolsModule.ManagmentEntity;
namespace DepartmentDatabaseImplementation.Implementations.AbstractGenerticEntityService
{
/// <summary>
/// Реализация ILecturerService
/// </summary>
public class LecturerService :
AbstractGenerticEntityService<LecturerGetBindingModel, LecturerSetBindingModel, Lecturer, LecturerListViewModel, LecturerViewModel>,
ILecturerService
{
protected override OperationResultModel AdditionalCheckingWhenAdding(DbContext context, LecturerSetBindingModel model) => OperationResultModel.Success(null);
protected override OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, Lecturer entity, LecturerGetBindingModel model)
{
if (entity.User == null)
{
return OperationResultModel.Error("Error:", "Не найден пользователь от сотрудника", ResultServiceStatusCode.NotFound);
}
if (context.Set<EducationDirection>().Any(x => x.LecturerId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Есть направления обучения, у которых этот преподаватель указан руководителем", ResultServiceStatusCode.ExsistItem);
}
if (context.Set<StudentGroup>().Any(x => x.LecturerId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Есть учебные группы, у которых этот преподаватель числится как куратор", ResultServiceStatusCode.ExsistItem);
}
return OperationResultModel.Success(null); ;
}
protected override IQueryable<Lecturer> AdditionalCheckingWhenReadingList(IQueryable<Lecturer> query, LecturerGetBindingModel model)
{
if (model.UserId.HasValue)
{
query = query.Where(x => x.UserId == model.UserId);
}
if (model.LecturerAcademicDegreeId.HasValue)
{
query = query.Where(x => x.LecturerAcademicDegreeId == model.LecturerAcademicDegreeId);
}
if (model.LecturerAcademicRankId.HasValue)
{
query = query.Where(x => x.LecturerAcademicRankId == model.LecturerAcademicRankId);
}
return query;
}
protected override OperationResultModel AdditionalCheckingWhenUpdateing(DbContext context, LecturerSetBindingModel model) => OperationResultModel.Success(null);
protected override void AdditionalDeleting(DbContext context, Lecturer entity, LecturerGetBindingModel model)
{
entity.User.IsDeleted = true;
entity.User.DateDelete = DateTime.Now;
context.SaveChanges();
var links = context.Set<LecturerPost>().Where(x => x.LecturerId == model.Id);
foreach (var link in links)
{
link.IsDeleted = true;
link.DateDelete = DateTime.Now;
}
context.SaveChanges();
}
protected override Lecturer GetUniqueEntity(LecturerSetBindingModel model, IQueryable<Lecturer> query) => query.FirstOrDefault(x => x.LastName == model.LastName && x.FirstName == model.FirstName &&
x.Patronymic == model.Patronymic && x.Id != model.Id);
protected override IQueryable<Lecturer> IncludingWhenReading(IQueryable<Lecturer> query)
{
return query
.Include(x => x.LecturerAcademicRank)
.Include(x => x.LecturerAcademicDegree)
.Include(x => x.LecturerPosts).Include("LecturerPosts.Post");
}
protected override IQueryable<Lecturer> OrderingWhenReading(IQueryable<Lecturer> query) => query
.OrderByDescending(x => x.LecturerPosts.Max(y => y.Post.Order))
.ThenBy(x => x.LecturerAcademicRank.Order)
.ThenBy(x => x.LecturerAcademicDegree.Order)
.ThenBy(x => x.LastName)
.ThenBy(x => x.FirstName);
}
}