CourseWork_KPO/CandidateReviewClientApp/Controllers/UserController.cs

146 lines
4.6 KiB
C#
Raw Normal View History

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(int? id)
{
if (APIClient.User == null)
{
return Redirect("/Home/Enter");
}
if (APIClient.Company == null)
{
return Redirect("/Home/Index");
}
if (!id.HasValue)
{
return View(new UserViewModel());
}
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 void UserProfileEdit(UserBindingModel model)
{
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 = model.CompanyId,
Email = model.Email,
Password = model.Password,
EmailConfirmed = model.EmailConfirmed,
Role = CandidateReviewDataModels.Enums.RoleEnum.Сотрудник,
AvatarFilePath = model.AvatarFilePath,
PhoneNumber = model.PhoneNumber
});
}
Response.Redirect($"/Company/CompanyProfile/{model.CompanyId}");
return;
}
Response.Redirect($"/User/UserProfile/{model.Id}");
}
public IActionResult DeleteEmployee(int id)
{
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");
}
[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 });
}
}
}