diff --git a/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/DepartmentDatabaseImplementation.csproj b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/DepartmentDatabaseImplementation.csproj new file mode 100644 index 0000000..3c7192e --- /dev/null +++ b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/DepartmentDatabaseImplementation.csproj @@ -0,0 +1,16 @@ + + + + net5.0 + + + + + + + + + + + + diff --git a/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/DepartmentDatabaseImplementation.csproj.csproj b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/DepartmentDatabaseImplementation.csproj.csproj new file mode 100644 index 0000000..f208d30 --- /dev/null +++ b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/DepartmentDatabaseImplementation.csproj.csproj @@ -0,0 +1,7 @@ + + + + net5.0 + + + diff --git a/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/DepartmentImplementationExtensions.cs b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/DepartmentImplementationExtensions.cs new file mode 100644 index 0000000..6455c6f --- /dev/null +++ b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/DepartmentImplementationExtensions.cs @@ -0,0 +1,17 @@ +using DepartmentBusinessLogic.Interfaces; +using DepartmentDatabaseImplementation.Implementations; +using ModuleTools.BusinessLogics; +using ModuleTools.Interfaces; + +namespace DepartmentDatabaseImplementation +{ + public class DepartmentImplementationExtensions : IImplementationExtension + { + public void RegisterServices() + { + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + } + } +} \ No newline at end of file diff --git a/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/EmployeeEmployeePostService.cs b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/EmployeeEmployeePostService.cs new file mode 100644 index 0000000..5d9b80a --- /dev/null +++ b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/EmployeeEmployeePostService.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 +{ + /// + /// Реализация IEmployeeEmployeePostService + /// + public class EmployeeEmployeePostService : IEmployeeEmployeePostService + { + public OperationResultModel Create(EmployeeEmployeePostSetBindingModel model) + { + using var context = DatabaseManager.GetContext; + + var exsistEntity = context.EmployeeEmployeePosts.FirstOrDefault(x => x.EmployeeId == model.EmployeeId && x.EmployeePostId == model.EmployeePostId); + if (exsistEntity == null) + { + var entity = Mapper.MapToClass(model, true); + context.EmployeeEmployeePosts.Add(entity); + context.SaveChanges(); + return OperationResultModel.Success(Mapper.MapToClass(entity, true)); + } + else + { + if (exsistEntity.IsDeleted) + { + exsistEntity.IsDeleted = false; + context.SaveChanges(); + return OperationResultModel.Success(Mapper.MapToClass(exsistEntity, true)); + } + else + { + return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem); + } + } + } + + public OperationResultModel Delete(EmployeeEmployeePostGetBindingModel model) + { + using var context = DatabaseManager.GetContext; + + var entity = context.EmployeeEmployeePosts.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(EmployeeEmployeePostGetBindingModel model) + { + int countPages = 0; + using var context = DatabaseManager.GetContext; + + // для одной записи + if (model.Id.HasValue) + { + var entity = context.EmployeeEmployeePosts.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.EmployeeEmployeePosts.Where(x => !x.IsDeleted).AsQueryable(); + if (model.EmployeePostId.HasValue) + { + query = query.Where(x => x.EmployeePostId == model.EmployeePostId); + } + if (model.EmployeeId.HasValue) + { + query = query.Where(x => x.EmployeeId == model.EmployeeId); + } + + query = query.OrderBy(x => x.Employee.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.Employee); + + var result = new EmployeeEmployeePostListViewModel + { + MaxCount = countPages, + List = query.Select(x => Mapper.MapToClass(x, model.HaveRight)).ToList() + }; + + return OperationResultModel.Success(result); + } + + public OperationResultModel Update(EmployeeEmployeePostSetBindingModel model) + { + using var context = DatabaseManager.GetContext; + + var entity = context.EmployeeEmployeePosts.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/EmployeePostService.cs b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/EmployeePostService.cs new file mode 100644 index 0000000..220f81c --- /dev/null +++ b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/EmployeePostService.cs @@ -0,0 +1,142 @@ +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 +{ + /// + /// Реализация IEmployeePostService + /// + public class EmployeePostService : IEmployeePostService + { + public OperationResultModel Create(EmployeePostSetBindingModel model) + { + using var context = DatabaseManager.GetContext; + + var exsistEntity = context.EmployeePosts.FirstOrDefault(x => x.EmployeePostName == model.EmployeePostName); + if (exsistEntity == null) + { + var entity = Mapper.MapToClass(model, true); + context.EmployeePosts.Add(entity); + context.SaveChanges(); + return OperationResultModel.Success(Mapper.MapToClass(entity, true)); + } + else + { + if (exsistEntity.IsDeleted) + { + exsistEntity.IsDeleted = false; + context.SaveChanges(); + return OperationResultModel.Success(Mapper.MapToClass(exsistEntity, true)); + } + else + { + return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem); + } + } + } + + 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) + { + 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(); + } + catch (Exception) + { + transaction.Rollback(); + throw; + } + + return OperationResultModel.Success(true); + } + + public OperationResultModel Read(EmployeePostGetBindingModel model) + { + int countPages = 0; + using var context = DatabaseManager.GetContext; + + // для одной записи + if (model.Id.HasValue) + { + var entity = context.EmployeePosts.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.EmployeePosts.Where(x => !x.IsDeleted).AsQueryable(); + + query = query.OrderBy(x => x.EmployeePostName); + + 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 EmployeePostListViewModel + { + MaxCount = countPages, + List = query.Select(x => Mapper.MapToClass(x, model.HaveRight)).ToList() + }; + + return OperationResultModel.Success(result); + } + + public OperationResultModel Update(EmployeePostSetBindingModel model) + { + using var context = DatabaseManager.GetContext; + + 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 = 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/EmployeeService.cs b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/EmployeeService.cs new file mode 100644 index 0000000..dd34db9 --- /dev/null +++ b/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/EmployeeService.cs @@ -0,0 +1,143 @@ +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 +{ + /// + /// Реализация IEmployeeService + /// + public class EmployeeService : IEmployeeService + { + public OperationResultModel Create(EmployeeSetBindingModel model) + { + using var context = DatabaseManager.GetContext; + + var exsistEntity = context.Employees.FirstOrDefault(x => x.LastName == model.LastName && x.FirstName == model.FirstName && + x.Patronymic == model.Patronymic); + if (exsistEntity == null) + { + var entity = Mapper.MapToClass(model, true); + context.Employees.Add(entity); + context.SaveChanges(); + return OperationResultModel.Success(Mapper.MapToClass(entity, true)); + } + else + { + if (exsistEntity.IsDeleted) + { + exsistEntity.IsDeleted = false; + context.SaveChanges(); + return OperationResultModel.Success(Mapper.MapToClass(exsistEntity, true)); + } + else + { + return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem); + } + } + } + + public OperationResultModel Delete(EmployeeGetBindingModel model) + { + using var context = DatabaseManager.GetContext; + using var transaction = context.Database.BeginTransaction(); + try + { + var entity = context.Employees.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.EmployeeId == 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(EmployeeGetBindingModel model) + { + int countPages = 0; + using var context = DatabaseManager.GetContext; + + // для одной записи + if (model.Id.HasValue) + { + var entity = context.Employees.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.Employees.Where(x => !x.IsDeleted).AsQueryable(); + + 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 EmployeeListViewModel + { + MaxCount = countPages, + List = query.Select(x => Mapper.MapToClass(x, model.HaveRight)).ToList() + }; + + return OperationResultModel.Success(result); + } + + public OperationResultModel Update(EmployeeSetBindingModel model) + { + using var context = DatabaseManager.GetContext; + + var entity = context.Employees.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/DepartmentPortal.sln b/DepartmentPortal/DepartmentPortal.sln index 713c797..8dfa719 100644 --- a/DepartmentPortal/DepartmentPortal.sln +++ b/DepartmentPortal/DepartmentPortal.sln @@ -25,6 +25,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Department", "Department", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DepartmentBusinessLogic", "Department\DepartmentBusinessLogic\DepartmentBusinessLogic.csproj", "{69B94DB1-D1BE-4905-81AC-A5D49D0C9719}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DepartmentDatabaseImplementation", "Department\DepartmentDatabaseImplementation.csproj\DepartmentDatabaseImplementation.csproj", "{1A9E20FE-6324-4839-A3AD-5726305122A5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -63,6 +65,10 @@ Global {69B94DB1-D1BE-4905-81AC-A5D49D0C9719}.Debug|Any CPU.Build.0 = Debug|Any CPU {69B94DB1-D1BE-4905-81AC-A5D49D0C9719}.Release|Any CPU.ActiveCfg = Release|Any CPU {69B94DB1-D1BE-4905-81AC-A5D49D0C9719}.Release|Any CPU.Build.0 = Release|Any CPU + {1A9E20FE-6324-4839-A3AD-5726305122A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1A9E20FE-6324-4839-A3AD-5726305122A5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1A9E20FE-6324-4839-A3AD-5726305122A5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1A9E20FE-6324-4839-A3AD-5726305122A5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -75,6 +81,7 @@ Global {26FCAAC6-F8F4-4CE6-92FE-1C6C376E5DE6} = {7DA26C36-778E-4563-9AEC-966E26EA7B2A} {84CD6D9F-9B38-4392-B8A5-4FD32CF54BEC} = {6F154F8D-3437-45EE-9D89-02B96BDF3E8E} {69B94DB1-D1BE-4905-81AC-A5D49D0C9719} = {A19E7709-6AD8-4E9B-B3AB-4339C67D9F39} + {1A9E20FE-6324-4839-A3AD-5726305122A5} = {A19E7709-6AD8-4E9B-B3AB-4339C67D9F39} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {FBA0CB49-EF2D-4538-9D00-FCEDA24879A9}