PIAPS_CW/WebApp/Pages/Cart.cshtml.cs

61 lines
2.0 KiB
C#

using Contracts.BindingModels;
using Contracts.ViewModels;
using DataModels.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Routing.Constraints;
using System.Diagnostics.Eventing.Reader;
using WebApp.Helpers;
namespace WebApp.Pages
{
[Authorize(Roles = Roles.User)]
public class CartModel : PageModel
{
[BindProperty]
public List<CartItemBindingModel> cartItems { get; set; }
public List<CartItemViewModel> cartItemsView { get; set; }
public PaymentViewModel paymentViewModel { get; set; }
public void OnGet()
{
var id = this.GetUserId();
if (id is null)
{
return;
}
cartItemsView = APIClient.GetRequest<List<CartItemViewModel>>($"cartitem/GetFullList?userId={id}");
}
public ProductViewModel GetProduct(Guid cartItemId)
{
return APIClient.GetRequest<ProductViewModel>($"CartItem/GetProduct?cartItemId={cartItemId}");
}
public void OnPostDeleteCartItem(Guid Id)
{
var response = APIClient.DeleteRequest($"CartItem/Delete?id={Id}");
if (response is null)
{
throw new Exception("Something wrong LOL!");
}
OnGet();
}
public IActionResult OnPostGoToPayment(int ProductCount, Guid UserId, double Cost)
{
paymentViewModel = new PaymentViewModel()
{
UserId = UserId,
Cost = Cost,
ProductCount = ProductCount,
UserFirstName = string.Empty,
UserSecondName = string.Empty,
};
if (ProductCount <= 0 || UserId == Guid.Empty || Cost <= 0)
{
throw new Exception("Something wrong LOL!");
}
return RedirectToPage("Purchase", paymentViewModel);
}
}
}