diff --git a/DepartmentPortal/Department/DepartmentBusinessLogic/BindingModels/LecturerBindingModels.cs b/DepartmentPortal/Department/DepartmentBusinessLogic/BindingModels/LecturerBindingModels.cs index d77bcb8..4b3e91a 100644 --- a/DepartmentPortal/Department/DepartmentBusinessLogic/BindingModels/LecturerBindingModels.cs +++ b/DepartmentPortal/Department/DepartmentBusinessLogic/BindingModels/LecturerBindingModels.cs @@ -11,6 +11,12 @@ namespace DepartmentBusinessLogic.BindingModels public class LecturerGetBindingModel : GetBindingModel { public Guid? UserId { get; set; } + + public Guid? LecturerPostId { get; set; } + + public Guid? LecturerAcademicRankId { get; set; } + + public Guid? LecturerAcademicDegreeId { get; set; } } /// diff --git a/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/DepartmentImplementationExtensions.cs b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/DepartmentImplementationExtensions.cs index 7c72622..10a269b 100644 --- a/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/DepartmentImplementationExtensions.cs +++ b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/DepartmentImplementationExtensions.cs @@ -17,6 +17,12 @@ namespace DepartmentDatabaseImplementation DependencyManager.Instance.RegisterType(); DependencyManager.Instance.RegisterType(); + + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); } } } \ No newline at end of file diff --git a/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/DisciplineBlockService.cs b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/DisciplineBlockService.cs index 509b079..7dc9a54 100644 --- a/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/DisciplineBlockService.cs +++ b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/DisciplineBlockService.cs @@ -47,35 +47,23 @@ namespace DepartmentDatabaseImplementation.Implementations public OperationResultModel Delete(DisciplineBlockGetBindingModel model) { using var context = DatabaseManager.GetContext; - using var transaction = context.Database.BeginTransaction(); - try + var entity = context.DisciplineBlocks.FirstOrDefault(x => x.Id == model.Id); + if (entity == null) { - var entity = context.DisciplineBlocks.FirstOrDefault(x => x.Id == model.Id); - if (entity == null) - { - return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); - } - else if (entity.IsDeleted) - { - return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); - } - var disciplines = context.Disciplines.Where(x => x.DisciplineBlockId == model.Id); - if (disciplines.Any()) - { - return OperationResultModel.Error("Error:", "Есть дисциплины, относящиеся к этому блоку", ResultServiceStatusCode.ExsistItem); - } - entity.IsDeleted = true; - entity.DateDelete = DateTime.Now; - - context.SaveChanges(); - - transaction.Commit(); + return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); } - catch (Exception) + else if (entity.IsDeleted) { - transaction.Rollback(); - throw; + return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); } + if (context.Disciplines.Any(x => x.DisciplineBlockId == model.Id && !x.IsDeleted)) + { + return OperationResultModel.Error("Error:", "Есть дисциплины, относящиеся к этому блоку", ResultServiceStatusCode.ExsistItem); + } + entity.IsDeleted = true; + entity.DateDelete = DateTime.Now; + + context.SaveChanges(); return OperationResultModel.Success(true); } diff --git a/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/EmployeePostService.cs b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/EmployeePostService.cs index d3a2ccd..2bdf769 100644 --- a/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/EmployeePostService.cs +++ b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/EmployeePostService.cs @@ -47,38 +47,27 @@ namespace DepartmentDatabaseImplementation.Implementations public OperationResultModel Delete(EmployeePostGetBindingModel model) { using var context = DatabaseManager.GetContext; - using var transaction = context.Database.BeginTransaction(); - try + var entity = context.EmployeePosts.FirstOrDefault(x => x.Id == model.Id); + if (entity == null) { - var entity = context.EmployeePosts.FirstOrDefault(x => x.Id == model.Id); - if (entity == null) - { - return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); - } - else if (entity.IsDeleted) - { - return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); - } - entity.IsDeleted = true; - entity.DateDelete = DateTime.Now; - - context.SaveChanges(); - - var links = context.EmployeeEmployeePosts.Where(x => x.EmployeePostId == model.Id); - foreach (var link in links) - { - link.IsDeleted = true; - link.DateDelete = DateTime.Now; - } - context.SaveChanges(); - - transaction.Commit(); + return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); } - catch (Exception) + else if (entity.IsDeleted) { - transaction.Rollback(); - throw; + return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); } + if (context.EmployeeEmployeePosts.Any(x => x.EmployeePostId == model.Id && !x.IsDeleted)) + { + return OperationResultModel.Error("Error:", "Существуют сотрудники с этой должностью", ResultServiceStatusCode.ExsistItem); + } + if (context.LecturerEmployeePosts.Any(x => x.EmployeePostId == model.Id && !x.IsDeleted)) + { + return OperationResultModel.Error("Error:", "Существуют преподаватели с этой должностью", ResultServiceStatusCode.ExsistItem); + } + entity.IsDeleted = true; + entity.DateDelete = DateTime.Now; + + context.SaveChanges(); return OperationResultModel.Success(true); } @@ -133,7 +122,7 @@ namespace DepartmentDatabaseImplementation.Implementations { return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); } - entity = Mapper.MapToClass(model, entity, true); + entity = Mapper.MapToClass(model, entity, true); context.SaveChanges(); diff --git a/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/EmployeeService.cs b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/EmployeeService.cs index ef9a1a7..e149983 100644 --- a/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/EmployeeService.cs +++ b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/EmployeeService.cs @@ -3,6 +3,7 @@ using DatabaseCore.Models.Department; using DepartmentBusinessLogic.BindingModels; using DepartmentBusinessLogic.Interfaces; using DepartmentBusinessLogic.ViewModels; +using Microsoft.EntityFrameworkCore; using ModuleTools.BusinessLogics; using ModuleTools.Enums; using ModuleTools.Models; @@ -51,7 +52,7 @@ namespace DepartmentDatabaseImplementation.Implementations using var transaction = context.Database.BeginTransaction(); try { - var entity = context.Employees.FirstOrDefault(x => x.Id == model.Id); + var entity = context.Employees.Include(x => x.User).FirstOrDefault(x => x.Id == model.Id); if (entity == null) { return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); @@ -60,13 +61,18 @@ namespace DepartmentDatabaseImplementation.Implementations { return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); } - var classrooms = context.Classrooms.Where(x => x.EmployeeId == model.Id); - if (classrooms.Any()) + else if (entity.User == null) + { + return OperationResultModel.Error("Error:", "Не найден пользователь от сотрудника", ResultServiceStatusCode.NotFound); + } + if (context.Classrooms.Any(x => x.EmployeeId == model.Id && !x.IsDeleted)) { return OperationResultModel.Error("Error:", "Есть аудитории, у которых этот сотрудник указан ответственным", ResultServiceStatusCode.ExsistItem); } entity.IsDeleted = true; entity.DateDelete = DateTime.Now; + entity.User.IsDeleted = true; + entity.User.DateDelete = DateTime.Now; context.SaveChanges(); @@ -78,6 +84,7 @@ namespace DepartmentDatabaseImplementation.Implementations } context.SaveChanges(); + transaction.Commit(); } catch (Exception) @@ -106,6 +113,10 @@ namespace DepartmentDatabaseImplementation.Implementations } var query = context.Employees.Where(x => !x.IsDeleted).AsQueryable(); + if (model.UserId.HasValue) + { + query = query.Where(x => x.UserId == model.UserId); + } query = query.OrderBy(x => x.LastName); diff --git a/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/LecturerAcademicDegreeService.cs b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/LecturerAcademicDegreeService.cs new file mode 100644 index 0000000..ba657ec --- /dev/null +++ b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/LecturerAcademicDegreeService.cs @@ -0,0 +1,128 @@ +using DatabaseCore; +using DatabaseCore.Models.Department; +using DepartmentBusinessLogic.BindingModels; +using DepartmentBusinessLogic.Interfaces; +using DepartmentBusinessLogic.ViewModels; +using ModuleTools.BusinessLogics; +using ModuleTools.Enums; +using ModuleTools.Models; +using System; +using System.Linq; + +namespace DepartmentDatabaseImplementation.Implementations +{ + /// + /// Реализация ILecturerAcademicDegreeService + /// + public class LecturerAcademicDegreeService : ILecturerAcademicDegreeService + { + public OperationResultModel Create(LecturerAcademicDegreeSetBindingModel model) + { + using var context = DatabaseManager.GetContext; + + var exsistEntity = context.LecturerAcademicDegrees.FirstOrDefault(x => x.LecturerAcademicDegreeName == model.LecturerAcademicDegreeName); + if (exsistEntity == null) + { + var entity = Mapper.MapToClass(model, true); + context.LecturerAcademicDegrees.Add(entity); + context.SaveChanges(); + return OperationResultModel.Success(Mapper.MapToClass(entity, true)); + } + else + { + if (exsistEntity.IsDeleted) + { + exsistEntity = Mapper.MapToClass(model, exsistEntity, true); + exsistEntity.IsDeleted = false; + context.SaveChanges(); + return OperationResultModel.Success(Mapper.MapToClass(exsistEntity, true)); + } + else + { + return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem); + } + } + } + + public OperationResultModel Delete(LecturerAcademicDegreeGetBindingModel model) + { + using var context = DatabaseManager.GetContext; + var entity = context.LecturerAcademicDegrees.FirstOrDefault(x => x.Id == model.Id); + if (entity == null) + { + return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); + } + else if (entity.IsDeleted) + { + return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); + } + if (context.Lecturers.Any(x => x.LecturerAcademicDegreeId == model.Id && !x.IsDeleted)) + { + return OperationResultModel.Error("Error:", "Имеются сотрудники, использующую эту запись", ResultServiceStatusCode.ExsistItem); + } + entity.IsDeleted = true; + entity.DateDelete = DateTime.Now; + + context.SaveChanges(); + + return OperationResultModel.Success(true); + } + + public OperationResultModel Read(LecturerAcademicDegreeGetBindingModel model) + { + int countPages = 0; + using var context = DatabaseManager.GetContext; + + // для одной записи + if (model.Id.HasValue) + { + var entity = context.LecturerAcademicDegrees.FirstOrDefault(x => x.Id == model.Id.Value); + if (entity == null) + { + return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); + } + return OperationResultModel.Success(Mapper.MapToClass(entity, model.HaveRight)); + } + + var query = context.LecturerAcademicDegrees.Where(x => !x.IsDeleted).AsQueryable(); + + query = query.OrderBy(x => x.Order); + + if (model.PageNumber.HasValue && model.PageSize.HasValue) + { + countPages = (int)Math.Ceiling((double)query.Count() / model.PageSize.Value); + query = query + .Skip(model.PageSize.Value * model.PageNumber.Value) + .Take(model.PageSize.Value); + } + + var result = new LecturerAcademicDegreeListViewModel + { + MaxCount = countPages, + List = query.Select(x => Mapper.MapToClass(x, model.HaveRight)).ToList() + }; + + return OperationResultModel.Success(result); + } + + public OperationResultModel Update(LecturerAcademicDegreeSetBindingModel model) + { + using var context = DatabaseManager.GetContext; + + var entity = context.LecturerAcademicDegrees.FirstOrDefault(x => x.Id == model.Id); + if (entity == null) + { + return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); + } + else if (entity.IsDeleted) + { + return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); + } + entity = Mapper.MapToClass(model, entity, true); + + context.SaveChanges(); + + return OperationResultModel.Success(Mapper.MapToClass(entity, true)); + } + } +} \ No newline at end of file diff --git a/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/LecturerAcademicRankService.cs b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/LecturerAcademicRankService.cs new file mode 100644 index 0000000..69cafe8 --- /dev/null +++ b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/LecturerAcademicRankService.cs @@ -0,0 +1,128 @@ +using DatabaseCore; +using DatabaseCore.Models.Department; +using DepartmentBusinessLogic.BindingModels; +using DepartmentBusinessLogic.Interfaces; +using DepartmentBusinessLogic.ViewModels; +using ModuleTools.BusinessLogics; +using ModuleTools.Enums; +using ModuleTools.Models; +using System; +using System.Linq; + +namespace DepartmentDatabaseImplementation.Implementations +{ + /// + /// Реализация ILecturerAcademicRankService + /// + public class LecturerAcademicRankService : ILecturerAcademicRankService + { + public OperationResultModel Create(LecturerAcademicRankSetBindingModel model) + { + using var context = DatabaseManager.GetContext; + + var exsistEntity = context.LecturerAcademicRanks.FirstOrDefault(x => x.LecturerAcademicRankName == model.LecturerAcademicRankName); + if (exsistEntity == null) + { + var entity = Mapper.MapToClass(model, true); + context.LecturerAcademicRanks.Add(entity); + context.SaveChanges(); + return OperationResultModel.Success(Mapper.MapToClass(entity, true)); + } + else + { + if (exsistEntity.IsDeleted) + { + exsistEntity = Mapper.MapToClass(model, exsistEntity, true); + exsistEntity.IsDeleted = false; + context.SaveChanges(); + return OperationResultModel.Success(Mapper.MapToClass(exsistEntity, true)); + } + else + { + return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem); + } + } + } + + public OperationResultModel Delete(LecturerAcademicRankGetBindingModel model) + { + using var context = DatabaseManager.GetContext; + var entity = context.LecturerAcademicRanks.FirstOrDefault(x => x.Id == model.Id); + if (entity == null) + { + return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); + } + else if (entity.IsDeleted) + { + return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); + } + if (context.Lecturers.Any(x => x.LecturerAcademicRankId == model.Id && !x.IsDeleted)) + { + return OperationResultModel.Error("Error:", "Имеются сотрудники, использующую эту запись", ResultServiceStatusCode.ExsistItem); + } + entity.IsDeleted = true; + entity.DateDelete = DateTime.Now; + + context.SaveChanges(); + + return OperationResultModel.Success(true); + } + + public OperationResultModel Read(LecturerAcademicRankGetBindingModel model) + { + int countPages = 0; + using var context = DatabaseManager.GetContext; + + // для одной записи + if (model.Id.HasValue) + { + var entity = context.LecturerAcademicRanks.FirstOrDefault(x => x.Id == model.Id.Value); + if (entity == null) + { + return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); + } + return OperationResultModel.Success(Mapper.MapToClass(entity, model.HaveRight)); + } + + var query = context.LecturerAcademicRanks.Where(x => !x.IsDeleted).AsQueryable(); + + query = query.OrderBy(x => x.Order); + + if (model.PageNumber.HasValue && model.PageSize.HasValue) + { + countPages = (int)Math.Ceiling((double)query.Count() / model.PageSize.Value); + query = query + .Skip(model.PageSize.Value * model.PageNumber.Value) + .Take(model.PageSize.Value); + } + + var result = new LecturerAcademicRankListViewModel + { + MaxCount = countPages, + List = query.Select(x => Mapper.MapToClass(x, model.HaveRight)).ToList() + }; + + return OperationResultModel.Success(result); + } + + public OperationResultModel Update(LecturerAcademicRankSetBindingModel model) + { + using var context = DatabaseManager.GetContext; + + var entity = context.LecturerAcademicRanks.FirstOrDefault(x => x.Id == model.Id); + if (entity == null) + { + return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); + } + else if (entity.IsDeleted) + { + return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); + } + entity = Mapper.MapToClass(model, entity, true); + + context.SaveChanges(); + + return OperationResultModel.Success(Mapper.MapToClass(entity, true)); + } + } +} \ No newline at end of file diff --git a/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/LecturerEmployeePostService.cs b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/LecturerEmployeePostService.cs new file mode 100644 index 0000000..e463023 --- /dev/null +++ b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/LecturerEmployeePostService.cs @@ -0,0 +1,135 @@ +using DatabaseCore; +using DatabaseCore.Models.Department; +using DepartmentBusinessLogic.BindingModels; +using DepartmentBusinessLogic.Interfaces; +using DepartmentBusinessLogic.ViewModels; +using Microsoft.EntityFrameworkCore; +using ModuleTools.BusinessLogics; +using ModuleTools.Enums; +using ModuleTools.Models; +using System; +using System.Linq; + +namespace DepartmentDatabaseImplementation.Implementations +{ + /// + /// Реализация ILecturerEmployeePostService + /// + public class LecturerEmployeePostService : ILecturerEmployeePostService + { + public OperationResultModel Create(LecturerEmployeePostSetBindingModel model) + { + using var context = DatabaseManager.GetContext; + + var exsistEntity = context.LecturerEmployeePosts.FirstOrDefault(x => x.LecturerId == model.LecturerId && x.EmployeePostId == model.EmployeePostId); + if (exsistEntity == null) + { + var entity = Mapper.MapToClass(model, true); + context.LecturerEmployeePosts.Add(entity); + context.SaveChanges(); + return OperationResultModel.Success(Mapper.MapToClass(entity, true)); + } + else + { + if (exsistEntity.IsDeleted) + { + exsistEntity = Mapper.MapToClass(model, exsistEntity, true); + exsistEntity.IsDeleted = false; + context.SaveChanges(); + return OperationResultModel.Success(Mapper.MapToClass(exsistEntity, true)); + } + else + { + return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem); + } + } + } + + public OperationResultModel Delete(LecturerEmployeePostGetBindingModel model) + { + using var context = DatabaseManager.GetContext; + var entity = context.LecturerEmployeePosts.FirstOrDefault(x => x.Id == model.Id); + if (entity == null) + { + return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); + } + else if (entity.IsDeleted) + { + return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); + } + entity.IsDeleted = true; + entity.DateDelete = DateTime.Now; + + context.SaveChanges(); + + return OperationResultModel.Success(true); + } + + public OperationResultModel Read(LecturerEmployeePostGetBindingModel model) + { + int countPages = 0; + using var context = DatabaseManager.GetContext; + + // для одной записи + if (model.Id.HasValue) + { + var entity = context.LecturerEmployeePosts.FirstOrDefault(x => x.Id == model.Id.Value); + if (entity == null) + { + return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); + } + return OperationResultModel.Success(Mapper.MapToClass(entity, model.HaveRight)); + } + + var query = context.LecturerEmployeePosts.Where(x => !x.IsDeleted).AsQueryable(); + if (model.EmployeePostId.HasValue) + { + query = query.Where(x => x.EmployeePostId == model.EmployeePostId); + } + if (model.LecturerId.HasValue) + { + query = query.Where(x => x.LecturerId == model.LecturerId); + } + + query = query.OrderBy(x => x.Lecturer.LastName); + + if (model.PageNumber.HasValue && model.PageSize.HasValue) + { + countPages = (int)Math.Ceiling((double)query.Count() / model.PageSize.Value); + query = query + .Skip(model.PageSize.Value * model.PageNumber.Value) + .Take(model.PageSize.Value); + } + + query = query.Include(x => x.EmployeePost).Include(x => x.Lecturer); + + var result = new LecturerEmployeePostListViewModel + { + MaxCount = countPages, + List = query.Select(x => Mapper.MapToClass(x, model.HaveRight)).ToList() + }; + + return OperationResultModel.Success(result); + } + + public OperationResultModel Update(LecturerEmployeePostSetBindingModel model) + { + using var context = DatabaseManager.GetContext; + + var entity = context.LecturerEmployeePosts.FirstOrDefault(x => x.Id == model.Id); + if (entity == null) + { + return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); + } + else if (entity.IsDeleted) + { + return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); + } + entity = Mapper.MapToClass(model, entity, true); + + context.SaveChanges(); + + return OperationResultModel.Success(Mapper.MapToClass(entity, true)); + } + } +} \ No newline at end of file diff --git a/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/LecturerPostService.cs b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/LecturerPostService.cs new file mode 100644 index 0000000..4189b04 --- /dev/null +++ b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/LecturerPostService.cs @@ -0,0 +1,128 @@ +using DatabaseCore; +using DatabaseCore.Models.Department; +using DepartmentBusinessLogic.BindingModels; +using DepartmentBusinessLogic.Interfaces; +using DepartmentBusinessLogic.ViewModels; +using ModuleTools.BusinessLogics; +using ModuleTools.Enums; +using ModuleTools.Models; +using System; +using System.Linq; + +namespace DepartmentDatabaseImplementation.Implementations +{ + /// + /// Реализация ILecturerPostService + /// + public class LecturerPostService : ILecturerPostService + { + public OperationResultModel Create(LecturerPostSetBindingModel model) + { + using var context = DatabaseManager.GetContext; + + var exsistEntity = context.LecturerPosts.FirstOrDefault(x => x.LecturerPostName == model.LecturerPostName); + if (exsistEntity == null) + { + var entity = Mapper.MapToClass(model, true); + context.LecturerPosts.Add(entity); + context.SaveChanges(); + return OperationResultModel.Success(Mapper.MapToClass(entity, true)); + } + else + { + if (exsistEntity.IsDeleted) + { + exsistEntity = Mapper.MapToClass(model, exsistEntity, true); + exsistEntity.IsDeleted = false; + context.SaveChanges(); + return OperationResultModel.Success(Mapper.MapToClass(exsistEntity, true)); + } + else + { + return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem); + } + } + } + + public OperationResultModel Delete(LecturerPostGetBindingModel model) + { + using var context = DatabaseManager.GetContext; + var entity = context.LecturerPosts.FirstOrDefault(x => x.Id == model.Id); + if (entity == null) + { + return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); + } + else if (entity.IsDeleted) + { + return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); + } + if (context.Lecturers.Any(x => x.LecturerPostId == model.Id && !x.IsDeleted)) + { + return OperationResultModel.Error("Error:", "Существуют преподаватели с этой должностью", ResultServiceStatusCode.ExsistItem); + } + entity.IsDeleted = true; + entity.DateDelete = DateTime.Now; + + context.SaveChanges(); + + return OperationResultModel.Success(true); + } + + public OperationResultModel Read(LecturerPostGetBindingModel model) + { + int countPages = 0; + using var context = DatabaseManager.GetContext; + + // для одной записи + if (model.Id.HasValue) + { + var entity = context.LecturerPosts.FirstOrDefault(x => x.Id == model.Id.Value); + if (entity == null) + { + return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); + } + return OperationResultModel.Success(Mapper.MapToClass(entity, model.HaveRight)); + } + + var query = context.LecturerPosts.Where(x => !x.IsDeleted).AsQueryable(); + + query = query.OrderBy(x => x.Order); + + if (model.PageNumber.HasValue && model.PageSize.HasValue) + { + countPages = (int)Math.Ceiling((double)query.Count() / model.PageSize.Value); + query = query + .Skip(model.PageSize.Value * model.PageNumber.Value) + .Take(model.PageSize.Value); + } + + var result = new LecturerPostListViewModel + { + MaxCount = countPages, + List = query.Select(x => Mapper.MapToClass(x, model.HaveRight)).ToList() + }; + + return OperationResultModel.Success(result); + } + + public OperationResultModel Update(LecturerPostSetBindingModel model) + { + using var context = DatabaseManager.GetContext; + + var entity = context.LecturerPosts.FirstOrDefault(x => x.Id == model.Id); + if (entity == null) + { + return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); + } + else if (entity.IsDeleted) + { + return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); + } + entity = Mapper.MapToClass(model, entity, true); + + context.SaveChanges(); + + return OperationResultModel.Success(Mapper.MapToClass(entity, true)); + } + } +} \ No newline at end of file diff --git a/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/LecturerService.cs b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/LecturerService.cs new file mode 100644 index 0000000..901219c --- /dev/null +++ b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/LecturerService.cs @@ -0,0 +1,168 @@ +using DatabaseCore; +using DatabaseCore.Models.Department; +using DepartmentBusinessLogic.BindingModels; +using DepartmentBusinessLogic.Interfaces; +using DepartmentBusinessLogic.ViewModels; +using Microsoft.EntityFrameworkCore; +using ModuleTools.BusinessLogics; +using ModuleTools.Enums; +using ModuleTools.Models; +using System; +using System.Linq; + +namespace DepartmentDatabaseImplementation.Implementations +{ + /// + /// Реализация ILecturerService + /// + public class LecturerService : ILecturerService + { + public OperationResultModel Create(LecturerSetBindingModel model) + { + using var context = DatabaseManager.GetContext; + + var exsistEntity = context.Lecturers.FirstOrDefault(x => x.LastName == model.LastName && x.FirstName == model.FirstName && + x.Patronymic == model.Patronymic); + if (exsistEntity == null) + { + var entity = Mapper.MapToClass(model, true); + context.Lecturers.Add(entity); + context.SaveChanges(); + return OperationResultModel.Success(Mapper.MapToClass(entity, true)); + } + else + { + if (exsistEntity.IsDeleted) + { + exsistEntity = Mapper.MapToClass(model, exsistEntity, true); + exsistEntity.IsDeleted = false; + context.SaveChanges(); + return OperationResultModel.Success(Mapper.MapToClass(exsistEntity, true)); + } + else + { + return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem); + } + } + } + + public OperationResultModel Delete(LecturerGetBindingModel model) + { + using var context = DatabaseManager.GetContext; + using var transaction = context.Database.BeginTransaction(); + try + { + var entity = context.Lecturers.Include(x => x.User).FirstOrDefault(x => x.Id == model.Id); + if (entity == null) + { + return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); + } + else if (entity.IsDeleted) + { + return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); + } + else if (entity.User == null) + { + return OperationResultModel.Error("Error:", "Не найден пользователь от сотрудника", ResultServiceStatusCode.NotFound); + } + entity.IsDeleted = true; + entity.DateDelete = DateTime.Now; + entity.User.IsDeleted = true; + entity.User.DateDelete = DateTime.Now; + + context.SaveChanges(); + + var links = context.LecturerEmployeePosts.Where(x => x.LecturerId == model.Id); + foreach (var link in links) + { + link.IsDeleted = true; + link.DateDelete = DateTime.Now; + } + context.SaveChanges(); + + + transaction.Commit(); + } + catch (Exception) + { + transaction.Rollback(); + throw; + } + + return OperationResultModel.Success(true); + } + + public OperationResultModel Read(LecturerGetBindingModel model) + { + int countPages = 0; + using var context = DatabaseManager.GetContext; + + // для одной записи + if (model.Id.HasValue) + { + var entity = context.Lecturers.FirstOrDefault(x => x.Id == model.Id.Value); + if (entity == null) + { + return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); + } + return OperationResultModel.Success(Mapper.MapToClass(entity, model.HaveRight)); + } + + var query = context.Lecturers.Where(x => !x.IsDeleted).AsQueryable(); + if(model.UserId.HasValue) + { + query = query.Where(x => x.UserId == model.UserId); + } + if (model.LecturerAcademicDegreeId.HasValue) + { + query = query.Where(x => x.LecturerAcademicDegreeId == model.LecturerAcademicDegreeId); + } + if (model.LecturerAcademicRankId.HasValue) + { + query = query.Where(x => x.LecturerAcademicRankId == model.LecturerAcademicRankId); + } + if (model.LecturerPostId.HasValue) + { + query = query.Where(x => x.LecturerPostId == model.LecturerPostId); + } + + query = query.OrderBy(x => x.LastName); + + if (model.PageNumber.HasValue && model.PageSize.HasValue) + { + countPages = (int)Math.Ceiling((double)query.Count() / model.PageSize.Value); + query = query + .Skip(model.PageSize.Value * model.PageNumber.Value) + .Take(model.PageSize.Value); + } + + var result = new LecturerListViewModel + { + MaxCount = countPages, + List = query.Select(x => Mapper.MapToClass(x, model.HaveRight)).ToList() + }; + + return OperationResultModel.Success(result); + } + + public OperationResultModel Update(LecturerSetBindingModel model) + { + using var context = DatabaseManager.GetContext; + + var entity = context.Lecturers.FirstOrDefault(x => x.Id == model.Id); + if (entity == null) + { + return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); + } + else if (entity.IsDeleted) + { + return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); + } + entity = Mapper.MapToClass(model, entity, true); + + context.SaveChanges(); + + return OperationResultModel.Success(Mapper.MapToClass(entity, true)); + } + } +} \ No newline at end of file diff --git a/DepartmentPortal/Security/SecurityDatabaseImplementation/Implementations/RoleService.cs b/DepartmentPortal/Security/SecurityDatabaseImplementation/Implementations/RoleService.cs index 84fc280..a6a6367 100644 --- a/DepartmentPortal/Security/SecurityDatabaseImplementation/Implementations/RoleService.cs +++ b/DepartmentPortal/Security/SecurityDatabaseImplementation/Implementations/RoleService.cs @@ -59,6 +59,11 @@ namespace SecurityDatabaseImplementation.Implementations { return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); } + if(context.UserRoles.Any(x => x.RoleId == model.Id && !x.IsDeleted)) + { + return OperationResultModel.Error("Error:", "Существуют пользователи, у которых есть эта роль", ResultServiceStatusCode.ExsistItem); + } + entity.IsDeleted = true; entity.DateDelete = DateTime.Now; @@ -72,14 +77,6 @@ namespace SecurityDatabaseImplementation.Implementations } context.SaveChanges(); - var users = context.UserRoles.Where(x => x.RoleId == model.Id); - foreach (var u in users) - { - u.IsDeleted = true; - u.DateDelete = DateTime.Now; - } - context.SaveChanges(); - transaction.Commit(); } catch(Exception) diff --git a/DepartmentPortal/Security/SecurityDatabaseImplementation/Implementations/UserService.cs b/DepartmentPortal/Security/SecurityDatabaseImplementation/Implementations/UserService.cs index 9e99680..4e42e28 100644 --- a/DepartmentPortal/Security/SecurityDatabaseImplementation/Implementations/UserService.cs +++ b/DepartmentPortal/Security/SecurityDatabaseImplementation/Implementations/UserService.cs @@ -66,6 +66,16 @@ namespace SecurityDatabaseImplementation.Implementations { return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); } + + if (context.Employees.Any(x => x.UserId == model.Id && !x.IsDeleted)) + { + return OperationResultModel.Error("Error:", "Имеется сотрудник, приязанный к этой учетке", ResultServiceStatusCode.ExsistItem); + } + if (context.Lecturers.Any(x => x.UserId == model.Id && !x.IsDeleted)) + { + return OperationResultModel.Error("Error:", "Имеется преподаватель, приязанный к этой учетке", ResultServiceStatusCode.ExsistItem); + } + entity.IsDeleted = true; entity.DateDelete = DateTime.Now;