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 { /// /// Реализация IUserService /// public class UserService : AbstractGenerticEntityService, 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().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(x => x.UserName == model.Login && x.Id != model.Id); protected override IQueryable IncludingWhenReading(IQueryable query) => query; protected override IQueryable OrderingWhenReading(IQueryable query) => query.OrderBy(x => x.UserName); } }