ComputerHardwareStore_YouAr.../ComputerHardwareStore/VendorClient/Controllers/PurchaseController.cs
2024-05-29 15:26:54 +04:00

100 lines
3.4 KiB
C#

using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDataModels.Models;
using Microsoft.AspNetCore.Mvc;
namespace VendorClient.Controllers
{
[Route("Home/[action]")]
public class PurchaseController : Controller
{
private readonly ILogger<PurchaseController> _logger;
private const string API_ROUTE = "api/purchases";
public PurchaseController(ILogger<PurchaseController> logger)
{
_logger = logger;
}
[HttpGet]
public IActionResult Products()
{
if (APIClient.Vendor == null)
{
return Redirect("~/Enter");
}
ViewBag.Goods = APIClient.GetRequest<List<ProductViewModel>>($"api/products/GetAllProducts");
return View();
}
[HttpPost]
public void PurchaseCreate([FromBody] PurchaseBindingModel purchaseModel)
{
if (APIClient.Vendor == null)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
if (purchaseModel.Cost <= 0)
{
throw new Exception("Цена должна быть больше 0");
}
purchaseModel.VendorId = APIClient.Vendor.Id;
APIClient.PostRequest($"{API_ROUTE}/createpurchase", purchaseModel);
}
[HttpGet]
public void PurchaseDelete(int id)
{
if (APIClient.Vendor == null)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
if (id <= 0)
{
throw new Exception("Некорректный идентификатор");
}
var purchase = APIClient.GetRequest<PurchaseViewModel>($"api/purchase/getpurchase?purchaseId={id}");
APIClient.PostRequest($"{API_ROUTE}/DeletePurchase", new PurchaseBindingModel
{
Id = id,
});
}
[HttpGet]
public IActionResult Purchases()
{
if (APIClient.Vendor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Purchases = APIClient.PostRequestWithResult<PurchaseSearchModel, List<PurchaseViewModel>>(
$"{API_ROUTE}/getpurchases", new() { VendorId = APIClient.Vendor.Id });
return View();
}
[HttpGet]
public void PurchaseUpdate([FromBody] PurchaseBindingModel purchaseModel)
{
if (APIClient.Vendor == null)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
purchaseModel.VendorId = APIClient.Vendor.Id;
APIClient.PostRequest("api/purchase/update", purchaseModel);
}
[HttpGet]
public string Report(DateTime dateFrom, DateTime dateTo)
{
if (APIClient.Vendor == null)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
return "NotImplement";
}
}
}