PIbd-23_Elatomtsev_L.K._Cou.../Bank/BankRestApi/Controllers/DepositController.cs

107 lines
3.3 KiB
C#
Raw Normal View History

2024-05-27 07:56:21 +04:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using BankContracts.BindingModels;
using BankContracts.BusinessLogicsContracts;
using BankContracts.SearchModels;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models;
namespace BankRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class DepositController : Controller
{
private readonly ILogger _logger;
2024-05-27 07:56:21 +04:00
private readonly IDepositLogic _deposit;
public DepositController(ILogger<DepositController> logger, IDepositLogic deposit)
{
_logger = logger;
2024-05-27 07:56:21 +04:00
_deposit = deposit;
}
[HttpGet]
2024-05-27 07:56:21 +04:00
public Tuple<DepositViewModel, List<Tuple<string, string>>>? GetDeposit(int depositId)
{
try
{
2024-05-27 07:56:21 +04:00
var elem = _deposit.ReadElement(new DepositSearchModel { Id = depositId });
if (elem == null)
return null;
var res = Tuple.Create(elem, elem.ClientDeposits.Select(x => Tuple.Create(x.Value.ClientSurname, x.Value.Snils)).ToList());
res.Item1.ClientDeposits = null!;
return res;
}
catch (Exception ex)
{
2024-05-27 07:56:21 +04:00
_logger.LogError(ex, "Ошибка получения вклада по id={Id}", depositId);
throw;
}
}
2024-05-27 07:56:21 +04:00
[HttpGet]
2024-05-27 07:56:21 +04:00
public List<DepositViewModel>? GetDepositList(int? workerId = null)
{
try
{
2024-05-27 07:56:21 +04:00
List<DepositViewModel> res;
if (!workerId.HasValue)
res = _deposit.ReadList(null);
else
res = _deposit.ReadList(new DepositSearchModel { WorkerId = workerId });
foreach (var deposit in res)
deposit.ClientDeposits = null;
return res;
}
catch (Exception ex)
{
2024-05-27 07:56:21 +04:00
_logger.LogError(ex, "Ошибка получения списка вкладов");
throw;
}
}
[HttpPost]
2024-05-27 07:56:21 +04:00
public bool CreateDeposit(DepositBindingModel model)
{
try
{
2024-05-27 07:56:21 +04:00
return _deposit.Create(model);
}
catch (Exception ex)
{
2024-05-27 07:56:21 +04:00
_logger.LogError(ex, "Не удалось создать вклад");
throw;
}
}
2024-05-27 07:56:21 +04:00
[HttpPost]
public bool UpdateDeposit(bool isConnection, DepositBindingModel model)
{
try
{
2024-05-27 07:56:21 +04:00
if (!isConnection)
model.ClientDeposit = null!;
return _deposit.Update(model);
}
catch (Exception ex)
{
2024-05-27 07:56:21 +04:00
_logger.LogError(ex, "Не удалось обновить вклад");
throw;
}
}
2024-05-27 07:56:21 +04:00
[HttpPost]
public bool DeleteDeposit(DepositBindingModel model)
{
try
{
2024-05-27 07:56:21 +04:00
return _deposit.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления вклада");
throw;
}
}
}
2024-05-27 07:56:21 +04:00
}