171 lines
5.2 KiB
C#
171 lines
5.2 KiB
C#
|
using DatabaseCore;
|
|||
|
using DatabaseCore.Models.Security;
|
|||
|
using ModelTools.BusinessLogics;
|
|||
|
using ModelTools.Enums;
|
|||
|
using ModelTools.Extensions;
|
|||
|
using ModelTools.OperationResultModels;
|
|||
|
using SecurityBusinessLogic.BindingModels;
|
|||
|
using SecurityBusinessLogic.Interfaces;
|
|||
|
using SecurityBusinessLogic.ViewModels;
|
|||
|
using System;
|
|||
|
using System.Linq;
|
|||
|
|
|||
|
namespace SecurityImplementation.Implementations
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Реализация IUserService
|
|||
|
/// </summary>
|
|||
|
public class UserService : IUserService
|
|||
|
{
|
|||
|
public OperationResultModel Create(UserSetBindingModel model)
|
|||
|
{
|
|||
|
using var context = DatabaseManager.GetContext;
|
|||
|
|
|||
|
var exsistEntity = context.Users.FirstOrDefault(x => x.UserName == model.Login);
|
|||
|
if (exsistEntity == null)
|
|||
|
{
|
|||
|
var entity = Mapper.MapToClass<UserSetBindingModel, User>(model);
|
|||
|
context.Users.Add(entity);
|
|||
|
context.SaveChanges();
|
|||
|
return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(entity));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
if (exsistEntity.IsDeleted)
|
|||
|
{
|
|||
|
exsistEntity.IsDeleted = false;
|
|||
|
context.SaveChanges();
|
|||
|
return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(exsistEntity));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public OperationResultModel Delete(UserGetBindingModel model)
|
|||
|
{
|
|||
|
using var context = DatabaseManager.GetContext;
|
|||
|
using var transaction = context.Database.BeginTransaction();
|
|||
|
try
|
|||
|
{
|
|||
|
var entity = context.Users.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 users = context.UserRoles.Where(x => x.UserId == 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(UserGetBindingModel model)
|
|||
|
{
|
|||
|
int countPages = 0;
|
|||
|
using var context = DatabaseManager.GetContext;
|
|||
|
|
|||
|
// для одной записи
|
|||
|
if (model.Id.HasValue)
|
|||
|
{
|
|||
|
var entity = context.Users.FirstOrDefault(x => x.Id == model.Id.Value);
|
|||
|
if (entity == null)
|
|||
|
{
|
|||
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|||
|
}
|
|||
|
return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(entity));
|
|||
|
}
|
|||
|
|
|||
|
if (model.Login.IsNotEmpty() && model.Password.IsNotEmpty())
|
|||
|
{
|
|||
|
var entity = context.Users.FirstOrDefault(x => x.UserName == model.Login && x.PasswordHash == model.Password);
|
|||
|
if (entity == null)
|
|||
|
{
|
|||
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|||
|
}
|
|||
|
return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(entity));
|
|||
|
}
|
|||
|
|
|||
|
if (model.Login.IsNotEmpty() && model.IsBanned.HasValue)
|
|||
|
{
|
|||
|
var entity = context.Users.FirstOrDefault(x => x.UserName == model.Login && x.IsBanned == model.IsBanned.Value);
|
|||
|
if (entity == null)
|
|||
|
{
|
|||
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|||
|
}
|
|||
|
return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(entity));
|
|||
|
}
|
|||
|
|
|||
|
var query = context.Users.Where(x => !x.IsDeleted).AsQueryable();
|
|||
|
if (model.IsBanned.HasValue)
|
|||
|
{
|
|||
|
query = query.Where(x => x.IsBanned == model.IsBanned.Value);
|
|||
|
}
|
|||
|
if (model.LecturerIds != null)
|
|||
|
{
|
|||
|
query = query.Where(x => x.LecturerId.HasValue && model.LecturerIds.Contains(x.LecturerId.Value));
|
|||
|
}
|
|||
|
|
|||
|
query = query.OrderBy(x => x.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);
|
|||
|
}
|
|||
|
|
|||
|
var result = new UserListViewModel
|
|||
|
{
|
|||
|
MaxCount = countPages,
|
|||
|
List = query.Select(Mapper.MapToClass<User, UserViewModel>).ToList()
|
|||
|
};
|
|||
|
|
|||
|
return OperationResultModel.Success(result);
|
|||
|
}
|
|||
|
|
|||
|
public OperationResultModel Update(UserSetBindingModel model)
|
|||
|
{
|
|||
|
using var context = DatabaseManager.GetContext;
|
|||
|
|
|||
|
var entity = context.Users.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<User, UserViewModel>(entity));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|