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

46 lines
2.0 KiB
C#

using DatabaseCore;
using DatabaseCore.Models.Security;
using Microsoft.EntityFrameworkCore;
using ModuleTools.Models;
using SecurityBusinessLogic.BindingModels;
using SecurityBusinessLogic.Interfaces;
using SecurityBusinessLogic.ViewModels;
using System.Linq;
namespace SecurityDatabaseImplementation.Implementations
{
/// <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, 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, UserRoleGetBindingModel model) { }
protected override UserRole GetUniqueEntity(UserRoleSetBindingModel model, DbContext context) => context.Set<UserRole>().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);
}
}