CourseWork_KPO/CandidateReviewClientApp/Controllers/AssessmentController.cs

86 lines
3.4 KiB
C#

using CandidateReviewClientApp.Models;
using CandidateReviewContracts.BindingModels;
using CandidateReviewContracts.SearchModels;
using CandidateReviewContracts.ViewModels;
using CandidateReviewDataModels.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace CandidateReviewClientApp.Controllers
{
public class AssessmentController : Controller
{
private readonly ILogger<AssessmentController> _logger;
public AssessmentController(ILogger<AssessmentController> logger)
{
_logger = logger;
}
[HttpPost]
public async Task<IActionResult> AddAssessmentCriterion(int resumeId, int[] criterion, int[] value, string comment)
{
string returnUrl = HttpContext.Request.Headers["Referer"].ToString();
try
{
if (APIClient.User == null)
{
throw new Exception("Необходима авторизация");
}
if (criterion == null || value == null || criterion.Length != value.Length)
{
throw new ArgumentException("Массивы критериев и оценок должны быть не null и одинаковой длины.");
}
var userId = APIClient.User?.Id;
var assessmentData = new Dictionary<int, (ICriterionModel, int)>();
var assessmentModel = new AssessmentBindingModel
{
ResumeId = resumeId,
UserId = userId,
Comment = comment,
AssessmentCriterions = new Dictionary<int, (ICriterionModel, int)>()
};
var assessmentId = await APIClient.PostRequestAsync("api/assessment/create", assessmentModel);
for (int i = 0; i < criterion.Length; i++)
{
if (value[i] < 1 || value[i] > 5)
{
throw new ArgumentException($"Оценка для критерия {criterion[i]} должна быть от 1 до 5.");
}
if (assessmentData.ContainsKey(assessmentId))
{
assessmentData[assessmentId] = (new CriterionViewModel { Id = criterion[i] }, value[i]);
}
else
{
assessmentData.Add(assessmentId, (new CriterionViewModel { Id = criterion[i] }, value[i]));
}
}
APIClient.PostRequest("api/assessment/AddCriterionToAssessment", assessmentData);
return Redirect($"~/Resume/ResumeDetails/{resumeId}");
}
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 });
}
}
}