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

47 lines
2.2 KiB
C#
Raw Normal View History

using DatabaseCore;
using DatabaseCore.Models.Department;
using DepartmentBusinessLogic.BindingModels;
using DepartmentBusinessLogic.Interfaces;
using DepartmentBusinessLogic.ViewModels;
using Microsoft.EntityFrameworkCore;
using ModuleTools.Enums;
using ModuleTools.Models;
using System.Linq;
namespace DepartmentDatabaseImplementation.Implementations
{
/// <summary>
/// Реализация IEmployeePostService
/// </summary>
public class PostService :
AbstractGenerticEntityService<PostGetBindingModel, PostSetBindingModel, Post, PostListViewModel, PostViewModel>,
IPostService
{
protected override OperationResultModel AdditionalCheckingWhenAdding(DbContext context, PostSetBindingModel model) => OperationResultModel.Success(null);
protected override OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, Post entity, PostGetBindingModel model)
{
if (context.Set<EmployeePost>().Any(x => x.PostId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Существуют сотрудники с этой должностью", ResultServiceStatusCode.ExsistItem);
}
if (context.Set<LecturerPost>().Any(x => x.PostId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Существуют преподаватели с этой должностью", ResultServiceStatusCode.ExsistItem);
}
return OperationResultModel.Success(null);
}
protected override IQueryable<Post> AdditionalCheckingWhenReadingList(IQueryable<Post> query, PostGetBindingModel model) => query;
protected override OperationResultModel AdditionalCheckingWhenUpdateing(DbContext context, PostSetBindingModel model) => OperationResultModel.Success(null);
protected override void AdditionalDeleting(DbContext context, Post entity, PostGetBindingModel model) { }
protected override Post GetUniqueEntity(PostSetBindingModel model, DbContext context) => context.Set<Post>().FirstOrDefault(x => x.PostName == model.PostName && x.Id != model.Id);
protected override IQueryable<Post> IncludingWhenReading(IQueryable<Post> query) => query;
protected override IQueryable<Post> OrderingWhenReading(IQueryable<Post> query) => query.OrderBy(x => x.Order);
}
}