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 { /// /// Реализация IStudentGroupService /// public class StudentGroupService : AbstractGenerticEntityService, IStudentGroupService { protected override OperationResultModel AdditionalCheckingWhenAdding(DbContext context, StudentGroupSetBindingModel model) => OperationResultModel.Success(null); protected override OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, StudentGroup entity, StudentGroupGetBindingModel model) { if (context.Set().Any(x => x.StudentGroupId == model.Id && !x.IsDeleted)) { return OperationResultModel.Error("Error:", "Есть студенты, относящиеся к этой группе", ResultServiceStatusCode.ExsistItem); } return OperationResultModel.Success(null); } protected override IQueryable AdditionalCheckingWhenReadingList(IQueryable query, StudentGroupGetBindingModel model) { if (model.EducationDirectionId.HasValue) { query = query.Where(x => x.AcademicPlan.EducationDirectionId == model.EducationDirectionId.Value); } if (model.AcademicPlanId.HasValue) { query = query.Where(x => x.AcademicPlanId == model.AcademicPlanId.Value); } 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); protected override void AdditionalDeleting(DbContext context, StudentGroup entity, StudentGroupGetBindingModel model) { var orders = context.Set().Where(x => x.StudentGroupFromId == model.Id || x.StudentGroupToId == model.Id); foreach (var o in orders) { o.IsDeleted = true; o.DateDelete = DateTime.Now; } context.SaveChanges(); } protected override StudentGroup GetUniqueEntity(StudentGroupSetBindingModel model, IQueryable query) => query.FirstOrDefault(x => x.AcademicPlanId == model.AcademicPlanId && x.YearEntrance == model.YearEntrance && x.GroupNumber == model.GroupNumber && x.Id != model.Id); protected override IQueryable IncludingWhenReading(IQueryable query) => query.Include(x => x.AcademicPlan).Include(x => x.AcademicPlan.EducationDirection).Include(x => x.Lecturer).Include(x => x.Students); protected override IQueryable OrderingWhenReading(IQueryable query) => query.OrderBy(x => x.AcademicPlan.EducationDirection.Qualification).ThenBy(x => x.AcademicPlan.EducationDirection.Cipher).ThenBy(x => x.YearEntrance).ThenBy(x => x.GroupNumber); } }