CourseWork_Bank/Bank/BankRestAPI/Controllers/CardController.cs

127 lines
2.9 KiB
C#

using BankContracts.BindingModels.Client;
using BankContracts.BusinessLogicsContracts.Cashier;
using BankContracts.BusinessLogicsContracts.Client;
using BankContracts.SearchModels.Client;
using BankContracts.ViewModels.Client.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System.Net;
namespace BankRestAPI.Controllers
{
// Route будет строиться по наванию контроллера и по названию метода (два Post-метода)
[Route("api/[controller]/[action]")]
[ApiController]
public class CardController : Controller
{
private readonly ILogger _logger;
private readonly ICardLogic _cardLogic;
private readonly IDebitingLogic _debitingLogic;
private readonly ICreditingLogic _creditingLogic;
private readonly IAccountLogic _accountLogic;
// Конструктор
public CardController(ILogger<ClientController> logger, ICardLogic cardLogic, IDebitingLogic debitingLogic, ICreditingLogic creditingLogic, IAccountLogic accountLogic)
{
_logger = logger;
_cardLogic = cardLogic;
_debitingLogic = debitingLogic;
_creditingLogic = creditingLogic;
_accountLogic = accountLogic;
}
[HttpGet]
public List<CardViewModel>? GetAllCards()
{
try
{
return _cardLogic.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения карт");
throw;
}
}
[HttpGet]
public List<CardViewModel>? GetClientsCardsList(int id)
{
try
{
return _cardLogic.ReadList(new CardSearchModel()
{
ClientID = id
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения карт");
throw;
}
}
[HttpPost]
public void CreateCard(CardBindingModel model)
{
try
{
_cardLogic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания банковской карты");
Response.StatusCode = (int)HttpStatusCode.NotAcceptable;
}
}
[HttpPost]
public void CreateDebitingRequest(DebitingBindingModel model)
{
try
{
_debitingLogic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания запроса на снятие наличных");
Response.StatusCode = (int)(HttpStatusCode.NotAcceptable);
}
}
[HttpPost]
public void CreateCreditingOperation(CreditingBindingModel model)
{
try
{
_creditingLogic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания операции на пополнение");
Response.StatusCode = (int)HttpStatusCode.NotAcceptable;
}
}
[HttpGet]
public CardViewModel? FindCard(int id)
{
try
{
return _cardLogic.ReadElement(new CardSearchModel
{
Id = id
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка входа в систему");
throw;
}
}
}
}