2022-03-18 22:55:48 +04:00
|
|
|
|
using CoreDatabase;
|
|
|
|
|
using CoreDatabase.Models.Department;
|
|
|
|
|
using CoreDatabase.Models.Security;
|
2021-04-05 10:08:49 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-03-19 19:58:10 +04:00
|
|
|
|
using SecurityContract.BindingModels;
|
|
|
|
|
using SecurityContract.Interfaces.IGenericEntityService;
|
|
|
|
|
using SecurityContract.ViewModels;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2022-03-18 22:38:52 +04:00
|
|
|
|
using ToolsModule.Enums;
|
|
|
|
|
using ToolsModule.Extensions;
|
|
|
|
|
using ToolsModule.Models;
|
2021-03-26 20:09:52 +04:00
|
|
|
|
|
2021-04-01 21:30:29 +04:00
|
|
|
|
namespace SecurityDatabaseImplementation.Implementations
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
2022-03-19 19:58:10 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Реализация IUserService
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class UserService :
|
2021-08-20 18:28:34 +04:00
|
|
|
|
AbstractGenerticEntityService<UserGetBindingModel, UserSetBindingModel, User, UserListViewModel, UserViewModel>,
|
2021-04-05 10:08:49 +04:00
|
|
|
|
IUserService
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
2021-04-05 10:08:49 +04:00
|
|
|
|
protected override OperationResultModel AdditionalCheckingWhenAdding(DbContext context, UserSetBindingModel model)
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
2022-03-18 12:47:34 +04:00
|
|
|
|
if (model.PasswordHash.IsEmpty())
|
2021-04-03 14:27:30 +04:00
|
|
|
|
{
|
2022-03-18 12:47:34 +04:00
|
|
|
|
model.PasswordHash = "qwerty";
|
2021-04-03 14:27:30 +04:00
|
|
|
|
}
|
2022-03-18 12:47:34 +04:00
|
|
|
|
model.PasswordHash = SecurityManager.GetPasswordHash(model.PasswordHash);
|
2021-04-05 10:08:49 +04:00
|
|
|
|
return OperationResultModel.Success(null);
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 12:25:10 +04:00
|
|
|
|
protected override OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, User entity, UserGetBindingModel model)
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
2021-04-05 10:08:49 +04:00
|
|
|
|
if (context.Set<Employee>().Any(x => x.UserId == model.Id && !x.IsDeleted))
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
2021-04-05 10:08:49 +04:00
|
|
|
|
return OperationResultModel.Error("Error:", "Имеется сотрудник, приязанный к этой учетке", ResultServiceStatusCode.ExsistItem);
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
2021-04-05 10:08:49 +04:00
|
|
|
|
if (context.Set<Lecturer>().Any(x => x.UserId == model.Id && !x.IsDeleted))
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
2021-04-05 10:08:49 +04:00
|
|
|
|
return OperationResultModel.Error("Error:", "Имеется преподаватель, приязанный к этой учетке", ResultServiceStatusCode.ExsistItem);
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
2021-04-12 16:57:29 +04:00
|
|
|
|
if (context.Set<Student>().Any(x => x.UserId == model.Id && !x.IsDeleted))
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Имеется студент, приязанный к этой учетке", ResultServiceStatusCode.ExsistItem);
|
|
|
|
|
}
|
2021-04-05 10:08:49 +04:00
|
|
|
|
return OperationResultModel.Success(null);
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 10:08:49 +04:00
|
|
|
|
protected override IQueryable<User> AdditionalCheckingWhenReadingList(IQueryable<User> query, UserGetBindingModel model)
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
2021-04-05 10:08:49 +04:00
|
|
|
|
if (model.UserNameForSearch.IsNotEmpty())
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
|
|
|
|
query = query.Where(x => x.UserName.Contains(model.UserNameForSearch));
|
|
|
|
|
}
|
2021-04-05 10:08:49 +04:00
|
|
|
|
return query;
|
|
|
|
|
}
|
2021-04-01 21:30:29 +04:00
|
|
|
|
|
2021-04-05 10:08:49 +04:00
|
|
|
|
protected override OperationResultModel AdditionalCheckingWhenUpdateing(DbContext context, UserSetBindingModel model) => OperationResultModel.Success(null);
|
2021-03-26 20:09:52 +04:00
|
|
|
|
|
2021-04-05 12:25:10 +04:00
|
|
|
|
protected override void AdditionalDeleting(DbContext context, User entity, UserGetBindingModel model)
|
2021-04-05 10:08:49 +04:00
|
|
|
|
{
|
|
|
|
|
var users = context.Set<UserRole>().Where(x => x.UserId == model.Id);
|
|
|
|
|
foreach (var u in users)
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
2021-04-05 10:08:49 +04:00
|
|
|
|
u.IsDeleted = true;
|
|
|
|
|
u.DateDelete = DateTime.Now;
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
2021-04-05 10:08:49 +04:00
|
|
|
|
context.SaveChanges();
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 12:47:34 +04:00
|
|
|
|
protected override User GetUniqueEntity(UserSetBindingModel model, DbContext context) => context.Set<User>().FirstOrDefault((System.Linq.Expressions.Expression<Func<User, bool>>)(x => x.UserName == model.UserName && x.Id != model.Id));
|
2021-03-26 20:09:52 +04:00
|
|
|
|
|
2021-04-05 10:08:49 +04:00
|
|
|
|
protected override IQueryable<User> IncludingWhenReading(IQueryable<User> query) => query;
|
2021-03-26 20:09:52 +04:00
|
|
|
|
|
2021-04-05 10:08:49 +04:00
|
|
|
|
protected override IQueryable<User> OrderingWhenReading(IQueryable<User> query) => query.OrderBy(x => x.UserName);
|
2021-08-20 18:28:34 +04:00
|
|
|
|
|
|
|
|
|
protected override bool AdditionalCheckForSingleGet(UserGetBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model.Login.IsNotEmpty())
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return base.AdditionalCheckForSingleGet(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override User GetSingleRecord(IQueryable<User> list, UserGetBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model.Login.IsNotEmpty())
|
|
|
|
|
{
|
|
|
|
|
return list.FirstOrDefault(x => x.UserName == model.Login);
|
|
|
|
|
}
|
|
|
|
|
return base.GetSingleRecord(list, model);
|
|
|
|
|
}
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
}
|