using CoreDatabase;
using CoreDatabase.Models.Department;
using DepartmentContract.BindingModels;
using DepartmentContract.Services.IGenericEntityService;
using DepartmentContract.ViewModels;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using ToolsModule.Enums;
using ToolsModule.Extensions;
using ToolsModule.Models;
namespace DepartmentDatabaseImplementation.Implementations.AbstractGenerticEntityService
{
///
/// Реализация IDisciplineService
///
public class DisciplineService :
AbstractGenerticEntityService,
IDisciplineService
{
protected override OperationResultModel AdditionalCheckingWhenAdding(DbContext context, DisciplineSetBindingModel model) => OperationResultModel.Success(null);
protected override OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, Discipline entity, DisciplineGetBindingModel model)
{
if (context.Set().Any(x => x.DisciplineId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Есть записи учебного плана, относящиеся к этой дисциплине", ResultServiceStatusCode.ExsistItem);
}
return OperationResultModel.Success(null);
}
protected override IQueryable AdditionalCheckingWhenReadingList(IQueryable query, DisciplineGetBindingModel model)
{
if (model.DisciplineName.IsNotEmpty())
{
query = query.Where(x => x.DisciplineName.Contains(model.DisciplineName));
}
if (model.DisciplineBlockId.HasValue)
{
query = query.Where(x => x.DisciplineBlockId == model.DisciplineBlockId.Value);
}
return query;
}
protected override OperationResultModel AdditionalCheckingWhenUpdateing(DbContext context, DisciplineSetBindingModel model) => OperationResultModel.Success(null);
protected override void AdditionalDeleting(DbContext context, Discipline entity, DisciplineGetBindingModel model) { }
protected override Discipline GetUniqueEntity(DisciplineSetBindingModel model, DbContext context) => context.Set().FirstOrDefault(x => x.DisciplineName == model.DisciplineName && x.Id != model.Id);
protected override IQueryable IncludingWhenReading(IQueryable query) => query.Include(x => x.DisciplineBlock);
protected override IQueryable OrderingWhenReading(IQueryable query) => query.OrderBy(x => x.DisciplineBlock.DisciplineBlockOrder).ThenBy(x => x.DisciplineName);
}
}