CourseWork_KPO/CandidateReviewClientApp/Controllers/VacancyController.cs

93 lines
3.0 KiB
C#

using CandidateReviewContracts.BindingModels;
using CandidateReviewContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace CandidateReviewClientApp.Controllers
{
public class VacancyController : Controller
{
private readonly ILogger<VacancyController> _logger;
public VacancyController(ILogger<VacancyController> logger)
{
_logger = logger;
}
[HttpGet]
public IActionResult Vacancy(int? id)
{
if (APIClient.User == null)
{
return Redirect("~/Home/Enter");
}
if (id.HasValue)
{
APIClient.Vacancy = APIClient.GetRequest<VacancyViewModel?>($"api/vacancy/details?id={id}");
}
var model = APIClient.Vacancy;
return View(model);
}
[HttpGet]
public IActionResult EditVacancy(int? id)
{
if (APIClient.User == null)
{
return Redirect("~/Home/Enter");
}
if (!id.HasValue)
{
return View(new VacancyViewModel());
}
var model = APIClient.GetRequest<VacancyViewModel?>($"api/vacancy/details?id={id}");
return View(model);
}
[HttpPost]
public void EditVacancy(VacancyBindingModel model)
{
if (APIClient.User == null)
{
throw new Exception("Доступно только авторизованным пользователям");
}
if (model.Id != 0)
{
APIClient.PostRequest("api/vacancy/update", model);
}
else
{
APIClient.PostRequest("api/vacancy/create", model);
if (APIClient.Company != null)
{
APIClient.Company?.Vacancies.Add(new VacancyViewModel
{
Id = model.Id,
CompanyId = model.CompanyId,
CreatedAt = model.CreatedAt,
Description = model.Description,
JobTitle = model.JobTitle,
JobType = model.JobType,
Requirements = model.Requirements,
Responsibilities = model.Responsibilities,
Salary = model.Salary,
Status = model.Status,
Tags = model.Tags
});
}
}
Response.Redirect($"/Company/CompanyProfile/{model.CompanyId}");
}
[HttpPost]
public void Delete(int id)
{
if (APIClient.User == null)
{
throw new Exception("Доступно только авторизованным пользователям");
}
APIClient.PostRequest($"api/vacancy/delete", new VacancyBindingModel { Id = id });
Response.Redirect("/Home/Index");
}
}
}