168 lines
5.9 KiB
C#
168 lines
5.9 KiB
C#
using AndDietCokeContracts.DataModels;
|
|
using AndDietCokeContracts.Exceptions;
|
|
using AndDietCokeContracts.Mapper;
|
|
using AndDietCokeContracts.Resources;
|
|
using AndDietCokeContracts.StoragesContracts;
|
|
using AndDietCokeDatabase.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Localization;
|
|
using Npgsql;
|
|
using System.Data;
|
|
|
|
namespace AndDietCokeDatabase.Implementations;
|
|
|
|
internal class WorkerStorageContract : IWorkerStorageContract
|
|
{
|
|
private readonly AndDietCokeDbContext _dbContext;
|
|
private readonly IStringLocalizer<Messages> _localizer;
|
|
public WorkerStorageContract(AndDietCokeDbContext dbContext, IStringLocalizer<Messages> localizer)
|
|
{
|
|
_dbContext = dbContext;
|
|
_localizer = localizer;
|
|
}
|
|
|
|
public List<WorkerDataModel> GetList(bool onlyActive = true, string? postId = null, DateTime? fromBirthDate = null, DateTime? toBirthDate = null, DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null)
|
|
{
|
|
try
|
|
{
|
|
var query = _dbContext.Workers.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 >= fromBirthDate && x.BirthDate <= toBirthDate);
|
|
}
|
|
if (fromEmploymentDate is not null && toEmploymentDate is not null)
|
|
{
|
|
query = query.Where(x => x.EmploymentDate >= fromEmploymentDate && x.EmploymentDate <= toEmploymentDate);
|
|
}
|
|
return [.. JoinPost(query).Select(x => CustomMapper.MapObject<WorkerDataModel>(x))];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
|
|
public WorkerDataModel? GetElementById(string id)
|
|
{
|
|
try
|
|
{
|
|
return CustomMapper.MapObjectWithNull<WorkerDataModel>(GetWorkerById(id));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
|
|
public WorkerDataModel? GetElementByFIO(string fio)
|
|
{
|
|
try
|
|
{
|
|
return CustomMapper.MapObjectWithNull<WorkerDataModel>(AddPost(_dbContext.Workers.FirstOrDefault(x => x.FIO == fio && !x.IsDeleted)));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
|
|
public void AddElement(WorkerDataModel workerDataModel)
|
|
{
|
|
try
|
|
{
|
|
_dbContext.Workers.Add(CustomMapper.MapObject<Worker>(workerDataModel));
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("Id", workerDataModel.Id, _localizer);
|
|
}
|
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "PK_Workers" })
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("Id", workerDataModel.Id, _localizer);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
|
|
public int GetWorkerTrend(DateTime fromPeriod, DateTime toPeriod)
|
|
{
|
|
try
|
|
{
|
|
var countWorkersOnBegining = _dbContext.Workers.Count(x => x.EmploymentDate < fromPeriod && (!x.IsDeleted || x.DateOfDelete > fromPeriod));
|
|
var countWorkersOnEnding = _dbContext.Workers.Count(x => x.EmploymentDate < toPeriod && (!x.IsDeleted || x.DateOfDelete > toPeriod));
|
|
return countWorkersOnEnding - countWorkersOnBegining;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
|
|
public void UpdElement(WorkerDataModel workerDataModel)
|
|
{
|
|
try
|
|
{
|
|
var element = GetWorkerById(workerDataModel.Id) ?? throw new ElementNotFoundException(workerDataModel.Id, _localizer);
|
|
_dbContext.Workers.Update(CustomMapper.MapObject(workerDataModel, element));
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (ElementNotFoundException)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
|
|
public void DelElement(string id)
|
|
{
|
|
try
|
|
{
|
|
var element = GetWorkerById(id) ?? throw new ElementNotFoundException(id, _localizer);
|
|
element.IsDeleted = true;
|
|
element.DateOfDelete = DateTime.UtcNow;
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (ElementNotFoundException)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
private Worker? GetWorkerById(string id) => AddPost(_dbContext.Workers.FirstOrDefault(x => x.Id == id && !x.IsDeleted));
|
|
|
|
private IQueryable<Worker> JoinPost(IQueryable<Worker> query)
|
|
=> query.GroupJoin(_dbContext.Posts.Where(x => x.IsActual), x => x.PostId, y => y.PostId, (x, y) => new { Worker = x, Post = y })
|
|
.SelectMany(xy => xy.Post.DefaultIfEmpty(), (x, y) => x.Worker.AddPost(y));
|
|
|
|
private Worker? AddPost(Worker? worker)
|
|
=> worker?.AddPost(_dbContext.Posts.FirstOrDefault(x => x.PostId == worker.PostId && x.IsActual));
|
|
}
|