CourseWork_KPO/CandidateReviewClientApp/Controllers/ResumeController.cs

160 lines
6.0 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 ResumeController : Controller
{
private readonly ILogger<ResumeController> _logger;
public ResumeController(ILogger<ResumeController> logger)
{
_logger = logger;
}
[HttpGet]
public async Task<IActionResult> ResumeDetails(int? id)
{
if (APIClient.User == null)
{
return Redirect("~/Home/Enter");
}
var resume = await APIClient.GetRequestAsync<ResumeViewModel?>($"api/resume/details?id={id}");
if (resume == null || !id.HasValue)
{
return View();
}
var assessments = APIClient.GetRequest<List<AssessmentViewModel>>($"api/assessment/listByResumeId?id={id}");
var criterions = APIClient.GetRequest<List<CriterionViewModel>>($"api/criterion/list");
resume.Assessments = assessments;
ViewBag.Criterions = criterions;
return View(resume);
}
[HttpGet]
public async Task<IActionResult> EditResume(int? id, int? vacancyId)
{
if (APIClient.User == null)
{
return RedirectToAction("Enter", "Home");
}
ResumeViewModel model;
if (id.HasValue)
{
model = await APIClient.GetRequestAsync<ResumeViewModel>($"api/resume/details?id={id}");
if (model == null)
{
return NotFound();
}
}
else
{
model = new ResumeViewModel { UserId = APIClient.User.Id, VacancyId = vacancyId ?? 0 };
}
return View(model);
}
[HttpPost]
public IActionResult EditResume(ResumeBindingModel model, bool isDraft)
{
string returnUrl = HttpContext.Request.Headers["Referer"].ToString();
try
{
if (APIClient.User == null)
{
throw new Exception("Доступно только авторизованным пользователям");
}
if (model.Id != 0)
{
if (isDraft)
{
model.Status = CandidateReviewDataModels.Enums.ResumeStatusEnum.Черновик;
}
else
{
model.Status = CandidateReviewDataModels.Enums.ResumeStatusEnum.Обрабатывается;
}
APIClient.PostRequest("api/resume/update", model);
}
else
{
model.UserId = APIClient.User.Id;
if (isDraft)
{
model.Status = CandidateReviewDataModels.Enums.ResumeStatusEnum.Черновик;
}
else
{
model.Status = CandidateReviewDataModels.Enums.ResumeStatusEnum.Обрабатывается;
}
var vacancy = APIClient.GetRequest<VacancyViewModel>($"api/vacancy/details?id={model.VacancyId}");
var resume = APIClient.GetRequest<ResumeViewModel>($"api/resume/check?userId={model.UserId}&vacancyId={model.VacancyId}");
if (resume == null)
{
APIClient.PostRequest("api/resume/create", model);
if (APIClient.User != null)
{
APIClient.User?.Resumes.Add(new ResumeViewModel
{
Id = model.Id
});
}
else
{
throw new Exception("Пользователь не найден");
}
if (vacancy != null)
{
vacancy.Resumes.Add(new ResumeViewModel
{
Id = model.Id
});
}
else
{
throw new Exception("Вакансия не найдена");
}
}
else
{
throw new Exception("Вы уже создавали резюме на эту вакансию!");
}
}
return Redirect($"~/User/UserProfile/{model.UserId}");
}
catch (Exception ex)
{
return RedirectToAction("Error", new { errorMessage = $"{ex.Message}", returnUrl });
}
}
public IActionResult Delete(int id)
{
string returnUrl = HttpContext.Request.Headers["Referer"].ToString();
try
{
APIClient.PostRequest($"api/resume/delete", new ResumeBindingModel { Id = id });
return Redirect("~/User/UserProfile");
}
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 ?? "Произошла непредвиденная ошибка.";
ViewBag.ReturnUrl = returnUrl;
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}