CourseWork_KPO/CandidateReviewClientApp/Controllers/HomeController.cs

114 lines
3.8 KiB
C#
Raw Normal View History

using CandidateReviewClientApp;
using CandidateReviewClientApp.Models;
using CandidateReviewContracts.BindingModels;
using CandidateReviewContracts.ViewModels;
using CandidateReviewDataModels.Enums;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace CandidateReviewUserApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
return View();
}
[HttpGet]
public IActionResult Enter()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Enter(string login, string password)
{
string returnUrl = HttpContext.Request.Headers["Referer"].ToString();
try
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
{
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
APIClient.User = await APIClient.GetRequestAsync<UserViewModel>($"api/user/login?login={login}&password={password}");
if (APIClient.User == null)
{
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>/<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
if (APIClient.User?.CompanyId != null)
{
APIClient.Company = await APIClient.GetRequestAsync<CompanyViewModel>($"api/company/profile?id={APIClient.User?.CompanyId}");
}
return RedirectToAction("Index");
}
catch (Exception ex)
{
return RedirectToAction("Error", new { errorMessage = $"{ex.Message}", returnUrl });
}
}
[HttpGet]
public IActionResult Register()
{
return View();
}
[HttpPost]
public IActionResult Register(string login, string password, string surname, string name, string lastname)
{
string returnUrl = HttpContext.Request.Headers["Referer"].ToString();
try
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(surname) || string.IsNullOrEmpty(name))
{
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD>");
}
RoleEnum role = RoleEnum.<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;
if (login.Equals("tania.art03@gmail.com", StringComparison.OrdinalIgnoreCase))
{
role = RoleEnum.<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;
}
else
{
role = RoleEnum.<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;
}
APIClient.PostRequest("api/user/register", new UserBindingModel
{
Surname = surname,
Name = name,
LastName = lastname ?? null,
Email = login,
Password = password,
EmailConfirmed = false,
Role = role
});
return RedirectToAction("Enter");
}
catch (Exception ex)
{
return RedirectToAction("Error", new { errorMessage = $"{ex.Message}", returnUrl });
}
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error(string errorMessage, string returnUrl)
{
ViewBag.ErrorMessage = errorMessage ?? "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.";
ViewBag.ReturnUrl = returnUrl;
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}