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;
using ToolsModule.ManagmentExtension;
namespace DepartmentDatabaseImplementation.Implementations.AbstractGenerticEntityService
{
///
/// Реализация IStudentService
///
public class StudentService :
AbstractGenerticEntityService,
IStudentService
{
protected override OperationResultModel AdditionalCheckingWhenAdding(DbContext context, StudentSetBindingModel model) => OperationResultModel.Success(null);
protected override OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, Student entity, StudentGetBindingModel model) => OperationResultModel.Success(null);
protected override IQueryable AdditionalCheckingWhenReadingList(IQueryable query, StudentGetBindingModel model)
{
if (model.UserId.HasValue)
{
query = query.Where(x => x.UserId == model.UserId.Value);
}
if (model.StudentGroupId.HasValue)
{
query = query.Where(x => x.StudentGroupId == model.StudentGroupId.Value);
}
if (model.StudentState.HasValue)
{
query = query.Where(x => x.StudentState == model.StudentState.Value);
}
if (model.BasicDepartmentId.HasValue)
{
query = query.Where(x => x.BasicDepartmentId == model.BasicDepartmentId.Value);
}
return query;
}
protected override OperationResultModel AdditionalCheckingWhenUpdateing(DbContext context, StudentSetBindingModel model) => OperationResultModel.Success(null);
protected override void AdditionalDeleting(DbContext context, Student entity, StudentGetBindingModel model)
{
var orders = context.Set().Where(x => x.StudentId == model.Id);
foreach (var o in orders)
{
o.IsDeleted = true;
o.DateDelete = DateTime.Now;
}
context.SaveChanges();
}
protected override Student GetUniqueEntity(StudentSetBindingModel model, IQueryable query) => query.FirstOrDefault(x => x.NumberOfBook == model.NumberOfBook && x.Id != model.Id);
protected override IQueryable IncludingWhenReading(IQueryable query) => query.Include(x => x.StudentGroup).Include(x => x.User);
protected override IQueryable OrderingWhenReading(IQueryable query) => query.OrderBy(x => x.StudentGroup.YearEntrance).ThenBy(x => x.StudentGroup.GroupNumber).ThenBy(x => x.LastName).ThenBy(x => x.FirstName);
protected override bool AdditionalCheckForSingleGet(StudentGetBindingModel model)
{
if (model.NumberOfBook.IsNotEmpty())
{
return true;
}
return base.AdditionalCheckForSingleGet(model);
}
protected override Student GetSingleRecord(IQueryable list, StudentGetBindingModel model)
{
if (model.NumberOfBook.IsNotEmpty())
{
return list.FirstOrDefault(x => x.NumberOfBook == model.NumberOfBook);
}
return base.GetSingleRecord(list, model);
}
}
}