CourseWork_KPO/CandidateReviewClientApp/Controllers/UserController.cs
2024-12-13 04:12:54 +04:00

152 lines
5.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 model = APIClient.GetRequest<UserViewModel>($"api/user/profile?id={id}");
if (model == null)
{
return Redirect("/Home/Index");
}
return View(model);
}
[HttpGet]
public IActionResult UserProfileEdit(int? id)
{
if (APIClient.User == null)
{
return Redirect("/Home/Enter");
}
if (APIClient.Company == null)
{
return Redirect("/Home/Index");
}
if (!id.HasValue)
{
var model = new UserViewModel
{
CompanyId = APIClient.Company.Id
};
return View(model);
}
else if (id.HasValue)
{
var employee = APIClient.GetRequest<UserViewModel?>($"api/user/profile?id={id}");
return View(employee);
}
else
{
var model = APIClient.GetRequest<UserViewModel?>($"api/user/profile?id={APIClient.User.Id}");
return View(model);
}
}
[HttpPost]
public IActionResult UserProfileEdit(UserBindingModel model)
{
string returnUrl = HttpContext.Request.Headers["Referer"].ToString();
try
{
if (model.Id != 0)
{
APIClient.PostRequest("api/user/update", model);
}
else
{
APIClient.PostRequest("api/user/register", model);
if (APIClient.Company != null)
{
APIClient.Company?.Employees.Add(new UserViewModel
{
Id = model.Id,
Surname = model.Surname,
Name = model.Name,
LastName = model.LastName,
CompanyId = APIClient.Company.Id,
Email = model.Email,
Password = model.Password,
EmailConfirmed = model.EmailConfirmed,
Role = CandidateReviewDataModels.Enums.RoleEnum.Сотрудник,
AvatarFilePath = model.AvatarFilePath,
PhoneNumber = model.PhoneNumber
});
}
return Redirect($"~/Company/CompanyProfile/{model.CompanyId}");
}
return Redirect($"/User/UserProfile/{model.Id}");
}
catch (Exception ex)
{
return RedirectToAction("Error", new { errorMessage = $"{ex.Message}", returnUrl });
}
}
public IActionResult DeleteEmployee(int id)
{
string returnUrl = HttpContext.Request.Headers["Referer"].ToString();
try
{
APIClient.PostRequest("api/user/delete", new UserBindingModel
{
Id = id
});
APIClient.Company = APIClient.GetRequest<CompanyViewModel?>($"api/company/profile?id={APIClient.User?.CompanyId}");
return Redirect($"~/Company/CompanyProfile");
}
catch (Exception ex)
{
return RedirectToAction("Error", new { errorMessage = $"{ex.Message}", returnUrl });
}
}
[HttpGet]
public void Logout()
{
APIClient.User = null;
APIClient.Company = null;
Response.Redirect("/Home/Enter");
}
[HttpPost]
public void Delete(UserBindingModel model)
{
if (APIClient.User == null)
{
throw new Exception("Доступно только авторизованным пользователям");
}
APIClient.PostRequest($"api/user/delete", model);
Response.Redirect("/Home/Enter");
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error(string errorMessage, string returnUrl)
{
ViewBag.ErrorMessage = errorMessage ?? "Произошла непредвиденная ошибка.";
ViewBag.ReturnUrl = returnUrl;
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}