feat: сторадж контракты клерка
This commit is contained in:
@@ -6,6 +6,10 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BankContracts\BankContracts.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -57,7 +57,7 @@ internal class DepositBusinessLogicContract(
|
||||
return _depositStorageContract.GetElementById(data)
|
||||
?? throw new ElementNotFoundException($"element not found: {data}");
|
||||
}
|
||||
return _depositStorageContract.GetElementByName(data)
|
||||
return _depositStorageContract.GetElementByInterestRate(data)
|
||||
?? throw new ElementNotFoundException($"element not found: {data}");
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ public interface IDepositStorageContract
|
||||
|
||||
DepositDataModel? GetElementById(string id);
|
||||
|
||||
DepositDataModel? GetElementByName(string name);
|
||||
DepositDataModel? GetElementByInterestRate(float interestRate);
|
||||
|
||||
void AddElement(DepositDataModel depositDataModel);
|
||||
|
||||
|
||||
177
TheBank/BankDatabase/Implementations/ClientStorageContract.cs
Normal file
177
TheBank/BankDatabase/Implementations/ClientStorageContract.cs
Normal file
@@ -0,0 +1,177 @@
|
||||
using AutoMapper;
|
||||
using BankContracts.DataModels;
|
||||
using BankContracts.Exceptions;
|
||||
using BankContracts.StorageContracts;
|
||||
using BankDatabase.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace BankDatabase.Implementations;
|
||||
|
||||
internal class ClientStorageContract : IClientStorageContract
|
||||
{
|
||||
|
||||
private readonly BankDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public ClientStorageContract(BankDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<Clerk, ClerkDataModel>();
|
||||
cfg.CreateMap<Client, ClientDataModel>();
|
||||
cfg.CreateMap<ClientDataModel, Client>();
|
||||
cfg.CreateMap<Replenishment, ReplenishmentDataModel>();
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<ClientDataModel> GetList(string? clerkId = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var query = _dbContext.Deposits.Include(x => x.Clerk).AsQueryable();
|
||||
if (clerkId is not null)
|
||||
{
|
||||
query = query.Where(x => x.ClerkId == clerkId);
|
||||
}
|
||||
return [.. query.Select(x => _mapper.Map<ClientDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public ClientDataModel? GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<ClientDataModel>(_dbContext.Clients.FirstOrDefault(x => x.Id == id));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public ClientDataModel? GetElementByName(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<ClientDataModel>(_dbContext.Clients.Include(x => x.Clerk).FirstOrDefault(x => x.Name == name));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public ClientDataModel? GetElementBySurname(string surname)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<ClientDataModel>(_dbContext.Clients.Include(x => x.Clerk).FirstOrDefault(x => x.Surname == surname));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
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_Name" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException($"Name {clientDataModel.Name}");
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Clients_Surname" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException($"Surname {clientDataModel.Surname}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdElement(ClientDataModel clientDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var transaction = _dbContext.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var element = GetClientById(clientDataModel.Id) ?? throw new ElementNotFoundException(clientDataModel.Id);
|
||||
//проверь пожалуйста(не уверен)
|
||||
if (clientDataModel.Deposits != null && clientDataModel.CreditPrograms != null)
|
||||
{
|
||||
if (element.DepositClients != null || element.DepositClients?.Count >= 0)
|
||||
{
|
||||
_dbContext.DepositClients.RemoveRange(element.DepositClients);
|
||||
}
|
||||
|
||||
if (element.CreditProgramClients != null || element.CreditProgramClients?.Count >= 0)
|
||||
{
|
||||
_dbContext.DepositClients.RemoveRange(element.DepositClients);
|
||||
}
|
||||
|
||||
element.DepositClients = _mapper.Map<List<DepositClient>>(clientDataModel.Deposits);
|
||||
element.DepositClients = _mapper.Map<List<DepositClient>>(clientDataModel.CreditPrograms);
|
||||
}
|
||||
_dbContext.SaveChanges();
|
||||
transaction.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Clients_Name" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException($"Name {clientDataModel.Name}");
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Clients_Surname" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException($"Surname {clientDataModel.Surname}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex.Message);
|
||||
}
|
||||
}
|
||||
private Client? GetClientById(string id) => _dbContext.Clients.FirstOrDefault(x => x.Id == id);
|
||||
}
|
||||
@@ -5,11 +5,6 @@ using BankContracts.StorageContracts;
|
||||
using BankDatabase.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BankDatabase.Implementations;
|
||||
|
||||
@@ -114,7 +109,7 @@ internal class DepositStorageContract : IDepositStorageContract
|
||||
_dbContext.DepositCurrencies.RemoveRange(element.DepositCurrencies);
|
||||
}
|
||||
|
||||
element.Components = _mapper.Map<List<ShipComponents>>(shipDataModel.Components);
|
||||
element.DepositCurrencies = _mapper.Map<List<DepositCurrency>>(depositDataModel.Currencies);
|
||||
}
|
||||
_dbContext.SaveChanges();
|
||||
transaction.Commit();
|
||||
@@ -130,20 +125,15 @@ internal class DepositStorageContract : IDepositStorageContract
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Ships_Name" })
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Deposits_InterestRate" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("Name", shipDataModel.Name);
|
||||
}
|
||||
catch (Exception ex) when (ex is ElementDeletedException || ex is ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
throw new ElementExistsException($"InterestRate {depositDataModel.InterestRate}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
throw new StorageException(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
using AutoMapper;
|
||||
using BankContracts.DataModels;
|
||||
using BankContracts.Exceptions;
|
||||
using BankContracts.StorageContracts;
|
||||
using BankDatabase.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql;
|
||||
|
||||
namespace BankDatabase.Implementations;
|
||||
|
||||
internal class ReplenishmentStorageContract : IReplenishmentStorageContract
|
||||
{
|
||||
private readonly BankDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public ReplenishmentStorageContract(BankDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<Replenishment, ReplenishmentDataModel>();
|
||||
cfg.CreateMap<ReplenishmentDataModel, Replenishment>();
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<ReplenishmentDataModel> GetList(DateTime? fromDate = null, DateTime? toDate = null, string? clerkId = null, string? depositId = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var query = _dbContext.Replenishments.Where(x => x.Date >= fromDate && x.Date <= toDate);
|
||||
if (clerkId is not null)
|
||||
{
|
||||
query = query.Where(x => x.ClerkId == clerkId);
|
||||
}
|
||||
if (depositId is not null)
|
||||
{
|
||||
query = query.Where(x => x.DepositId == depositId);
|
||||
}
|
||||
return [.. query.Select(x => _mapper.Map<ReplenishmentDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public ReplenishmentDataModel? GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<ReplenishmentDataModel>(_dbContext.Replenishments.FirstOrDefault(x => x.Id == id));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void AddElement(ReplenishmentDataModel replenishmentDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Replenishments.Add(_mapper.Map<Replenishment>(replenishmentDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException($"Id {replenishmentDataModel.Id}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdElement(ReplenishmentDataModel replenishmentDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetReplenishmentById(replenishmentDataModel.Id) ?? throw new ElementNotFoundException(replenishmentDataModel.Id);
|
||||
_dbContext.Replenishments.Update(_mapper.Map(replenishmentDataModel, element));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex.Message);
|
||||
}
|
||||
}
|
||||
private Replenishment? GetReplenishmentById(string id) => _dbContext.Replenishments.FirstOrDefault(x => x.Id == id);
|
||||
}
|
||||
Reference in New Issue
Block a user