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

64 lines
2.6 KiB
C#

using CoreDatabase;
using CoreDatabase.Models.Security;
using Microsoft.EntityFrameworkCore;
using SecurityContract.BindingModels;
using SecurityContract.Services.IGenericEntityService;
using SecurityContract.ViewModels;
using System.Linq;
using ToolsModule.ManagmentEntity;
namespace SecurityDatabaseImplementation.Implementations.AbstractGenerticEntityService
{
/// <summary>
/// Реализация IUserRoleService
/// </summary>
public class UserRoleService :
AbstractGenerticEntityService<UserRoleGetBindingModel, UserRoleSetBindingModel, UserRole, UserRoleListViewModel, UserRoleViewModel>,
IUserRoleService
{
protected override OperationResultModel AdditionalCheckingWhenAdding(DbContext context, UserRoleSetBindingModel model) => OperationResultModel.Success(null);
protected override OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, UserRole entity, UserRoleGetBindingModel model) => OperationResultModel.Success(null);
protected override IQueryable<UserRole> AdditionalCheckingWhenReadingList(IQueryable<UserRole> query, UserRoleGetBindingModel model)
{
if (model.RoleId.HasValue)
{
query = query.Where(x => x.RoleId == model.RoleId);
}
if (model.UserId.HasValue)
{
query = query.Where(x => x.UserId == model.UserId);
}
return query;
}
protected override OperationResultModel AdditionalCheckingWhenUpdateing(DbContext context, UserRoleSetBindingModel model) => OperationResultModel.Success(null);
protected override void AdditionalDeleting(DbContext context, UserRole entity, UserRoleGetBindingModel model) { }
protected override UserRole GetUniqueEntity(UserRoleSetBindingModel model, IQueryable<UserRole> query) => query.FirstOrDefault(x => x.UserId == model.UserId && x.RoleId == model.RoleId && x.Id != model.Id);
protected override IQueryable<UserRole> IncludingWhenReading(IQueryable<UserRole> query) => query.Include(x => x.Role).Include(x => x.User);
protected override IQueryable<UserRole> OrderingWhenReading(IQueryable<UserRole> query) => query.OrderBy(x => x.User.UserName);
protected override bool AdditionalCheckForSingleGet(UserRoleGetBindingModel model)
{
if (model.RoleId.HasValue && model.UserId.HasValue)
{
return true;
}
return base.AdditionalCheckForSingleGet(model);
}
protected override UserRole GetSingleRecord(IQueryable<UserRole> list, UserRoleGetBindingModel model)
{
if (model.RoleId.HasValue && model.UserId.HasValue)
{
return list.FirstOrDefault(x => x.RoleId == model.RoleId && x.UserId == model.UserId);
}
return base.GetSingleRecord(list, model);
}
}
}