150 lines
4.3 KiB
C#
150 lines
4.3 KiB
C#
using AutoMapper;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Npgsql;
|
|
using SquirrelContract.DataModels;
|
|
using SquirrelContract.Exceptions;
|
|
using SquirrelContract.StoragesContracts;
|
|
using SquirrelDatabase.Models;
|
|
using System;
|
|
|
|
namespace SquirrelDatabase.Implementations;
|
|
|
|
public class ClientStorageContract : IClientStorageContract
|
|
{
|
|
private readonly SquirrelDbContext _dbContext;
|
|
private readonly Mapper _mapper;
|
|
|
|
public ClientStorageContract(SquirrelDbContext squirrelDbContext)
|
|
{
|
|
_dbContext = squirrelDbContext;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.AddMaps(typeof(SquirrelDbContext).Assembly);
|
|
});
|
|
_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);
|
|
}
|