2024-06-20 04:18:28 +04:00
|
|
|
using Contracts.BindingModels;
|
|
|
|
using Contracts.Converters;
|
|
|
|
using Contracts.ViewModels;
|
2024-06-19 22:32:09 +04:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
|
|
using WebApp.Helpers;
|
|
|
|
|
|
|
|
namespace WebApp.Pages.User
|
|
|
|
{
|
2024-06-20 04:18:28 +04:00
|
|
|
[Authorize(Roles = Roles.User)]
|
2024-06-19 22:32:09 +04:00
|
|
|
public class SettingsModel : PageModel
|
|
|
|
{
|
2024-06-20 04:18:28 +04:00
|
|
|
[BindProperty]
|
|
|
|
public UserBindingModel UserModel { get; set; }
|
|
|
|
|
2024-06-19 22:32:09 +04:00
|
|
|
public void OnGet()
|
|
|
|
{
|
2024-06-20 04:18:28 +04:00
|
|
|
var id = this.GetUserId();
|
|
|
|
if (id is null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var userView = APIClient.GetRequest<UserViewModel>($"user/get?id={id}");
|
|
|
|
if (userView is null)
|
|
|
|
{
|
|
|
|
throw new Exception("User is not found.");
|
|
|
|
}
|
|
|
|
|
|
|
|
UserModel = UserConverter.ToBinding(userView);
|
|
|
|
}
|
|
|
|
|
|
|
|
public IActionResult OnPostDeleteProfile()
|
|
|
|
{
|
|
|
|
var id = this.GetUserId();
|
|
|
|
if (id is null)
|
|
|
|
{
|
|
|
|
throw new Exception("User not found!");
|
|
|
|
}
|
|
|
|
|
|
|
|
var response = APIClient.DeleteRequest($"user/delete?id={id}");
|
|
|
|
if (response is null)
|
|
|
|
{
|
|
|
|
throw new Exception("Something wrong LOL!");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.DeleteJWT();
|
|
|
|
|
|
|
|
return RedirectToPage("../Index");
|
|
|
|
}
|
|
|
|
|
|
|
|
public IActionResult OnPostAsync()
|
|
|
|
{
|
|
|
|
var response = APIClient.PatchRequest("user/update", UserModel);
|
|
|
|
|
|
|
|
if (response is null)
|
|
|
|
{
|
|
|
|
throw new Exception("Something wrong LOL!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return RedirectToPage("Index");
|
2024-06-19 22:32:09 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|