using DatabaseCore;
using DatabaseCore.Models.Department;
using DepartmentBusinessLogic.BindingModels;
using DepartmentBusinessLogic.Interfaces;
using DepartmentBusinessLogic.ViewModels;
using Microsoft.EntityFrameworkCore;
using ModuleTools.Models;
using System.Linq;
namespace DepartmentDatabaseImplementation.Implementations
{
///
/// Реализация IOrderStudentRecordService
///
public class OrderStudentRecordService :
AbstractGenerticEntityService,
IOrderStudentRecordService
{
protected override OperationResultModel AdditionalCheckingWhenAdding(DbContext context, OrderStudentRecordSetBindingModel model) => OperationResultModel.Success(null);
protected override OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, OrderStudentRecord entity, OrderStudentRecordGetBindingModel model) => OperationResultModel.Success(null);
protected override IQueryable AdditionalCheckingWhenReadingList(IQueryable query, OrderStudentRecordGetBindingModel model)
{
if (model.OrderId.HasValue)
{
query = query.Where(x => x.OrderId == model.OrderId.Value);
}
if (model.StudentId.HasValue)
{
query = query.Where(x => x.StudentId == model.StudentId.Value);
}
if (model.StudentGroupId.HasValue)
{
query = query.Where(x => x.StudentGroupFromId == model.StudentGroupId.Value || x.StudentGroupToId == model.StudentGroupId.Value);
}
return query;
}
protected override OperationResultModel AdditionalCheckingWhenUpdateing(DbContext context, OrderStudentRecordSetBindingModel model) => OperationResultModel.Success(null);
protected override void AdditionalDeleting(DbContext context, OrderStudentRecord entity, OrderStudentRecordGetBindingModel model) { }
protected override OrderStudentRecord GetUniqueEntity(OrderStudentRecordSetBindingModel model, DbContext context) => context.Set().FirstOrDefault(x => x.StudentId == model.StudentId && x.OrderId == model.OrderId && x.Id != model.Id);
protected override IQueryable IncludingWhenReading(IQueryable query) => query.Include(x => x.Order).Include(x => x.Student).Include(x => x.StudentGroupFrom).Include(x => x.StudentGroupTo);
protected override IQueryable OrderingWhenReading(IQueryable query) => query.OrderBy(x => x.Order.OrderDate).ThenBy(x => x.Order.OrderNumber).ThenBy(x => x.OrderStudentMoveType).ThenBy(x => x.Student.LastName).ThenBy(x => x.Student.FirstName);
protected override bool AdditionalCheckForSingleGet(OrderStudentRecordGetBindingModel model)
{
if (model.OrderId.HasValue && model.StudentId.HasValue && model.OrderStudentMoveType.HasValue)
{
return true;
}
return base.AdditionalCheckForSingleGet(model);
}
protected override OrderStudentRecord GetSingleRecord(IQueryable list, OrderStudentRecordGetBindingModel model)
{
if (model.OrderId.HasValue && model.StudentId.HasValue && model.OrderStudentMoveType.HasValue)
{
return list.FirstOrDefault(x => x.OrderId == model.OrderId && x.StudentId == model.StudentId && x.OrderStudentMoveType == (int)model.OrderStudentMoveType);
}
return base.GetSingleRecord(list, model);
}
}
}