99 lines
3.3 KiB
C#
99 lines
3.3 KiB
C#
using Contracts.BindingModels;
|
|
using Contracts.Converters;
|
|
using Contracts.ViewModels;
|
|
using DataModels.Enums;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
using WebApp.Helpers;
|
|
using WebApp.Pages.User;
|
|
|
|
namespace WebApp.Pages
|
|
{
|
|
[Authorize(Roles = Roles.User)]
|
|
public class PurchaseModel : PageModel
|
|
{
|
|
[BindProperty]
|
|
public PurchaseBindingModel purchaseModel { get; set; }
|
|
public PaymentViewModel paymentViewModel { get; set; }
|
|
public UserViewModel userModel { get; set; }
|
|
[BindProperty]
|
|
public bool bonus { get; set; }
|
|
public void OnGet(PaymentViewModel model)
|
|
{
|
|
var id = this.GetUserId();
|
|
if (id is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
userModel = APIClient.GetRequest<UserViewModel>($"user/get?id={id}");
|
|
if (userModel is null)
|
|
{
|
|
throw new Exception("User is not found.");
|
|
}
|
|
|
|
paymentViewModel = model;
|
|
paymentViewModel.Bonus = userModel.Bonus;
|
|
paymentViewModel.UserFirstName = userModel.FirstName;
|
|
paymentViewModel.UserSecondName = userModel.SecondName;
|
|
paymentViewModel.Email = userModel.Email;
|
|
}
|
|
|
|
public IActionResult OnPostAsync(Guid userId, double cost, string PaymentMethod)
|
|
{
|
|
purchaseModel.Cost = cost;
|
|
if (PaymentMethod == "Online")
|
|
purchaseModel.IsPaid = true;
|
|
else
|
|
purchaseModel.IsPaid = false;
|
|
|
|
userModel = APIClient.GetRequest<UserViewModel>($"user/get?id={userId}");
|
|
UserBindingModel userBinding = UserConverter.ToBinding(userModel);
|
|
|
|
var bonusModel = new BonusUpdateBindingModel();
|
|
bonusModel.UserId = userId;
|
|
bonusModel.BonusPlus = Convert.ToInt32(purchaseModel.Cost) / 100;
|
|
|
|
if (bonus)
|
|
{
|
|
bonusModel.BonusMinus = userModel.Bonus;
|
|
purchaseModel.Cost -= userModel.Bonus;
|
|
|
|
var response_update1 = APIClient.PatchRequest($"user/updatebonus", bonusModel);
|
|
|
|
if (response_update1 is null || response_update1 is not string)
|
|
{
|
|
throw new Exception("Something wrong LOL!");
|
|
}
|
|
}
|
|
var response_update = APIClient.PatchRequest($"user/updatebonus", bonusModel);
|
|
|
|
if (response_update is null || response_update is not string)
|
|
{
|
|
throw new Exception("Something wrong LOL!");
|
|
}
|
|
|
|
purchaseModel.Id = new Guid();
|
|
purchaseModel.Status = PurchaseStatus.Processing;
|
|
purchaseModel.DateCreated = DateTime.Now;
|
|
|
|
var response_create = APIClient.PostRequest($"Purchase/Create/", purchaseModel);
|
|
|
|
if (response_create is null || response_create is not string)
|
|
{
|
|
throw new Exception("Something wrong LOL!");
|
|
}
|
|
|
|
var response_delete = APIClient.DeleteRequest($"CartItem/DeleteAll/");
|
|
|
|
if (response_delete is null || response_delete is not string)
|
|
{
|
|
throw new Exception("Something wrong LOL!");
|
|
}
|
|
|
|
return RedirectToPage("/User/Purchases");
|
|
}
|
|
}
|
|
}
|