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

136 lines
4.2 KiB
C#
Raw Normal View History

using DatabaseCore;
using DatabaseCore.Models.Security;
using Microsoft.EntityFrameworkCore;
using ModelTools.BusinessLogics;
using ModelTools.Enums;
using ModelTools.Extensions;
using ModelTools.Models;
using SecurityBusinessLogic.BindingModels;
using SecurityBusinessLogic.Interfaces;
using SecurityBusinessLogic.ViewModels;
using System;
using System.Linq;
namespace SecurityImplementation.Implementations
{
/// <summary>
/// Реализация IAccessService
/// </summary>
public class AccessService : IAccessService
{
public OperationResultModel Create(AccessSetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var exsistEntity = context.Accesses.FirstOrDefault(x => x.AccessOperation == model.Operation && x.RoleId == model.RoleId && x.AccessType == model.AccessType);
if (exsistEntity == null)
{
var entity = Mapper.MapToClass<AccessSetBindingModel, Access>(model);
context.Accesses.Add(entity);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Access, AccessViewModel>(entity));
}
else
{
if (exsistEntity.IsDeleted)
{
exsistEntity.IsDeleted = false;
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Access, AccessViewModel>(exsistEntity));
}
else
{
return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem);
}
}
}
public OperationResultModel Delete(AccessGetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var entity = context.Accesses.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();
return OperationResultModel.Success(true);
}
public OperationResultModel Read(AccessGetBindingModel model)
{
int countPages = 0;
using var context = DatabaseManager.GetContext;
// для одной записи
if(model.Id.HasValue)
{
var entity = context.Accesses.FirstOrDefault(x => x.Id == model.Id.Value);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
return OperationResultModel.Success(Mapper.MapToClass<Access, AccessViewModel>(entity));
}
var query = context.Accesses.Where(x => !x.IsDeleted).AsQueryable();
if (model.RoleId.HasValue)
{
query = query.Where(x => x.RoleId == model.RoleId);
}
if(model.RoleName.IsNotEmpty())
{
query = query.Where(x => x.Role.RoleName == model.RoleName);
}
query = query.OrderBy(x => x.Role.RoleName).ThenBy(x => x.AccessOperation).ThenBy(x => x.AccessType);
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);
}
query = query.Include(x => x.Role);
var result = new AccessListViewModel
{
MaxCount = countPages,
List = query.Select(Mapper.MapToClass<Access, AccessViewModel>).ToList()
};
return OperationResultModel.Success(result);
}
public OperationResultModel Update(AccessSetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var entity = context.Accesses.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(model, entity);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Access, AccessViewModel>(entity));
}
}
}