2021-03-26 20:09:52 +04:00
|
|
|
|
using DatabaseCore;
|
|
|
|
|
using DatabaseCore.Models.Security;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
using ModuleTools.BusinessLogics;
|
|
|
|
|
using ModuleTools.Enums;
|
2021-04-01 21:30:29 +04:00
|
|
|
|
using ModuleTools.Extensions;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
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-04-01 21:30:29 +04:00
|
|
|
|
namespace SecurityDatabaseImplementation.Implementations
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
2021-03-28 20:26:25 +04:00
|
|
|
|
var entity = Mapper.MapToClass<UserSetBindingModel, User>(model, true);
|
2021-03-26 20:09:52 +04:00
|
|
|
|
context.Users.Add(entity);
|
|
|
|
|
context.SaveChanges();
|
2021-03-28 20:26:25 +04:00
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(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<User, UserViewModel>(exsistEntity, true));
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}
|
2021-03-28 20:26:25 +04:00
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(entity, model.HaveRight));
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var query = context.Users.Where(x => !x.IsDeleted).AsQueryable();
|
|
|
|
|
|
2021-04-01 21:30:29 +04:00
|
|
|
|
if(model.UserNameForSearch.IsNotEmpty())
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(x => x.UserName.Contains(model.UserNameForSearch));
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-26 20:09:52 +04:00
|
|
|
|
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,
|
2021-03-28 20:26:25 +04:00
|
|
|
|
List = query.Select(x => Mapper.MapToClass<User, UserViewModel>(x, model.HaveRight)).ToList()
|
2021-03-26 20:09:52 +04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2021-03-30 22:34:31 +04:00
|
|
|
|
if(entity.IsBanned && !model.IsBanned)
|
|
|
|
|
{
|
|
|
|
|
model.DateBanned = null;
|
|
|
|
|
model.CountAttempt = 0;
|
|
|
|
|
}
|
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<User, UserViewModel>(entity, true));
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|