DepartmentProject/DepartmentPortal/Security/SecurityDatabaseImplementation/Implementations/UserRoleService.cs

136 lines
4.2 KiB
C#
Raw Normal View History

using DatabaseCore;
using DatabaseCore.Models.Security;
using Microsoft.EntityFrameworkCore;
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>
/// Реализация IUserRoleService
/// </summary>
public class UserRoleService : IUserRoleService
{
public OperationResultModel Create(UserRoleSetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var exsistEntity = context.UserRoles.FirstOrDefault(x => x.UserId == model.UserId && x.RoleId == model.RoleId);
if (exsistEntity == null)
{
var entity = Mapper.MapToClass<UserRoleSetBindingModel, UserRole>(model, true);
context.UserRoles.Add(entity);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<UserRole, UserRoleViewModel>(entity, true));
}
else
{
if (exsistEntity.IsDeleted)
{
exsistEntity = Mapper.MapToClass(model, exsistEntity, true);
exsistEntity.IsDeleted = false;
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<UserRole, UserRoleViewModel>(exsistEntity, true));
}
else
{
return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem);
}
}
}
public OperationResultModel Delete(UserRoleGetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var entity = context.UserRoles.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(UserRoleGetBindingModel model)
{
int countPages = 0;
using var context = DatabaseManager.GetContext;
// для одной записи
if (model.Id.HasValue)
{
var entity = context.UserRoles.FirstOrDefault(x => x.Id == model.Id.Value);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
return OperationResultModel.Success(Mapper.MapToClass<UserRole, UserRoleViewModel>(entity, model.HaveRight));
}
var query = context.UserRoles.Where(x => !x.IsDeleted).AsQueryable();
if (model.RoleId.HasValue)
{
query = query.Where(x => x.RoleId == model.RoleId);
}
if (model.UserId.HasValue)
{
query = query.Where(x => x.UserId == model.UserId);
}
query = query.OrderBy(x => x.User.UserName);
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).Include(x => x.User);
var result = new UserRoleListViewModel
{
MaxCount = countPages,
List = query.Select(x => Mapper.MapToClass<UserRole, UserRoleViewModel>(x, model.HaveRight)).ToList()
};
return OperationResultModel.Success(result);
}
public OperationResultModel Update(UserRoleSetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var entity = context.UserRoles.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, true);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<UserRole, UserRoleViewModel>(entity, true));
}
}
}