using CoreDatabase;
using CoreDatabase.Models.Department;
using CoreDatabase.Models.Security;
using Microsoft.EntityFrameworkCore;
using SecurityContract.BindingModels;
using SecurityContract.Services.IGenericEntityService;
using SecurityContract.ViewModels;
using System;
using System.Linq;
using ToolsModule.Enums;
using ToolsModule.Extensions;
using ToolsModule.Models;
namespace SecurityDatabaseImplementation.Implementations.AbstractGenerticEntityService
{
///
/// Реализация IUserService
///
public class UserService :
AbstractGenerticEntityService,
IUserService
{
protected override OperationResultModel AdditionalCheckingWhenAdding(DbContext context, UserSetBindingModel model)
{
if (model.PasswordHash.IsEmpty())
{
model.PasswordHash = "qwerty";
}
model.PasswordHash = SecurityManager.GetPasswordHash(model.PasswordHash);
return OperationResultModel.Success(null);
}
protected override OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, User entity, UserGetBindingModel model)
{
if (context.Set().Any(x => x.UserId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Имеется сотрудник, приязанный к этой учетке", ResultServiceStatusCode.ExsistItem);
}
if (context.Set().Any(x => x.UserId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Имеется преподаватель, приязанный к этой учетке", ResultServiceStatusCode.ExsistItem);
}
if (context.Set().Any(x => x.UserId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Имеется студент, приязанный к этой учетке", ResultServiceStatusCode.ExsistItem);
}
return OperationResultModel.Success(null);
}
protected override IQueryable AdditionalCheckingWhenReadingList(IQueryable 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().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().FirstOrDefault((System.Linq.Expressions.Expression>)(x => x.UserName == model.UserName && x.Id != model.Id));
protected override IQueryable IncludingWhenReading(IQueryable query) => query;
protected override IQueryable OrderingWhenReading(IQueryable query) => query.OrderBy(x => x.UserName);
protected override bool AdditionalCheckForSingleGet(UserGetBindingModel model)
{
if (model.Login.IsNotEmpty())
{
return true;
}
return base.AdditionalCheckForSingleGet(model);
}
protected override User GetSingleRecord(IQueryable list, UserGetBindingModel model)
{
if (model.Login.IsNotEmpty())
{
return list.FirstOrDefault(x => x.UserName == model.Login);
}
return base.GetSingleRecord(list, model);
}
}
}