2021-03-26 20:09:52 +04:00
|
|
|
|
using DatabaseCore;
|
|
|
|
|
using DatabaseCore.Models.Security;
|
2021-04-05 10:08:49 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-03-18 22:38:52 +04:00
|
|
|
|
using ToolsModule.Enums;
|
|
|
|
|
using ToolsModule.Extensions;
|
|
|
|
|
using ToolsModule.Models;
|
2021-03-26 20:09:52 +04:00
|
|
|
|
using SecurityBusinessLogic.BindingModels;
|
|
|
|
|
using SecurityBusinessLogic.Interfaces;
|
|
|
|
|
using SecurityBusinessLogic.ViewModels;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2021-04-01 21:30:29 +04:00
|
|
|
|
namespace SecurityDatabaseImplementation.Implementations
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Реализация IRoleService
|
|
|
|
|
/// </summary>
|
2021-04-05 10:08:49 +04:00
|
|
|
|
public class RoleService :
|
|
|
|
|
AbstractGenerticEntityService<RoleGetBindingModel, RoleSetBindingModel, Role, RoleListViewModel, RoleViewModel>,
|
|
|
|
|
IRoleService
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
2021-04-05 10:08:49 +04:00
|
|
|
|
protected override OperationResultModel AdditionalCheckingWhenAdding(DbContext context, RoleSetBindingModel model) => OperationResultModel.Success(null);
|
2021-03-26 20:09:52 +04:00
|
|
|
|
|
2021-04-05 12:25:10 +04:00
|
|
|
|
protected override OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, Role entity, RoleGetBindingModel model)
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
2021-04-05 10:08:49 +04:00
|
|
|
|
if (context.Set<UserRole>().Any(x => x.RoleId == model.Id && !x.IsDeleted))
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
2021-04-05 10:08:49 +04:00
|
|
|
|
return OperationResultModel.Error("Error:", "Существуют пользователи, у которых есть эта роль", ResultServiceStatusCode.ExsistItem);
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
2021-04-05 10:08:49 +04:00
|
|
|
|
return OperationResultModel.Success(null);
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 10:08:49 +04:00
|
|
|
|
protected override IQueryable<Role> AdditionalCheckingWhenReadingList(IQueryable<Role> query, RoleGetBindingModel model) => query;
|
2021-03-26 20:09:52 +04:00
|
|
|
|
|
2021-04-05 10:08:49 +04:00
|
|
|
|
protected override OperationResultModel AdditionalCheckingWhenUpdateing(DbContext context, RoleSetBindingModel model) => OperationResultModel.Success(null);
|
2021-03-26 20:09:52 +04:00
|
|
|
|
|
2021-04-05 12:25:10 +04:00
|
|
|
|
protected override void AdditionalDeleting(DbContext context, Role entity, RoleGetBindingModel model)
|
2021-04-05 10:08:49 +04:00
|
|
|
|
{
|
|
|
|
|
var access = context.Set<Access>().Where(x => x.RoleId == model.Id);
|
|
|
|
|
foreach (var ac in access)
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
2021-04-05 10:08:49 +04:00
|
|
|
|
ac.IsDeleted = true;
|
|
|
|
|
ac.DateDelete = DateTime.Now;
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
2021-04-05 10:08:49 +04:00
|
|
|
|
context.SaveChanges();
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 10:08:49 +04:00
|
|
|
|
protected override Role GetUniqueEntity(RoleSetBindingModel model, DbContext context) => context.Set<Role>().FirstOrDefault(x => x.RoleName == model.RoleName && x.Id != model.Id);
|
2021-03-26 20:09:52 +04:00
|
|
|
|
|
2021-04-05 10:08:49 +04:00
|
|
|
|
protected override IQueryable<Role> IncludingWhenReading(IQueryable<Role> query) => query;
|
2021-03-26 20:09:52 +04:00
|
|
|
|
|
2021-04-05 10:08:49 +04:00
|
|
|
|
protected override IQueryable<Role> OrderingWhenReading(IQueryable<Role> query) => query.OrderBy(x => x.RoleName);
|
2022-03-18 20:09:15 +04:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
}
|