DepartmentProject/DepartmentPortal/Security/SecurityDatabaseImplementation/Implementations/AbstractGenerticEntityService/RoleService.cs

71 lines
2.7 KiB
C#
Raw Normal View History

2022-03-18 22:55:48 +04:00
using CoreDatabase;
using CoreDatabase.Models.Security;
using Microsoft.EntityFrameworkCore;
using SecurityContract.BindingModels;
using SecurityContract.Services.IGenericEntityService;
using SecurityContract.ViewModels;
using System;
using System.Linq;
2022-03-20 10:10:44 +04:00
using ToolsModule.ManagmentEntity;
using ToolsModule.ManagmentExtension;
namespace SecurityDatabaseImplementation.Implementations.AbstractGenerticEntityService
{
/// <summary>
/// Реализация IRoleService
/// </summary>
public class RoleService :
AbstractGenerticEntityService<RoleGetBindingModel, RoleSetBindingModel, Role, RoleListViewModel, RoleViewModel>,
IRoleService
{
protected override OperationResultModel AdditionalCheckingWhenAdding(DbContext context, RoleSetBindingModel model) => OperationResultModel.Success(null);
protected override OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, Role entity, RoleGetBindingModel model)
{
if (context.Set<UserRole>().Any(x => x.RoleId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Существуют пользователи, у которых есть эта роль", ResultServiceStatusCode.ExsistItem);
}
return OperationResultModel.Success(null);
}
protected override IQueryable<Role> AdditionalCheckingWhenReadingList(IQueryable<Role> query, RoleGetBindingModel model) => query;
protected override OperationResultModel AdditionalCheckingWhenUpdateing(DbContext context, RoleSetBindingModel model) => OperationResultModel.Success(null);
protected override void AdditionalDeleting(DbContext context, Role entity, RoleGetBindingModel model)
{
var access = context.Set<Access>().Where(x => x.RoleId == model.Id);
foreach (var ac in access)
{
ac.IsDeleted = true;
ac.DateDelete = DateTime.Now;
}
context.SaveChanges();
}
protected override Role GetUniqueEntity(RoleSetBindingModel model, IQueryable<Role> query) => query.FirstOrDefault(x => x.RoleName == model.RoleName && x.Id != model.Id);
protected override IQueryable<Role> IncludingWhenReading(IQueryable<Role> query) => query;
protected override IQueryable<Role> OrderingWhenReading(IQueryable<Role> query) => query.OrderBy(x => x.RoleName);
protected override bool AdditionalCheckForSingleGet(RoleGetBindingModel model)
{
if (model.RoleName.IsNotEmpty())
{
return true;
}
return base.AdditionalCheckForSingleGet(model);
}
protected override Role GetSingleRecord(IQueryable<Role> list, RoleGetBindingModel model)
{
if (model.RoleName.IsNotEmpty())
{
return list.FirstOrDefault(x => x.RoleName == model.RoleName);
}
return base.GetSingleRecord(list, model);
}
}
}