Реализация сервисов для сотрудников

This commit is contained in:
kotcheshir73 2021-04-03 10:02:32 +04:00
parent 6c835fd8e5
commit 89affe64e3
7 changed files with 467 additions and 0 deletions

View File

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\Common\DatabaseCore\DatabaseCore.csproj" />
<ProjectReference Include="..\DepartmentBusinessLogic\DepartmentBusinessLogic.csproj" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="copy /Y &quot;$(TargetDir)*.dll&quot; &quot;$(SolutionDir)ImplementationExtensions\*.dll&quot;" />
</Target>
</Project>

View File

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>

View File

@ -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<IEmployeePostService, EmployeePostService>();
DependencyManager.Instance.RegisterType<IEmployeeService, EmployeeService>();
DependencyManager.Instance.RegisterType<IEmployeeEmployeePostService, EmployeeEmployeePostService>();
}
}
}

View File

@ -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
{
/// <summary>
/// Реализация IEmployeeEmployeePostService
/// </summary>
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<EmployeeEmployeePostSetBindingModel, EmployeeEmployeePost>(model, true);
context.EmployeeEmployeePosts.Add(entity);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<EmployeeEmployeePost, EmployeeEmployeePostViewModel>(entity, true));
}
else
{
if (exsistEntity.IsDeleted)
{
exsistEntity.IsDeleted = false;
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<EmployeeEmployeePost, EmployeeEmployeePostViewModel>(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<EmployeeEmployeePost, EmployeeEmployeePostViewModel>(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<EmployeeEmployeePost, EmployeeEmployeePostViewModel>(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<EmployeeEmployeePost, EmployeeEmployeePostViewModel>(entity, true));
}
}
}

View File

@ -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
{
/// <summary>
/// Реализация IEmployeePostService
/// </summary>
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<EmployeePostSetBindingModel, EmployeePost>(model, true);
context.EmployeePosts.Add(entity);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<EmployeePost, EmployeePostViewModel>(entity, true));
}
else
{
if (exsistEntity.IsDeleted)
{
exsistEntity.IsDeleted = false;
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<EmployeePost, EmployeePostViewModel>(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<EmployeePost, EmployeePostViewModel>(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<EmployeePost, EmployeePostViewModel>(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<EmployeePostSetBindingModel, EmployeePost>(model, entity, true);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<EmployeePost, EmployeePostViewModel>(entity, true));
}
}
}

View File

@ -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
{
/// <summary>
/// Реализация IEmployeeService
/// </summary>
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<EmployeeSetBindingModel, Employee>(model, true);
context.Employees.Add(entity);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Employee, EmployeeViewModel>(entity, true));
}
else
{
if (exsistEntity.IsDeleted)
{
exsistEntity.IsDeleted = false;
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Employee, EmployeeViewModel>(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<Employee, EmployeeViewModel>(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<Employee, EmployeeViewModel>(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<Employee, EmployeeViewModel>(entity, true));
}
}
}

View File

@ -25,6 +25,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Department", "Department",
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DepartmentBusinessLogic", "Department\DepartmentBusinessLogic\DepartmentBusinessLogic.csproj", "{69B94DB1-D1BE-4905-81AC-A5D49D0C9719}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DepartmentBusinessLogic", "Department\DepartmentBusinessLogic\DepartmentBusinessLogic.csproj", "{69B94DB1-D1BE-4905-81AC-A5D49D0C9719}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DepartmentDatabaseImplementation", "Department\DepartmentDatabaseImplementation.csproj\DepartmentDatabaseImplementation.csproj", "{1A9E20FE-6324-4839-A3AD-5726305122A5}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{69B94DB1-D1BE-4905-81AC-A5D49D0C9719}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -75,6 +81,7 @@ Global
{26FCAAC6-F8F4-4CE6-92FE-1C6C376E5DE6} = {7DA26C36-778E-4563-9AEC-966E26EA7B2A} {26FCAAC6-F8F4-4CE6-92FE-1C6C376E5DE6} = {7DA26C36-778E-4563-9AEC-966E26EA7B2A}
{84CD6D9F-9B38-4392-B8A5-4FD32CF54BEC} = {6F154F8D-3437-45EE-9D89-02B96BDF3E8E} {84CD6D9F-9B38-4392-B8A5-4FD32CF54BEC} = {6F154F8D-3437-45EE-9D89-02B96BDF3E8E}
{69B94DB1-D1BE-4905-81AC-A5D49D0C9719} = {A19E7709-6AD8-4E9B-B3AB-4339C67D9F39} {69B94DB1-D1BE-4905-81AC-A5D49D0C9719} = {A19E7709-6AD8-4E9B-B3AB-4339C67D9F39}
{1A9E20FE-6324-4839-A3AD-5726305122A5} = {A19E7709-6AD8-4E9B-B3AB-4339C67D9F39}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FBA0CB49-EF2D-4538-9D00-FCEDA24879A9} SolutionGuid = {FBA0CB49-EF2D-4538-9D00-FCEDA24879A9}