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 logger, IDepositLogic deposit) { _logger = logger; _deposit = deposit; } [HttpGet] public Tuple>>? 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? GetDepositList(int? workerId = null) { try { List 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; } } } }