using CoreDatabase;
using CoreDatabase.Models.Security;
using Microsoft.EntityFrameworkCore;
using SecurityContract.BindingModels;
using SecurityContract.Services.IGenericEntityService;
using SecurityContract.ViewModels;
using System;
using System.Linq;
using ToolsModule.Enums;
using ToolsModule.Extensions;
using ToolsModule.Models;
namespace SecurityDatabaseImplementation.Implementations.AbstractGenerticEntityService
{
///
/// Реализация IRoleService
///
public class RoleService :
AbstractGenerticEntityService,
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().Any(x => x.RoleId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Существуют пользователи, у которых есть эта роль", ResultServiceStatusCode.ExsistItem);
}
return OperationResultModel.Success(null);
}
protected override IQueryable AdditionalCheckingWhenReadingList(IQueryable 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().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().FirstOrDefault(x => x.RoleName == model.RoleName && x.Id != model.Id);
protected override IQueryable IncludingWhenReading(IQueryable query) => query;
protected override IQueryable OrderingWhenReading(IQueryable 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 list, RoleGetBindingModel model)
{
if (model.RoleName.IsNotEmpty())
{
return list.FirstOrDefault(x => x.RoleName == model.RoleName);
}
return base.GetSingleRecord(list, model);
}
}
}