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

67 lines
3.2 KiB
C#
Raw Permalink Normal View History

2022-03-18 22:55:48 +04:00
using CoreDatabase;
using CoreDatabase.Models.Department;
using DepartmentContract.BindingModels;
using DepartmentContract.Services.IGenericEntityService;
using DepartmentContract.ViewModels;
2021-04-12 10:01:42 +04:00
using Microsoft.EntityFrameworkCore;
2021-04-13 12:52:45 +04:00
using System;
2021-04-12 10:01:42 +04:00
using System.Linq;
2022-03-20 10:10:44 +04:00
using ToolsModule.ManagmentEntity;
2021-04-12 10:01:42 +04:00
namespace DepartmentDatabaseImplementation.Implementations.AbstractGenerticEntityService
2021-04-12 10:01:42 +04:00
{
/// <summary>
/// Реализация IStudentGroupService
/// </summary>
public class StudentGroupService :
2021-04-12 10:01:42 +04:00
AbstractGenerticEntityService<StudentGroupGetBindingModel, StudentGroupSetBindingModel, StudentGroup, StudentGroupListViewModel, StudentGroupViewModel>,
IStudentGroupService
{
protected override OperationResultModel AdditionalCheckingWhenAdding(DbContext context, StudentGroupSetBindingModel model) => OperationResultModel.Success(null);
protected override OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, StudentGroup entity, StudentGroupGetBindingModel model)
{
2021-04-12 16:57:29 +04:00
if (context.Set<Student>().Any(x => x.StudentGroupId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Есть студенты, относящиеся к этой группе", ResultServiceStatusCode.ExsistItem);
}
2021-04-12 10:01:42 +04:00
return OperationResultModel.Success(null);
}
protected override IQueryable<StudentGroup> AdditionalCheckingWhenReadingList(IQueryable<StudentGroup> query, StudentGroupGetBindingModel model)
{
2021-04-27 18:04:01 +04:00
if (model.EducationDirectionId.HasValue)
2021-04-12 10:01:42 +04:00
{
2022-12-16 17:43:23 +04:00
query = query.Where(x => x.AcademicPlan.EducationDirectionId == model.EducationDirectionId.Value);
}
if (model.AcademicPlanId.HasValue)
{
query = query.Where(x => x.AcademicPlanId == model.AcademicPlanId.Value);
2021-04-12 10:01:42 +04:00
}
if (model.LecturerId.HasValue)
{
query = query.Where(x => x.LecturerId == model.LecturerId.Value);
}
return query;
}
protected override OperationResultModel AdditionalCheckingWhenUpdateing(DbContext context, StudentGroupSetBindingModel model) => OperationResultModel.Success(null);
2021-04-13 12:52:45 +04:00
protected override void AdditionalDeleting(DbContext context, StudentGroup entity, StudentGroupGetBindingModel model)
{
var orders = context.Set<OrderStudentRecord>().Where(x => x.StudentGroupFromId == model.Id || x.StudentGroupToId == model.Id);
foreach (var o in orders)
{
o.IsDeleted = true;
o.DateDelete = DateTime.Now;
}
context.SaveChanges();
}
2021-04-12 10:01:42 +04:00
2022-12-16 17:43:23 +04:00
protected override StudentGroup GetUniqueEntity(StudentGroupSetBindingModel model, IQueryable<StudentGroup> query) => query.FirstOrDefault(x => x.AcademicPlanId == model.AcademicPlanId && x.YearEntrance == model.YearEntrance && x.GroupNumber == model.GroupNumber && x.Id != model.Id);
2021-04-12 10:01:42 +04:00
2022-12-16 17:43:23 +04:00
protected override IQueryable<StudentGroup> IncludingWhenReading(IQueryable<StudentGroup> query) => query.Include(x => x.AcademicPlan).Include(x => x.AcademicPlan.EducationDirection).Include(x => x.Lecturer).Include(x => x.Students);
2021-04-12 10:01:42 +04:00
2022-12-16 17:43:23 +04:00
protected override IQueryable<StudentGroup> OrderingWhenReading(IQueryable<StudentGroup> query) => query.OrderBy(x => x.AcademicPlan.EducationDirection.Qualification).ThenBy(x => x.AcademicPlan.EducationDirection.Cipher).ThenBy(x => x.YearEntrance).ThenBy(x => x.GroupNumber);
2021-04-12 10:01:42 +04:00
}
}