DepartmentProject/DepartmentPortal/Security/SecurityImplementation/Implementations/RoleService.cs

150 lines
4.3 KiB
C#

using DatabaseCore;
using DatabaseCore.Models.Security;
using ModelTools.BusinessLogics;
using ModelTools.Enums;
using ModelTools.OperationResultModels;
using SecurityBusinessLogic.BindingModels;
using SecurityBusinessLogic.Interfaces;
using SecurityBusinessLogic.ViewModels;
using System;
using System.Linq;
namespace SecurityImplementation.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);
context.Roles.Add(entity);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(entity));
}
else
{
if (exsistEntity.IsDeleted)
{
exsistEntity.IsDeleted = false;
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(exsistEntity));
}
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);
}
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();
var users = context.UserRoles.Where(x => x.RoleId == model.Id);
foreach (var u in users)
{
u.IsDeleted = true;
u.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));
}
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(Mapper.MapToClass<Role, RoleViewModel>).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);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(entity));
}
}
}