2022-03-18 22:55:48 +04:00
|
|
|
|
using CoreDatabase;
|
|
|
|
|
using CoreDatabase.Models.Security;
|
2021-04-05 10:08:49 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-03-19 19:58:10 +04:00
|
|
|
|
using SecurityContract.BindingModels;
|
2022-03-19 22:48:13 +04:00
|
|
|
|
using SecurityContract.Services.IGenericEntityService;
|
2022-03-19 19:58:10 +04:00
|
|
|
|
using SecurityContract.ViewModels;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2022-03-20 10:10:44 +04:00
|
|
|
|
using ToolsModule.ManagmentEntity;
|
|
|
|
|
using ToolsModule.ManagmentExtension;
|
2021-03-26 20:09:52 +04:00
|
|
|
|
|
2022-03-19 22:48:13 +04:00
|
|
|
|
namespace SecurityDatabaseImplementation.Implementations.AbstractGenerticEntityService
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
2022-03-19 19:58:10 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Реализация IRoleService
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class RoleService :
|
2021-04-05 10:08:49 +04:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2022-03-29 13:33:32 +04:00
|
|
|
|
protected override Role GetUniqueEntity(RoleSetBindingModel model, IQueryable<Role> query) => query.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
|
|
|
|
}
|
|
|
|
|
}
|