using CoreDatabase;
using CoreDatabase.Models.Security;
using Microsoft.EntityFrameworkCore;
using SecurityContract.BindingModels;
using SecurityContract.Services.IGenericEntityService;
using SecurityContract.ViewModels;
using System.Linq;
using ToolsModule.Models;
namespace SecurityDatabaseImplementation.Implementations.AbstractGenerticEntityService
{
///
/// Реализация IUserRoleService
///
public class UserRoleService :
AbstractGenerticEntityService,
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 AdditionalCheckingWhenReadingList(IQueryable 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, DbContext context) => context.Set().FirstOrDefault(x => x.UserId == model.UserId && x.RoleId == model.RoleId && x.Id != model.Id);
protected override IQueryable IncludingWhenReading(IQueryable query) => query.Include(x => x.Role).Include(x => x.User);
protected override IQueryable OrderingWhenReading(IQueryable query) => query.OrderBy(x => x.User.UserName);
}
}