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

72 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DatabaseCore;
using DatabaseCore.Models.Security;
using Microsoft.EntityFrameworkCore;
using ModuleTools.Enums;
using ModuleTools.Extensions;
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);
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);
}
}
}