160 lines
5.1 KiB
C#
160 lines
5.1 KiB
C#
using AutoMapper;
|
|
using SmallSoftwareContracts.DataModels;
|
|
using SmallSoftwareContracts.Exceptions;
|
|
using SmallSoftwareContracts.StoragesContracts;
|
|
using SmallSoftwareDatabase.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SmallSoftwareDatabase.Implementations;
|
|
|
|
internal class WorkerStorageContract : IWorkerStorageContract
|
|
{
|
|
private readonly SmallSoftwareDbContext _dbContext;
|
|
private readonly Mapper _mapper;
|
|
public WorkerStorageContract(SmallSoftwareDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<Post, PostDataModel>()
|
|
.ForMember(x => x.Id, x => x.MapFrom(src =>
|
|
src.PostId));
|
|
cfg.CreateMap<Worker, WorkerDataModel>();
|
|
cfg.CreateMap<WorkerDataModel, Worker>()
|
|
.ForMember(x => x.Post, x => x.Ignore());
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
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 => _mapper.Map<WorkerDataModel>(x))];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
public WorkerDataModel? GetElementById(string id)
|
|
{
|
|
try
|
|
{
|
|
return _mapper.Map<WorkerDataModel>(GetWorkerById(id));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
public WorkerDataModel? GetElementByFIO(string fio)
|
|
{
|
|
try
|
|
{
|
|
return
|
|
_mapper.Map<WorkerDataModel>(AddPost(_dbContext.Workers.FirstOrDefault(x => x.FIO == fio && !x.IsDeleted)));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
public void AddElement(WorkerDataModel workerDataModel)
|
|
{
|
|
try
|
|
{
|
|
_dbContext.Workers.Add(_mapper.Map<Worker>(workerDataModel));
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("Id", workerDataModel.Id);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
public void UpdElement(WorkerDataModel workerDataModel)
|
|
{
|
|
try
|
|
{
|
|
var element = GetWorkerById(workerDataModel.Id) ?? throw new
|
|
ElementNotFoundException(workerDataModel.Id);
|
|
_dbContext.Workers.Update(_mapper.Map(workerDataModel,
|
|
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 = GetWorkerById(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);
|
|
}
|
|
}
|
|
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));
|
|
} |