CourseWork_BankYouBankrupt/BankYouBankrupt/BankYouBankruptRestAPI/Controllers/ClientController.cs
2023-05-17 15:18:35 +04:00

142 lines
4.4 KiB
C#
Raw 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 BankYouBankruptBusinessLogic.BusinessLogics;
using BankYouBankruptContracts.BindingModels;
using BankYouBankruptContracts.BusinessLogicsContracts;
using BankYouBankruptContracts.SearchModels;
using BankYouBankruptContracts.ViewModels;
using DocumentFormat.OpenXml.Office2010.Excel;
using Microsoft.AspNetCore.Mvc;
namespace BankYouBankruptRestApi.Controllers
{
//указание у контроллера, что Route будет строиться не только по наванию контроллера, но и по названию метода (так как у нас два Post-метода)
[Route("api/[controller]/[action]")]
[ApiController]
public class ClientController : Controller
{
private readonly ILogger _logger;
private readonly IClientLogic _clientLogic;
private readonly IDebitingLogic _debitingLogic;
private readonly ICreditingLogic _creditingLogic;
public ClientController(ILogger<ClientController> logger,
IClientLogic clientLogic, IDebitingLogic debitingLogic, ICreditingLogic creditingLogic)
{
_logger = logger;
_clientLogic = clientLogic;
_debitingLogic = debitingLogic;
_creditingLogic = creditingLogic;
}
[HttpGet]
public ClientViewModel? Login(string login, string password)
{
try
{
//попытка найти запись по переданным логину и паролю
return _clientLogic.ReadElement(new ClientSearchModel
{
Email = login,
Password = password
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка входа в систему");
throw;
}
}
[HttpGet]
public List<ClientViewModel>? GetAllClients() {
try
{
return _clientLogic.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения пользователей");
throw;
}
}
[HttpGet]
public ClientViewModel? GetClient(int clientId)
{
try
{
return _clientLogic.ReadElement(new ClientSearchModel { Id = clientId });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения пользователя");
throw;
}
}
[HttpPost]
public void Register(ClientBindingModel model)
{
try
{
//создание клиента
_clientLogic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка регистрации");
throw;
}
}
[HttpPost]
public void UpdateData(ClientBindingModel model)
{
try
{
//изменение клиента
_clientLogic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления данных");
throw;
}
}
[HttpGet]
public List<DebitingViewModel>? getUsersDebitings(int userId) {
try
{
return _debitingLogic.ReadList(new DebitingSearchModel()
{
UserId = userId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения пользователей");
throw;
}
}
[HttpGet]
public List<CreditingViewModel>? getUsersCreditings(int userId)
{
try
{
return _creditingLogic.ReadList(new CreditingSearchModel()
{
UserId = userId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения пользователей");
throw;
}
}
}
}