188 lines
6.1 KiB
C#
188 lines
6.1 KiB
C#
using AutoMapper;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Npgsql;
|
|
using SmallSoftwareContracts.DataModels;
|
|
using SmallSoftwareContracts.Exceptions;
|
|
using SmallSoftwareContracts.StoragesContracts;
|
|
using SmallSoftwareDatabase.Models;
|
|
|
|
namespace SmallSoftwareDatabase.Implementations;
|
|
|
|
internal class SoftwareStorageContract : ISoftwareStorageContract
|
|
{
|
|
private readonly SmallSoftwareDbContext _dbContext;
|
|
private readonly Mapper _mapper;
|
|
public SoftwareStorageContract(SmallSoftwareDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<Manufacturer, ManufacturerDataModel>();
|
|
cfg.CreateMap<Software, SoftwareDataModel>();
|
|
cfg.CreateMap<SoftwareDataModel, Software>()
|
|
.ForMember(x => x.IsDeleted, x => x.MapFrom(src => false));
|
|
cfg.CreateMap<SoftwareHistory, SoftwareHistoryDataModel>();
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
public List<SoftwareDataModel> GetList(bool onlyActive = true, string?
|
|
manufacturerId = null)
|
|
{
|
|
try
|
|
{
|
|
var query = _dbContext.Softwares.Include(x => x.Manufacturer).AsQueryable();
|
|
if (onlyActive)
|
|
{
|
|
query = query.Where(x => !x.IsDeleted);
|
|
}
|
|
if (manufacturerId is not null)
|
|
{
|
|
query = query.Where(x => x.ManufacturerId ==
|
|
manufacturerId);
|
|
}
|
|
return [.. query.Select(x => _mapper.Map<SoftwareDataModel>(x))];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
public List<SoftwareHistoryDataModel> GetHistoryBySoftwareId(string
|
|
softwareId)
|
|
{
|
|
try
|
|
{
|
|
return [.. _dbContext.SoftwareHistories.Include(x => x.Software).Where(x => x.SoftwareId == softwareId)
|
|
.OrderByDescending(x => x.ChangeDate)
|
|
.Select(x => _mapper.Map<SoftwareHistoryDataModel>(x))];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
public SoftwareDataModel? GetElementById(string id)
|
|
{
|
|
try
|
|
{
|
|
return _mapper.Map<SoftwareDataModel>(GetSoftwareById(id));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
public SoftwareDataModel? GetElementByName(string name)
|
|
{
|
|
try
|
|
{
|
|
return
|
|
_mapper.Map<SoftwareDataModel>(_dbContext.Softwares.Include(x => x.Manufacturer).FirstOrDefault(x =>
|
|
x.SoftwareName == name && !x.IsDeleted));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
public void AddElement(SoftwareDataModel softwareDataModel)
|
|
{
|
|
try
|
|
{
|
|
_dbContext.Softwares.Add(_mapper.Map<Software>(softwareDataModel));
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (InvalidOperationException ex) when (ex.TargetSite?.Name ==
|
|
"ThrowIdentityConflict")
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("Id", softwareDataModel.Id);
|
|
}
|
|
catch (DbUpdateException ex) when (ex.InnerException is
|
|
PostgresException { ConstraintName: "IX_Softwares_SoftwareName_IsDeleted" })
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("SoftwareName",
|
|
softwareDataModel.SoftwareName);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
public void UpdElement(SoftwareDataModel softwareDataModel)
|
|
{
|
|
try
|
|
{
|
|
var transaction = _dbContext.Database.BeginTransaction();
|
|
try
|
|
{
|
|
var element = GetSoftwareById(softwareDataModel.Id) ??
|
|
throw new ElementNotFoundException(softwareDataModel.Id);
|
|
if (element.Price != softwareDataModel.Price)
|
|
{
|
|
_dbContext.SoftwareHistories.Add(new
|
|
SoftwareHistory()
|
|
{ SoftwareId = element.Id, OldPrice = element.Price });
|
|
_dbContext.SaveChanges();
|
|
}
|
|
_dbContext.Softwares.Update(_mapper.Map(softwareDataModel,
|
|
element));
|
|
_dbContext.SaveChanges();
|
|
transaction.Commit();
|
|
}
|
|
catch
|
|
{
|
|
transaction.Rollback();
|
|
throw;
|
|
}
|
|
}
|
|
catch (DbUpdateException ex) when (ex.InnerException is
|
|
PostgresException { ConstraintName: "IX_Softwares_SoftwareName_IsDeleted" })
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("SoftwareName",
|
|
softwareDataModel.SoftwareName);
|
|
}
|
|
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 = GetSoftwareById(id) ?? throw new
|
|
ElementNotFoundException(id);
|
|
element.IsDeleted = true;
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
private Software? GetSoftwareById(string id) =>
|
|
_dbContext.Softwares.Include(x => x.Manufacturer).FirstOrDefault(x => x.Id == id && !x.IsDeleted);
|
|
}
|
|
|