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 { /// /// Реализация 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, IQueryable query) => query.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); protected override bool AdditionalCheckForSingleGet(UserRoleGetBindingModel model) { if (model.RoleId.HasValue && model.UserId.HasValue) { return true; } return base.AdditionalCheckForSingleGet(model); } protected override UserRole GetSingleRecord(IQueryable 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); } } }