forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
184 lines
6.3 KiB
C#
184 lines
6.3 KiB
C#
using AutoMapper;
|
|
using MagicCarpetContracts.DataModels;
|
|
using MagicCarpetContracts.Exceptions;
|
|
using MagicCarpetContracts.StoragesContracts;
|
|
using MagicCarpetDatabase.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MagicCarpetDatabase.Implementations;
|
|
|
|
public class EmployeeStorageContract : IEmployeeStorageContract
|
|
{
|
|
private readonly MagicCarpetDbContext _dbContext;
|
|
private readonly Mapper _mapper;
|
|
|
|
public EmployeeStorageContract(MagicCarpetDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<Post, PostDataModel>()
|
|
.ForMember(x => x.Id, x => x.MapFrom(src => src.PostId));
|
|
cfg.CreateMap<Employee, EmployeeDataModel>();
|
|
cfg.CreateMap<EmployeeDataModel, Employee>();
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
|
|
public List<EmployeeDataModel> GetList(bool onlyActive = true, string? postId = null, DateTime? fromBirthDate = null, DateTime? toBirthDate = null, DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null)
|
|
{
|
|
try
|
|
{
|
|
var query = _dbContext.Employees.AsQueryable();
|
|
if (onlyActive)
|
|
{
|
|
query = query.Where(x => !x.IsDeleted);
|
|
}
|
|
if (postId is not null)
|
|
{
|
|
query = query.Where(x => x.PostId == postId);
|
|
}
|
|
if (fromBirthDate is not null && toBirthDate is not null)
|
|
{
|
|
query = query.Where(x => x.BirthDate >= DateTime.SpecifyKind(fromBirthDate ?? DateTime.UtcNow, DateTimeKind.Utc) && x.BirthDate <= DateTime.SpecifyKind(toBirthDate ?? DateTime.UtcNow, DateTimeKind.Utc));
|
|
}
|
|
if (fromEmploymentDate is not null && toEmploymentDate is not null)
|
|
{
|
|
query = query.Where(x => x.EmploymentDate >= DateTime.SpecifyKind(fromEmploymentDate ?? DateTime.UtcNow, DateTimeKind.Utc) && x.EmploymentDate <= DateTime.SpecifyKind(toEmploymentDate ?? DateTime.UtcNow, DateTimeKind.Utc));
|
|
}
|
|
return [.. JoinPost(query).Select(x => _mapper.Map<EmployeeDataModel>(x))];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public EmployeeDataModel? GetElementById(string id)
|
|
{
|
|
try
|
|
{
|
|
return _mapper.Map<EmployeeDataModel>(GetEmployeeById(id));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public EmployeeDataModel? GetElementByFIO(string fio)
|
|
{
|
|
try
|
|
{
|
|
return _mapper.Map<EmployeeDataModel>(AddPost(_dbContext.Employees.FirstOrDefault(x => x.FIO == fio && !x.IsDeleted)));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public EmployeeDataModel? GetElementByEmail(string email)
|
|
{
|
|
try
|
|
{
|
|
return _mapper.Map<EmployeeDataModel>(AddPost(_dbContext.Employees.FirstOrDefault(x => x.Email == email && !x.IsDeleted)));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public void AddElement(EmployeeDataModel employeeDataModel)
|
|
{
|
|
try
|
|
{
|
|
_dbContext.Employees.Add(_mapper.Map<Employee>(employeeDataModel));
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("Id", employeeDataModel.Id);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public void UpdElement(EmployeeDataModel employeeDataModel)
|
|
{
|
|
try
|
|
{
|
|
var element = GetEmployeeById(employeeDataModel.Id) ?? throw new ElementNotFoundException(employeeDataModel.Id);
|
|
_dbContext.Employees.Update(_mapper.Map(employeeDataModel, element));
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (ElementNotFoundException)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public void DelElement(string id)
|
|
{
|
|
try
|
|
{
|
|
var element = GetEmployeeById(id) ?? throw new ElementNotFoundException(id);
|
|
element.IsDeleted = true;
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (ElementNotFoundException)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public int GetEmployeeTrend(DateTime fromPeriod, DateTime toPeriod)
|
|
{
|
|
try
|
|
{
|
|
var countWorkersOnBegining = _dbContext.Employees.Count(x => x.EmploymentDate < fromPeriod && (!x.IsDeleted || x.DateOfDelete > fromPeriod));
|
|
var countWorkersOnEnding = _dbContext.Employees.Count(x => x.EmploymentDate < toPeriod && (!x.IsDeleted || x.DateOfDelete > toPeriod));
|
|
return countWorkersOnEnding - countWorkersOnBegining;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
private Employee? GetEmployeeById(string id) => AddPost(_dbContext.Employees.FirstOrDefault(x => x.Id == id && !x.IsDeleted));
|
|
|
|
private IQueryable<Employee> JoinPost(IQueryable<Employee> query)
|
|
=> query.GroupJoin(_dbContext.Posts.Where(x => x.IsActual), x => x.PostId, y => y.PostId, (x, y) => new { Employee = x, Post = y })
|
|
.SelectMany(xy => xy.Post.DefaultIfEmpty(), (x, y) => x.Employee.AddPost(y));
|
|
|
|
private Employee? AddPost(Employee? employee)
|
|
=> employee?.AddPost(_dbContext.Posts.FirstOrDefault(x => x.PostId == employee.PostId && x.IsActual));
|
|
}
|