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

64 lines
2.9 KiB
C#
Raw Normal View History

2021-04-12 10:01:42 +04:00
using DatabaseCore;
using DatabaseCore.Models.Department;
using DepartmentBusinessLogic.BindingModels;
using DepartmentBusinessLogic.Interfaces;
using DepartmentBusinessLogic.ViewModels;
using Microsoft.EntityFrameworkCore;
2021-04-12 16:57:29 +04:00
using ModuleTools.Enums;
2021-04-12 10:01:42 +04:00
using ModuleTools.Models;
2021-04-13 12:52:45 +04:00
using System;
2021-04-12 10:01:42 +04:00
using System.Linq;
namespace DepartmentDatabaseImplementation.Implementations
{
/// <summary>
/// Реализация IStudentGroupService
/// </summary>
public class StudentGroupService :
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
{
2021-04-27 18:04:01 +04:00
query = query.Where(x => x.EducationDirectionId == model.EducationDirectionId.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
2021-04-27 18:04:01 +04:00
protected override StudentGroup GetUniqueEntity(StudentGroupSetBindingModel model, DbContext context) => context.Set<StudentGroup>().FirstOrDefault(x => x.EducationDirectionId == model.EducationDirectionId && x.AcademicCourse == (int)model.AcademicCourse && x.GroupNumber == model.GroupNumber && x.Id != model.Id);
2021-04-12 10:01:42 +04:00
2021-04-27 18:04:01 +04:00
protected override IQueryable<StudentGroup> IncludingWhenReading(IQueryable<StudentGroup> query) => query.Include(x => x.EducationDirection).Include(x => x.Lecturer);
2021-04-12 10:01:42 +04:00
2021-04-27 18:04:01 +04:00
protected override IQueryable<StudentGroup> OrderingWhenReading(IQueryable<StudentGroup> query) => query.OrderBy(x => x.EducationDirection.Cipher).ThenBy(x => x.AcademicCourse).ThenBy(x => x.GroupNumber);
2021-04-12 10:01:42 +04:00
}
}