137 lines
3.7 KiB
C#
137 lines
3.7 KiB
C#
using AircraftPlantContracts.BindingModels;
|
||
using AircraftPlantContracts.BusinessLogicsContracts;
|
||
using AircraftPlantContracts.SearchModels;
|
||
using AircraftPlantContracts.ViewModels;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
|
||
namespace AircraftPlantRestApi.Controllers
|
||
{
|
||
/// <summary>
|
||
/// Контроллер для работы с клиентами
|
||
/// </summary>
|
||
[Route("api/[controller]/[action]")]
|
||
[ApiController]
|
||
public class ClientController : Controller
|
||
{
|
||
/// <summary>
|
||
/// Логгер
|
||
/// </summary>
|
||
private readonly ILogger _logger;
|
||
|
||
/// <summary>
|
||
/// Бизнес-логика для клиентов
|
||
/// </summary>
|
||
private readonly IClientLogic _logic;
|
||
|
||
/// <summary>
|
||
/// Бизнес-логика для писем
|
||
/// </summary>
|
||
private readonly IMessageInfoLogic _mailLogic;
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="logic"></param>
|
||
/// <param name="logger"></param>
|
||
/// <param name="mailLogic"></param>
|
||
public ClientController(IClientLogic logic, ILogger<ClientController> logger, IMessageInfoLogic mailLogic)
|
||
{
|
||
_logger = logger;
|
||
_logic = logic;
|
||
_mailLogic = mailLogic;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Вход в систему
|
||
/// </summary>
|
||
/// <param name="login"></param>
|
||
/// <param name="password"></param>
|
||
/// <returns></returns>
|
||
[HttpGet]
|
||
public ClientViewModel? Login(string login, string password)
|
||
{
|
||
try
|
||
{
|
||
return _logic.ReadElement(new ClientSearchModel
|
||
{
|
||
Email = login,
|
||
Password = password
|
||
});
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка входа в систему");
|
||
throw;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Регистрация
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
[HttpPost]
|
||
public void Register(ClientBindingModel model)
|
||
{
|
||
try
|
||
{
|
||
_logic.Create(model);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка регистрации");
|
||
throw;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Обновление данных
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
[HttpPost]
|
||
public void UpdateData(ClientBindingModel model)
|
||
{
|
||
try
|
||
{
|
||
_logic.Update(model);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка обновления данных");
|
||
throw;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Получение списка писем клиента
|
||
/// </summary>
|
||
/// <param name="clientId"></param>
|
||
/// <returns></returns>
|
||
[HttpGet]
|
||
public List<MessageInfoViewModel>? GetMessages(int clientId, int? currentPage)
|
||
{
|
||
try
|
||
{
|
||
if (currentPage == null)
|
||
{
|
||
return _mailLogic.ReadList(new MessageInfoSearchModel
|
||
{
|
||
ClientId = clientId
|
||
});
|
||
}
|
||
|
||
return _mailLogic.ReadList(new MessageInfoSearchModel
|
||
{
|
||
ClientId = clientId,
|
||
CurrentPage = currentPage,
|
||
PageSize = 3
|
||
});
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка получения писем клиента");
|
||
throw;
|
||
}
|
||
}
|
||
}
|
||
}
|