CourseWork_KPO/CandidateReviewClientApp/Controllers/UserController.cs

108 lines
3.3 KiB
C#

using CandidateReviewClientApp.Models;
using CandidateReviewContracts.BindingModels;
using CandidateReviewContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace CandidateReviewClientApp.Controllers
{
public class UserController : Controller
{
private readonly ILogger<UserController> _logger;
public UserController(ILogger<UserController> logger)
{
_logger = logger;
}
[HttpGet]
public IActionResult UserProfile(int? id)
{
var userId = id ?? APIClient.User.Id;
var model = APIClient.GetRequest<UserViewModel>($"api/user/profile?id={userId}");
if (model == null)
{
return RedirectToAction("/Home/Index");
}
return View(model);
}
[HttpGet]
public IActionResult UserProfileEdit()
{
if (APIClient.User == null)
{
return Redirect("/Home/Enter");
}
var model = APIClient.GetRequest<UserViewModel?>($"api/user/profile?id={APIClient.User.Id}");
if (model == null)
{
return RedirectToAction("/Home/Index");
}
return View(model);
}
[HttpPost]
public void UserProfileEdit(UserBindingModel model)
{
APIClient.PostRequest("api/user/update", new UserBindingModel
{
Id = model.Id,
Surname = model.Surname,
Name = model.Name,
LastName = model.LastName,
CompanyId = model.CompanyId,
Email = model.Email,
Password = model.Password,
EmailConfirmed = model.EmailConfirmed,
Role = model.Role,
AvatarFilePath = model.AvatarFilePath,
PhoneNumber = model.PhoneNumber
});
Response.Redirect($"/User/UserProfile/{model.Id}");
}
[HttpGet]
public void Logout()
{
APIClient.User = null;
Response.Redirect("/Home/Enter");
}
[HttpPost]
public void Delete(UserBindingModel model)
{
if (APIClient.User == null)
{
throw new Exception("Доступно только авторизованным пользователям");
}
APIClient.PostRequest($"api/user/delete", new UserBindingModel {
Id = model.Id,
Surname = model.Surname,
Name = model.Name,
LastName = model.LastName,
CompanyId = model.CompanyId,
Email = model.Email,
Password = model.Password,
EmailConfirmed = model.EmailConfirmed,
Role = model.Role,
AvatarFilePath = model.AvatarFilePath,
PhoneNumber = model.PhoneNumber
});
Response.Redirect("/Home/Enter");
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}