148 lines
4.8 KiB
C#
148 lines
4.8 KiB
C#
using AutoMapper;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Localization;
|
|
using Npgsql;
|
|
using SquirrelContract.DataModels;
|
|
using SquirrelContract.Exceptions;
|
|
using SquirrelContract.Mapper;
|
|
using SquirrelContract.Resources;
|
|
using SquirrelContract.StoragesContracts;
|
|
using SquirrelDatabase.Models;
|
|
using System;
|
|
|
|
namespace SquirrelDatabase.Implementations;
|
|
|
|
internal class ClientStorageContract(SquirrelDbContext squirrelDbContext, IStringLocalizer<Messages> localizer) : IClientStorageContract
|
|
{
|
|
private readonly SquirrelDbContext _dbContext = squirrelDbContext;
|
|
private readonly IStringLocalizer<Messages> _localizer = localizer;
|
|
|
|
public List<ClientDataModel> GetList()
|
|
{
|
|
try
|
|
{
|
|
return [.. _dbContext.Clients.Select(x => CustomMapper.MapObject<ClientDataModel>(x))];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
|
|
public ClientDataModel? GetElementById(string id)
|
|
{
|
|
try
|
|
{
|
|
return CustomMapper.MapObjectWithNull<ClientDataModel>(GetClientById(id));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
|
|
public ClientDataModel? GetElementByFIO(string fio)
|
|
{
|
|
try
|
|
{
|
|
return CustomMapper.MapObjectWithNull<ClientDataModel>(_dbContext.Clients.FirstOrDefault(x => x.FIO == fio));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
|
|
public ClientDataModel? GetElementByPhoneNumber(string phoneNumber)
|
|
{
|
|
try
|
|
{
|
|
return CustomMapper.MapObjectWithNull<ClientDataModel>(_dbContext.Clients.FirstOrDefault(x => x.PhoneNumber == phoneNumber));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
|
|
public void AddElement(ClientDataModel clientDataModel)
|
|
{
|
|
try
|
|
{
|
|
_dbContext.Clients.Add(CustomMapper.MapObject<Client>(clientDataModel));
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("Id", clientDataModel.Id, _localizer);
|
|
}
|
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Clients_PhoneNumber" })
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("PhoneNumber", clientDataModel.PhoneNumber, _localizer);
|
|
}
|
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "PK_Clients" })
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("Id", clientDataModel.Id, _localizer);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
|
|
public void UpdElement(ClientDataModel clientDataModel)
|
|
{
|
|
try
|
|
{
|
|
var element = GetClientById(clientDataModel.Id) ?? throw new ElementNotFoundException(clientDataModel.Id, _localizer);
|
|
_dbContext.Clients.Update(CustomMapper.MapObject(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, _localizer);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
|
|
public void DelElement(string id)
|
|
{
|
|
try
|
|
{
|
|
var element = GetClientById(id) ?? throw new ElementNotFoundException(id, _localizer);
|
|
_dbContext.Clients.Remove(element);
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (ElementNotFoundException)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex, _localizer);
|
|
}
|
|
}
|
|
|
|
private Client? GetClientById(string id) => _dbContext.Clients.FirstOrDefault(x => x.Id == id);
|
|
}
|