2021-03-26 20:09:52 +04:00
|
|
|
|
using DatabaseCore;
|
|
|
|
|
using DatabaseCore.Models.Security;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
using ModuleTools.BusinessLogics;
|
|
|
|
|
using ModuleTools.Enums;
|
|
|
|
|
using ModuleTools.Extensions;
|
|
|
|
|
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-03-28 11:14:14 +04:00
|
|
|
|
namespace SecurityImplementation.SecurityDatabaseImplementation
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Реализация IAccessService
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class AccessService : IAccessService
|
|
|
|
|
{
|
|
|
|
|
public OperationResultModel Create(AccessSetBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
|
2021-03-27 23:50:29 +04:00
|
|
|
|
var exsistEntity = context.Accesses.FirstOrDefault(x => x.AccessOperation == model.Operation && x.RoleId == model.RoleId && x.AccessType == model.AccessType);
|
2021-03-26 20:09:52 +04:00
|
|
|
|
if (exsistEntity == null)
|
|
|
|
|
{
|
2021-03-28 20:26:25 +04:00
|
|
|
|
var entity = Mapper.MapToClass<AccessSetBindingModel, Access>(model, true);
|
2021-03-26 20:09:52 +04:00
|
|
|
|
context.Accesses.Add(entity);
|
|
|
|
|
context.SaveChanges();
|
2021-03-28 20:26:25 +04:00
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<Access, AccessViewModel>(entity, true));
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (exsistEntity.IsDeleted)
|
|
|
|
|
{
|
|
|
|
|
exsistEntity.IsDeleted = false;
|
|
|
|
|
context.SaveChanges();
|
2021-03-28 20:26:25 +04:00
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<Access, AccessViewModel>(exsistEntity, true));
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}
|
2021-03-28 20:26:25 +04:00
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<Access, AccessViewModel>(entity, model.HaveRight));
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var query = context.Accesses.Where(x => !x.IsDeleted).AsQueryable();
|
|
|
|
|
if (model.RoleId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(x => x.RoleId == model.RoleId);
|
|
|
|
|
}
|
2021-03-27 23:50:29 +04:00
|
|
|
|
if(model.RoleName.IsNotEmpty())
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(x => x.Role.RoleName == model.RoleName);
|
|
|
|
|
}
|
2021-03-26 20:09:52 +04:00
|
|
|
|
|
2021-03-27 23:50:29 +04:00
|
|
|
|
query = query.OrderBy(x => x.Role.RoleName).ThenBy(x => x.AccessOperation).ThenBy(x => x.AccessType);
|
2021-03-26 20:09:52 +04:00
|
|
|
|
|
|
|
|
|
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,
|
2021-03-28 20:26:25 +04:00
|
|
|
|
List = query.Select(x => Mapper.MapToClass<Access, AccessViewModel>(x, model.HaveRight)).ToList()
|
2021-03-26 20:09:52 +04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2021-03-28 20:26:25 +04:00
|
|
|
|
entity = Mapper.MapToClass(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<Access, AccessViewModel>(entity, true));
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|