135 lines
4.5 KiB
C#
135 lines
4.5 KiB
C#
|
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));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|