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

107 lines
3.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
private readonly IDepositLogic _deposit;
public DepositController(ILogger<DepositController> logger, IDepositLogic deposit)
{
_logger = logger;
_deposit = deposit;
}
[HttpGet]
public Tuple<DepositViewModel, List<Tuple<string, string>>>? GetDeposit(int depositId)
{
try
{
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)
{
_logger.LogError(ex, "Ошибка получения вклада по id={Id}", depositId);
throw;
}
}
[HttpGet]
public List<DepositViewModel>? GetDepositList(int? workerId = null)
{
try
{
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)
{
_logger.LogError(ex, "Ошибка получения списка вкладов");
throw;
}
}
[HttpPost]
public bool CreateDeposit(DepositBindingModel model)
{
try
{
return _deposit.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Не удалось создать вклад");
throw;
}
}
[HttpPost]
public bool UpdateDeposit(bool isConnection, DepositBindingModel model)
{
try
{
if (!isConnection)
model.ClientDeposit = null!;
return _deposit.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Не удалось обновить вклад");
throw;
}
}
[HttpPost]
public bool DeleteDeposit(DepositBindingModel model)
{
try
{
return _deposit.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления вклада");
throw;
}
}
}
}