148 lines
4.5 KiB
C#
148 lines
4.5 KiB
C#
using DatabaseCore;
|
||
using DatabaseCore.Models.Security;
|
||
using ModuleTools.BusinessLogics;
|
||
using ModuleTools.Enums;
|
||
using ModuleTools.Models;
|
||
using SecurityBusinessLogic.BindingModels;
|
||
using SecurityBusinessLogic.Interfaces;
|
||
using SecurityBusinessLogic.ViewModels;
|
||
using System;
|
||
using System.Linq;
|
||
|
||
namespace SecurityDatabaseImplementation.Implementations
|
||
{
|
||
/// <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)
|
||
{
|
||
var entity = Mapper.MapToClass<RoleSetBindingModel, Role>(model, true);
|
||
context.Roles.Add(entity);
|
||
context.SaveChanges();
|
||
return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(entity, true));
|
||
}
|
||
else
|
||
{
|
||
if (exsistEntity.IsDeleted)
|
||
{
|
||
exsistEntity = Mapper.MapToClass(model, exsistEntity, true);
|
||
exsistEntity.IsDeleted = false;
|
||
context.SaveChanges();
|
||
return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(exsistEntity, true));
|
||
}
|
||
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);
|
||
}
|
||
if(context.UserRoles.Any(x => x.RoleId == model.Id && !x.IsDeleted))
|
||
{
|
||
return OperationResultModel.Error("Error:", "Существуют пользователи, у которых есть эта роль", ResultServiceStatusCode.ExsistItem);
|
||
}
|
||
|
||
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);
|
||
}
|
||
return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(entity, model.HaveRight));
|
||
}
|
||
|
||
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,
|
||
List = query.Select(x => Mapper.MapToClass<Role, RoleViewModel>(x, model.HaveRight)).ToList()
|
||
};
|
||
|
||
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);
|
||
}
|
||
entity = Mapper.MapToClass<RoleSetBindingModel, Role>(model, entity, true);
|
||
|
||
context.SaveChanges();
|
||
|
||
return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(entity, true));
|
||
}
|
||
}
|
||
} |