2021-03-26 20:09:52 +04:00
|
|
|
|
using DatabaseCore;
|
2021-04-05 10:08:49 +04:00
|
|
|
|
using DatabaseCore.Models.Department;
|
2021-03-26 20:09:52 +04:00
|
|
|
|
using DatabaseCore.Models.Security;
|
2021-04-05 10:08:49 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
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>
|
2021-04-05 10:08:49 +04:00
|
|
|
|
public class UserService :
|
|
|
|
|
AbstractGenerticEntityService<UserGetBindingModel, UserSetBindingModel, User, UserListViewModel, UserViewModel>,
|
|
|
|
|
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
|
|
|
|
{
|
2021-04-03 14:27:30 +04:00
|
|
|
|
if (model.Password.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
model.Password = "qwerty";
|
|
|
|
|
}
|
|
|
|
|
model.Password = SecurityManager.GetPasswordHash(model.Password);
|
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-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
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 10:08:49 +04:00
|
|
|
|
protected override User GetUniqueEntity(UserSetBindingModel model, DbContext context) => context.Set<User>().FirstOrDefault(x => x.UserName == model.Login && 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-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
}
|