Computer_Hardware_Store/HardwareShop/HardwareShopRestApi/Controllers/ClientController.cs

74 lines
2.1 KiB
C#
Raw Normal View History

2023-04-01 22:18:09 +04:00
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.BusinessLogicsContracts;
using HardwareShopContracts.SearchModels;
using HardwareShopContracts.ViewModels;
using HardwareShopDatabaseImplement.Models.Storekeeper;
2023-04-01 22:18:09 +04:00
using Microsoft.AspNetCore.Mvc;
namespace HardwareShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ClientController : Controller
{
private readonly ILogger _logger;
private readonly IUserLogic _logic;
private readonly IPurchaseLogic _purchaseLogic;
public ClientController(IUserLogic logic, IPurchaseLogic purchaseLogic, ILogger<ClientController> logger)
2023-04-01 22:18:09 +04:00
{
_logger = logger;
_logic = logic;
_purchaseLogic = purchaseLogic;
2023-04-01 22:18:09 +04:00
}
[HttpGet]
public UserViewModel? Login(string email, string password)
2023-04-01 22:18:09 +04:00
{
try
{
return _logic.ReadElement(new UserSearchModel
{
Email = email,
2023-04-01 22:18:09 +04:00
Password = password
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка входа в систему");
throw;
}
}
[HttpPost]
public void Register(UserBindingModel model)
{
try
{
_logic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка регистрации");
throw;
}
}
[HttpGet]
public List<PurchaseViewModel>? GetPurchases(int userId)
{
try
{
return _purchaseLogic.ReadList(new PurchaseSearchModel { UserId = userId });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка заказов клиента id={Id}", userId);
throw;
}
}
2023-04-01 22:18:09 +04:00
}
}