161 lines
5.5 KiB
C#
161 lines
5.5 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Localization;
|
|
using Npgsql;
|
|
using SmallSoftwareContracts.DataModels;
|
|
using SmallSoftwareContracts.Exceptions;
|
|
using SmallSoftwareContracts.Mapper;
|
|
using SmallSoftwareContracts.Resources;
|
|
using SmallSoftwareContracts.StoragesContracts;
|
|
using SmallSoftwareDatabase.Models;
|
|
|
|
namespace SmallSoftwareDatabase.Implementations;
|
|
internal class ManufacturerStorageContract(SmallSoftwareDbContext dbContext, IStringLocalizer<Messages> localizer) : IManufacturerStorageContract
|
|
{
|
|
private readonly SmallSoftwareDbContext _dbContext = dbContext;
|
|
private readonly IStringLocalizer<Messages> _localizer = localizer;
|
|
|
|
public List<ManufacturerDataModel> GetList()
|
|
{
|
|
try
|
|
{
|
|
return [.. _dbContext.Manufacturers.Select(x => CustomMapper.MapObject<ManufacturerDataModel>(x))];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
public ManufacturerDataModel? GetElementById(string id)
|
|
{
|
|
try
|
|
{
|
|
return
|
|
CustomMapper.MapObjectWithNull<ManufacturerDataModel>(GetManufacturerById(id));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
public ManufacturerDataModel? GetElementByName(string name)
|
|
{
|
|
try
|
|
{
|
|
return
|
|
CustomMapper.MapObjectWithNull<ManufacturerDataModel>(_dbContext.Manufacturers.FirstOrDefault(x => x.ManufacturerName == name));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
public ManufacturerDataModel? GetElementByOldName(string name)
|
|
{
|
|
try
|
|
{
|
|
return
|
|
CustomMapper.MapObjectWithNull<ManufacturerDataModel>(_dbContext.Manufacturers.FirstOrDefault(x =>
|
|
x.PrevManufacturerName == name ||
|
|
x.PrevPrevManufacturerName == name));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
public void AddElement(ManufacturerDataModel manufacturerDataModel)
|
|
{
|
|
try
|
|
{
|
|
_dbContext.Manufacturers.Add(CustomMapper.MapObject<Manufacturer>(manufacturerDataModel));
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (InvalidOperationException ex) when (ex.TargetSite?.Name ==
|
|
"ThrowIdentityConflict")
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("Id", manufacturerDataModel.Id, _localizer);
|
|
}
|
|
catch (DbUpdateException ex) when (ex.InnerException is
|
|
PostgresException { ConstraintName: "IX_Manufacturers_ManufacturerName" })
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("ManufacturerName",
|
|
manufacturerDataModel.ManufacturerName, _localizer);
|
|
}
|
|
catch (DbUpdateException ex) when (ex.InnerException is
|
|
PostgresException { ConstraintName: "PK_Manufacturers" })
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("Id", manufacturerDataModel.Id, _localizer);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
public void UpdElement(ManufacturerDataModel manufacturerDataModel)
|
|
{
|
|
try
|
|
{
|
|
var element = GetManufacturerById(manufacturerDataModel.Id) ??
|
|
throw new ElementNotFoundException(manufacturerDataModel.Id, _localizer);
|
|
if (element.ManufacturerName !=
|
|
manufacturerDataModel.ManufacturerName)
|
|
{
|
|
element.PrevPrevManufacturerName =
|
|
element.PrevManufacturerName;
|
|
element.PrevManufacturerName = element.ManufacturerName;
|
|
element.ManufacturerName =
|
|
manufacturerDataModel.ManufacturerName;
|
|
}
|
|
_dbContext.Manufacturers.Update(element);
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (ElementNotFoundException)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
catch (DbUpdateException ex) when (ex.InnerException is
|
|
PostgresException { ConstraintName: "IX_Manufacturers_ManufacturerName" })
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("ManufacturerName",
|
|
manufacturerDataModel.ManufacturerName, _localizer);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
public void DelElement(string id)
|
|
{
|
|
try
|
|
{
|
|
var element = GetManufacturerById(id) ?? throw new
|
|
ElementNotFoundException(id, _localizer);
|
|
_dbContext.Manufacturers.Remove(element);
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (ElementNotFoundException)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
private Manufacturer? GetManufacturerById(string id) =>
|
|
_dbContext.Manufacturers.FirstOrDefault(x => x.Id == id);
|
|
}
|