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 _logger; public ResumeController(ILogger logger) { _logger = logger; } [HttpGet] public async Task ResumeDetails(int? id) { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } var resume = await APIClient.GetRequestAsync($"api/resume/details?id={id}"); if (resume == null || !id.HasValue) { return View(); } var assessments = APIClient.GetRequest>($"api/assessment/listByResumeId?id={id}"); var criterions = APIClient.GetRequest>($"api/criterion/list"); resume.Assessments = assessments; ViewBag.Criterions = criterions; return View(resume); } [HttpGet] public async Task EditResume(int? id, int? vacancyId) { if (APIClient.User == null) { return RedirectToAction("Enter", "Home"); } ResumeViewModel model; if (id.HasValue) { model = await APIClient.GetRequestAsync($"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($"api/vacancy/details?id={model.VacancyId}"); var resume = APIClient.GetRequest($"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 }); } } }