2021-03-26 20:09:52 +04:00
|
|
|
|
using DatabaseCore;
|
|
|
|
|
using DatabaseCore.Models.Security;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
using ModuleTools.BusinessLogics;
|
|
|
|
|
using ModuleTools.Enums;
|
|
|
|
|
using ModuleTools.Models;
|
2021-03-26 20:09:52 +04:00
|
|
|
|
using SecurityBusinessLogic.BindingModels;
|
|
|
|
|
using SecurityBusinessLogic.Interfaces;
|
|
|
|
|
using SecurityBusinessLogic.ViewModels;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2021-04-01 21:30:29 +04:00
|
|
|
|
namespace SecurityDatabaseImplementation.Implementations
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Реализация IRoleService
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class RoleService : IRoleService
|
|
|
|
|
{
|
|
|
|
|
public OperationResultModel Create(RoleSetBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
|
|
|
|
|
var exsistEntity = context.Roles.FirstOrDefault(x => x.RoleName == model.RoleName);
|
|
|
|
|
if (exsistEntity == null)
|
|
|
|
|
{
|
2021-03-28 20:26:25 +04:00
|
|
|
|
var entity = Mapper.MapToClass<RoleSetBindingModel, Role>(model, true);
|
2021-03-26 20:09:52 +04:00
|
|
|
|
context.Roles.Add(entity);
|
|
|
|
|
context.SaveChanges();
|
2021-03-28 20:26:25 +04:00
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(entity, true));
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (exsistEntity.IsDeleted)
|
|
|
|
|
{
|
2021-04-03 11:38:43 +04:00
|
|
|
|
exsistEntity = Mapper.MapToClass(model, exsistEntity, true);
|
2021-03-26 20:09:52 +04:00
|
|
|
|
exsistEntity.IsDeleted = false;
|
|
|
|
|
context.SaveChanges();
|
2021-03-28 20:26:25 +04:00
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(exsistEntity, true));
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OperationResultModel Delete(RoleGetBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var entity = context.Roles.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (entity == null)
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
else if (entity.IsDeleted)
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
|
|
|
|
|
}
|
2021-04-03 23:46:48 +04:00
|
|
|
|
if(context.UserRoles.Any(x => x.RoleId == model.Id && !x.IsDeleted))
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Существуют пользователи, у которых есть эта роль", ResultServiceStatusCode.ExsistItem);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-26 20:09:52 +04:00
|
|
|
|
entity.IsDeleted = true;
|
|
|
|
|
entity.DateDelete = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
var access = context.Accesses.Where(x => x.RoleId == model.Id);
|
|
|
|
|
foreach(var ac in access)
|
|
|
|
|
{
|
|
|
|
|
ac.IsDeleted = true;
|
|
|
|
|
ac.DateDelete = DateTime.Now;
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
}
|
|
|
|
|
catch(Exception)
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return OperationResultModel.Success(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OperationResultModel Read(RoleGetBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
int countPages = 0;
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
|
|
|
|
|
// для одной записи
|
|
|
|
|
if (model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var entity = context.Roles.FirstOrDefault(x => x.Id == model.Id.Value);
|
|
|
|
|
if (entity == null)
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|
|
|
|
}
|
2021-03-28 20:26:25 +04:00
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(entity, model.HaveRight));
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var query = context.Roles.Where(x => !x.IsDeleted).AsQueryable();
|
|
|
|
|
|
|
|
|
|
query = query.OrderBy(x => x.RoleName);
|
|
|
|
|
|
|
|
|
|
if (model.PageNumber.HasValue && model.PageSize.HasValue)
|
|
|
|
|
{
|
|
|
|
|
countPages = (int)Math.Ceiling((double)query.Count() / model.PageSize.Value);
|
|
|
|
|
query = query
|
|
|
|
|
.Skip(model.PageSize.Value * model.PageNumber.Value)
|
|
|
|
|
.Take(model.PageSize.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = new RoleListViewModel
|
|
|
|
|
{
|
|
|
|
|
MaxCount = countPages,
|
2021-03-28 20:26:25 +04:00
|
|
|
|
List = query.Select(x => Mapper.MapToClass<Role, RoleViewModel>(x, model.HaveRight)).ToList()
|
2021-03-26 20:09:52 +04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return OperationResultModel.Success(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OperationResultModel Update(RoleSetBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
|
|
|
|
|
var entity = context.Roles.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (entity == null)
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
else if (entity.IsDeleted)
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
|
|
|
|
|
}
|
2021-03-28 20:26:25 +04:00
|
|
|
|
entity = Mapper.MapToClass<RoleSetBindingModel, Role>(model, entity, true);
|
2021-03-26 20:09:52 +04:00
|
|
|
|
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
2021-03-28 20:26:25 +04:00
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(entity, true));
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|