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

74 lines
2.8 KiB
C#

using DatabaseCore;
using DatabaseCore.Models.Department;
using DatabaseCore.Models.Security;
using Microsoft.EntityFrameworkCore;
using ModuleTools.Enums;
using ModuleTools.Extensions;
using ModuleTools.Models;
using SecurityBusinessLogic.BindingModels;
using SecurityBusinessLogic.Interfaces;
using SecurityBusinessLogic.ViewModels;
using System;
using System.Linq;
namespace SecurityDatabaseImplementation.Implementations
{
/// <summary>
/// Реализация IUserService
/// </summary>
public class UserService :
AbstractGenerticEntityService<UserGetBindingModel, UserSetBindingModel, User, UserListViewModel, UserViewModel>,
IUserService
{
protected override OperationResultModel AdditionalCheckingWhenAdding(DbContext context, UserSetBindingModel model)
{
if (model.Password.IsEmpty())
{
model.Password = "qwerty";
}
model.Password = SecurityManager.GetPasswordHash(model.Password);
return OperationResultModel.Success(null);
}
protected override OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, User entity, UserGetBindingModel model)
{
if (context.Set<Employee>().Any(x => x.UserId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Имеется сотрудник, приязанный к этой учетке", ResultServiceStatusCode.ExsistItem);
}
if (context.Set<Lecturer>().Any(x => x.UserId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Имеется преподаватель, приязанный к этой учетке", ResultServiceStatusCode.ExsistItem);
}
return OperationResultModel.Success(null);
}
protected override IQueryable<User> AdditionalCheckingWhenReadingList(IQueryable<User> query, UserGetBindingModel model)
{
if (model.UserNameForSearch.IsNotEmpty())
{
query = query.Where(x => x.UserName.Contains(model.UserNameForSearch));
}
return query;
}
protected override OperationResultModel AdditionalCheckingWhenUpdateing(DbContext context, UserSetBindingModel model) => OperationResultModel.Success(null);
protected override void AdditionalDeleting(DbContext context, User entity, UserGetBindingModel model)
{
var users = context.Set<UserRole>().Where(x => x.UserId == model.Id);
foreach (var u in users)
{
u.IsDeleted = true;
u.DateDelete = DateTime.Now;
}
context.SaveChanges();
}
protected override User GetUniqueEntity(UserSetBindingModel model, DbContext context) => context.Set<User>().FirstOrDefault(x => x.UserName == model.Login && x.Id != model.Id);
protected override IQueryable<User> IncludingWhenReading(IQueryable<User> query) => query;
protected override IQueryable<User> OrderingWhenReading(IQueryable<User> query) => query.OrderBy(x => x.UserName);
}
}