149 lines
4.4 KiB
C#
149 lines
4.4 KiB
C#
using AutoMapper;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Npgsql;
|
|
using TwoFromTheCaseContracts.DataModels;
|
|
using TwoFromTheCaseContracts.Exceptions;
|
|
using TwoFromTheCaseContracts.StoragesContracts;
|
|
using TwoFromTheCaseDatabase.Models;
|
|
|
|
namespace TwoFromTheCaseDatabase.Implementations;
|
|
|
|
class CustomerStorageContract : ICustomerStorageContract
|
|
{
|
|
private readonly TwoFromTheCaseDbContext _dbContext;
|
|
private readonly Mapper _mapper;
|
|
|
|
public CustomerStorageContract(TwoFromTheCaseDbContext twoFromTheCaseDbContext)
|
|
{
|
|
_dbContext = twoFromTheCaseDbContext;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.AddMaps(typeof(Customer));
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
|
|
public List<CustomerDataModel> GetList()
|
|
{
|
|
try
|
|
{
|
|
return [.. _dbContext.Customers.Select(x => _mapper.Map<CustomerDataModel>(x))];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public CustomerDataModel? GetElementById(string id)
|
|
{
|
|
try
|
|
{
|
|
return _mapper.Map<CustomerDataModel>(GetCustomerById(id));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public CustomerDataModel? GetElementByName(string name)
|
|
{
|
|
try
|
|
{
|
|
return _mapper.Map<CustomerDataModel>(_dbContext.Customers.FirstOrDefault(x => x.Name == name));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public CustomerDataModel? GetElementByPhoneNumber(string phoneNumber)
|
|
{
|
|
try
|
|
{
|
|
return _mapper.Map<CustomerDataModel>(_dbContext.Customers.FirstOrDefault(x => x.PhoneNumber == phoneNumber));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public void AddElement(CustomerDataModel customerDataModel)
|
|
{
|
|
try
|
|
{
|
|
_dbContext.Customers.Add(_mapper.Map<Customer>(customerDataModel));
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("Id", customerDataModel.Id);
|
|
}
|
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Customers_PhoneNumber" })
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("PhoneNumber", customerDataModel.PhoneNumber);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public void UpdElement(CustomerDataModel customerDataModel)
|
|
{
|
|
try
|
|
{
|
|
var element = GetCustomerById(customerDataModel.Id) ?? throw new ElementNotFoundException(customerDataModel.Id);
|
|
_dbContext.Customers.Update(_mapper.Map(customerDataModel, element));
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (ElementNotFoundException)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Customers_PhoneNumber" })
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("PhoneNumber", customerDataModel.PhoneNumber);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public void DelElement(string id)
|
|
{
|
|
try
|
|
{
|
|
var element = GetCustomerById(id) ?? throw new ElementNotFoundException(id);
|
|
_dbContext.Customers.Remove(element);
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (ElementNotFoundException)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
private Customer? GetCustomerById(string id) => _dbContext.Customers.FirstOrDefault(x => x.Id == id);
|
|
}
|