53 lines
2.2 KiB
C#
53 lines
2.2 KiB
C#
using DatabaseCore;
|
||
using DatabaseCore.Models.Security;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using ModuleTools.Enums;
|
||
using ModuleTools.Models;
|
||
using SecurityBusinessLogic.BindingModels;
|
||
using SecurityBusinessLogic.Interfaces;
|
||
using SecurityBusinessLogic.ViewModels;
|
||
using System;
|
||
using System.Linq;
|
||
|
||
namespace SecurityDatabaseImplementation.Implementations
|
||
{
|
||
/// <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, DbContext context) => context.Set<Role>().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);
|
||
}
|
||
} |