forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
Контракты
This commit is contained in:
@@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MagicCarpetContracts.Exceptions;
|
||||||
|
|
||||||
|
public class ElementDeletedException : Exception
|
||||||
|
{
|
||||||
|
public ElementDeletedException(string id) : base($"Cannot modify a deleted item (id: {id})") { }
|
||||||
|
}
|
||||||
@@ -0,0 +1,145 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using MagicCarpetContracts.DataModels;
|
||||||
|
using MagicCarpetContracts.Exceptions;
|
||||||
|
using MagicCarpetContracts.StoragesContracts;
|
||||||
|
using MagicCarpetDatabase.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Npgsql;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MagicCarpetDatabase.Implementations;
|
||||||
|
|
||||||
|
internal class ClientStorageContarct : IClientStorageContract
|
||||||
|
{
|
||||||
|
private readonly MagicCarpetDbContext _dbContext;
|
||||||
|
private readonly Mapper _mapper;
|
||||||
|
public ClientStorageContarct(MagicCarpetDbContext magicCarpetDbContext)
|
||||||
|
{
|
||||||
|
_dbContext = magicCarpetDbContext;
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.CreateMap<Client, ClientDataModel>();
|
||||||
|
cfg.CreateMap<ClientDataModel, Client>();
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
public List<ClientDataModel> GetList()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return [.. _dbContext.Clients.Select(x => _mapper.Map<ClientDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public ClientDataModel? GetElementById(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<ClientDataModel>(GetClientById(id));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public ClientDataModel? GetElementByFIO(string fio)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<ClientDataModel>(_dbContext.Clients.FirstOrDefault(x => x.FIO == fio));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public ClientDataModel? GetElementByPhoneNumber(string phoneNumber)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<ClientDataModel>(_dbContext.Clients.FirstOrDefault(x => x.PhoneNumber == phoneNumber));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void AddElement(ClientDataModel clientDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Clients.Add(_mapper.Map<Client>(clientDataModel));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("Id", clientDataModel.Id);
|
||||||
|
}
|
||||||
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Clients_PhoneNumber" })
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("PhoneNumber", clientDataModel.PhoneNumber);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void UpdElement(ClientDataModel clientDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetClientById(clientDataModel.Id) ?? throw new ElementNotFoundException(clientDataModel.Id);
|
||||||
|
_dbContext.Clients.Update(_mapper.Map(clientDataModel, element));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Clients_PhoneNumber" })
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("PhoneNumber", clientDataModel.PhoneNumber);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void DelElement(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetClientById(id) ?? throw new ElementNotFoundException(id);
|
||||||
|
_dbContext.Clients.Remove(element);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private Client? GetClientById(string id) => _dbContext.Clients.FirstOrDefault(x => x.Id == id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,161 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
internal class EmployeeStorageContract : IEmployeeStorageContract
|
||||||
|
{
|
||||||
|
private readonly MagicCarpetDbContext _dbContext;
|
||||||
|
private readonly Mapper _mapper;
|
||||||
|
|
||||||
|
public EmployeeStorageContract(MagicCarpetDbContext dbContext)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
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 >= fromBirthDate && x.BirthDate <= toBirthDate);
|
||||||
|
}
|
||||||
|
if (fromEmploymentDate is not null && toEmploymentDate is not null)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.EmploymentDate >= fromEmploymentDate && x.EmploymentDate <= toEmploymentDate);
|
||||||
|
}
|
||||||
|
return [.. 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>(_dbContext.Employees.FirstOrDefault(x => x.FIO == fio));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public EmployeeDataModel? GetElementByEmail(string email)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<EmployeeDataModel>(_dbContext.Employees.FirstOrDefault(x => x.Email == email));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddElement(EmployeeDataModel workerDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Employees.Add(_mapper.Map<Employee>(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(EmployeeDataModel workerDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetEmployeeById(workerDataModel.Id) ?? throw new ElementNotFoundException(workerDataModel.Id);
|
||||||
|
_dbContext.Employees.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 = 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Employee? GetEmployeeById(string id) => _dbContext.Employees.FirstOrDefault(x => x.Id == id && !x.IsDeleted);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,196 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using MagicCarpetContracts.DataModels;
|
||||||
|
using MagicCarpetContracts.Exceptions;
|
||||||
|
using MagicCarpetContracts.StoragesContracts;
|
||||||
|
using MagicCarpetDatabase.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Npgsql;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MagicCarpetDatabase.Implementations;
|
||||||
|
|
||||||
|
internal class PostStorageContract : IPostStorageContract
|
||||||
|
{
|
||||||
|
private readonly MagicCarpetDbContext _dbContext;
|
||||||
|
private readonly Mapper _mapper;
|
||||||
|
|
||||||
|
public PostStorageContract(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<PostDataModel, Post>()
|
||||||
|
.ForMember(x => x.Id, x => x.Ignore())
|
||||||
|
.ForMember(x => x.PostId, x => x.MapFrom(src => src.Id))
|
||||||
|
.ForMember(x => x.IsActual, x => x.MapFrom(src => true))
|
||||||
|
.ForMember(x => x.ChangeDate, x => x.MapFrom(src => DateTime.UtcNow));
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<PostDataModel> GetList(bool onlyActual = true)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var query = _dbContext.Posts.AsQueryable();
|
||||||
|
if (onlyActual)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.IsActual);
|
||||||
|
}
|
||||||
|
return [.. query.Select(x => _mapper.Map<PostDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<PostDataModel> GetPostWithHistory(string postId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return [.. _dbContext.Posts.Where(x => x.PostId == postId).Select(x => _mapper.Map<PostDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public PostDataModel? GetElementById(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<PostDataModel>(_dbContext.Posts.FirstOrDefault(x => x.PostId == id && x.IsActual));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public PostDataModel? GetElementByName(string name)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<PostDataModel>(_dbContext.Posts.FirstOrDefault(x => x.PostName == name && x.IsActual));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddElement(PostDataModel postDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Posts.Add(_mapper.Map<Post>(postDataModel));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostName_IsActual" })
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("PostName", postDataModel.PostName);
|
||||||
|
}
|
||||||
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostId_IsActual" })
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("PostId", postDataModel.Id);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdElement(PostDataModel postDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var transaction = _dbContext.Database.BeginTransaction();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetPostById(postDataModel.Id) ?? throw new ElementNotFoundException(postDataModel.Id);
|
||||||
|
if (!element.IsActual)
|
||||||
|
{
|
||||||
|
throw new ElementDeletedException(postDataModel.Id);
|
||||||
|
}
|
||||||
|
element.IsActual = false;
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
var newElement = _mapper.Map<Post>(postDataModel);
|
||||||
|
_dbContext.Posts.Add(newElement);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
transaction.Rollback();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostName_IsActual" })
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("PostName", postDataModel.PostName);
|
||||||
|
}
|
||||||
|
catch (Exception ex) when (ex is ElementDeletedException || ex is ElementNotFoundException)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DelElement(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetPostById(id) ?? throw new ElementNotFoundException(id);
|
||||||
|
if (!element.IsActual)
|
||||||
|
{
|
||||||
|
throw new ElementDeletedException(id);
|
||||||
|
}
|
||||||
|
element.IsActual = false;
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ResElement(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetPostById(id) ?? throw new ElementNotFoundException(id);
|
||||||
|
element.IsActual = true;
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Post? GetPostById(string id) => _dbContext.Posts.Where(x => x.PostId == id)
|
||||||
|
.OrderByDescending(x => x.ChangeDate).FirstOrDefault();
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
internal class SalaryStorageContract : ISalaryStorageContract
|
||||||
|
{
|
||||||
|
private readonly MagicCarpetDbContext _dbContext;
|
||||||
|
private readonly Mapper _mapper;
|
||||||
|
|
||||||
|
public SalaryStorageContract(MagicCarpetDbContext dbContext)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.CreateMap<Salary, SalaryDataModel>();
|
||||||
|
cfg.CreateMap<SalaryDataModel, Salary>()
|
||||||
|
.ForMember(dest => dest.EmployeeSalary, opt => opt.MapFrom(src => src.Salary));
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<SalaryDataModel> GetList(DateTime startDate, DateTime endDate, string? employeeId = null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var query = _dbContext.Salaries.Where(x => x.SalaryDate >= startDate && x.SalaryDate <= endDate);
|
||||||
|
if (employeeId is not null)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.EmployeeId == employeeId);
|
||||||
|
}
|
||||||
|
return [.. query.Select(x => _mapper.Map<SalaryDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddElement(SalaryDataModel salaryDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Salaries.Add(_mapper.Map<Salary>(salaryDataModel));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using MagicCarpetContracts.DataModels;
|
||||||
|
using MagicCarpetContracts.Exceptions;
|
||||||
|
using MagicCarpetDatabase.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MagicCarpetDatabase.Implementations;
|
||||||
|
|
||||||
|
internal class SaleStorageContract
|
||||||
|
{
|
||||||
|
private readonly MagicCarpetDbContext _dbContext;
|
||||||
|
private readonly Mapper _mapper;
|
||||||
|
|
||||||
|
public SaleStorageContract(MagicCarpetDbContext dbContext)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.CreateMap<SaleTour, SaleTourDataModel>();
|
||||||
|
cfg.CreateMap<SaleTourDataModel, SaleTour>();
|
||||||
|
cfg.CreateMap<Sale, SaleDataModel>();
|
||||||
|
cfg.CreateMap<SaleDataModel, Sale>()
|
||||||
|
.ForMember(x => x.IsCancel, x => x.MapFrom(src => false))
|
||||||
|
.ForMember(x => x.SaleTours, x => x.MapFrom(src => src.Tours));
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<SaleDataModel> GetList(DateTime? startDate = null, DateTime? endDate = null, string? employeeId = null, string? clientId = null, string? tourId = null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var query = _dbContext.Sales.Include(x => x.SaleTours).AsQueryable();
|
||||||
|
if (startDate is not null && endDate is not null)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.SaleDate >= startDate && x.SaleDate < endDate);
|
||||||
|
}
|
||||||
|
if (employeeId is not null)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.EmployeeId == employeeId);
|
||||||
|
}
|
||||||
|
if (clientId is not null)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.ClientId == clientId);
|
||||||
|
}
|
||||||
|
if (tourId is not null)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.SaleTours!.Any(y => y.TourId == tourId));
|
||||||
|
}
|
||||||
|
return [.. query.Select(x => _mapper.Map<SaleDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public SaleDataModel? GetElementById(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<SaleDataModel>(GetSaleById(id));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddElement(SaleDataModel saleDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Sales.Add(_mapper.Map<Sale>(saleDataModel));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DelElement(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetSaleById(id) ?? throw new ElementNotFoundException(id);
|
||||||
|
if (element.IsCancel)
|
||||||
|
{
|
||||||
|
throw new ElementDeletedException(id);
|
||||||
|
}
|
||||||
|
element.IsCancel = true;
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (Exception ex) when (ex is ElementDeletedException || ex is ElementNotFoundException)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Sale? GetSaleById(string id) => _dbContext.Sales.FirstOrDefault(x => x.Id == id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,173 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using MagicCarpetContracts.DataModels;
|
||||||
|
using MagicCarpetContracts.Exceptions;
|
||||||
|
using MagicCarpetContracts.StoragesContracts;
|
||||||
|
using MagicCarpetDatabase.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Npgsql;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MagicCarpetDatabase.Implementations;
|
||||||
|
|
||||||
|
internal class TourStorageContract : ITourStorageContract
|
||||||
|
{
|
||||||
|
private readonly MagicCarpetDbContext _dbContext;
|
||||||
|
private readonly Mapper _mapper;
|
||||||
|
|
||||||
|
public TourStorageContract(MagicCarpetDbContext dbContext)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.CreateMap<Tour, TourDataModel>();
|
||||||
|
cfg.CreateMap<TourDataModel, Tour>();
|
||||||
|
cfg.CreateMap<TourHistory, TourHistoryDataModel>();
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
public List<TourDataModel> GetList()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var query = _dbContext.Tours.AsQueryable();
|
||||||
|
return [.. query.Select(x => _mapper.Map<TourDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<TourHistoryDataModel> GetHistoryByTourId(string productId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return [.. _dbContext.TourHistories.Where(x => x.TourId == productId).OrderByDescending(x => x.ChangeDate)
|
||||||
|
.Select(x => _mapper.Map<TourHistoryDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TourDataModel? GetElementById(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<TourDataModel>(GetTourById(id));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TourDataModel? GetElementByName(string name)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<TourDataModel>(_dbContext.Tours.FirstOrDefault(x => x.TourName == name));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddElement(TourDataModel productDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Tours.Add(_mapper.Map<Tour>(productDataModel));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("Id", productDataModel.Id);
|
||||||
|
}
|
||||||
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Tours_TourName_IsDeleted" })
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("TourName", productDataModel.TourName);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdElement(TourDataModel productDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var transaction = _dbContext.Database.BeginTransaction();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetTourById(productDataModel.Id) ?? throw new ElementNotFoundException(productDataModel.Id);
|
||||||
|
if (element.Price != productDataModel.Price)
|
||||||
|
{
|
||||||
|
_dbContext.TourHistories.Add(new TourHistory() { TourId = element.Id, OldPrice = element.Price });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
_dbContext.Tours.Update(_mapper.Map(productDataModel, element));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
transaction.Rollback();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Tours_TourName_IsDeleted" })
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("TourName", productDataModel.TourName);
|
||||||
|
}
|
||||||
|
catch (Exception ex) when (ex is ElementDeletedException || ex is ElementNotFoundException)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DelElement(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void ResElement(string id)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Tour? GetTourById(string id) => _dbContext.Tours.FirstOrDefault(x => x.Id == id);
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="AutoMapper" Version="14.0.0" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.2" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.2" />
|
||||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.3" />
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ namespace MagicCarpetDatabase.Models;
|
|||||||
|
|
||||||
internal class Post
|
internal class Post
|
||||||
{
|
{
|
||||||
public required string Id { get; set; }
|
public required string Id { get; set; } = Guid.NewGuid().ToString();
|
||||||
public required string PostId { get; set; }
|
public required string PostId { get; set; }
|
||||||
public required string PostName { get; set; }
|
public required string PostName { get; set; }
|
||||||
public PostType PostType { get; set; }
|
public PostType PostType { get; set; }
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ namespace MagicCarpetDatabase.Models;
|
|||||||
|
|
||||||
internal class TourHistory
|
internal class TourHistory
|
||||||
{
|
{
|
||||||
public required string Id { get; set; } = Guid.NewGuid().ToString();
|
public string Id { get; set; } = Guid.NewGuid().ToString();
|
||||||
public required string TourId { get; set; }
|
public required string TourId { get; set; }
|
||||||
public double OldPrice { get; set; }
|
public double OldPrice { get; set; }
|
||||||
public DateTime ChangeDate { get; set; }
|
public DateTime ChangeDate { get; set; }
|
||||||
|
|||||||
Reference in New Issue
Block a user