using System.Collections.Generic; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using LCU.Api.Internal.Dispatcher.Services; using LCU.Library.Internal.DispatcherModels.Models.Requests; namespace LCU.Api.Internal.Dispatcher.Controllers; [Route("api/[controller]")] [ApiController] public class JobController : ControllerBase { private readonly IJobService _jobService; public JobController(IJobService jobService) { _jobService = jobService; } [HttpPost("GetFilteredJobs")] [ProducesResponseType(typeof(PagedResponse), 200)] [ProducesResponseType(500)] public async Task GetFilteredJobs([FromBody] Filter filter) { try { var filterResult = await _jobService.GetFilteredJobs(filter); var result = new PagedResponse(filterResult, filter); return Ok(result); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpGet(Name = "GetAllJobs")] [ProducesResponseType(typeof(IEnumerable), 200)] [ProducesResponseType(500)] public async Task Get() { try { var result = await _jobService.GetAsync(); return Ok(result); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpGet("GetInactiveJobs", Name = "GetInactiveJobs")] [ProducesResponseType(typeof(IEnumerable), 200)] [ProducesResponseType(500)] public async Task GetInactive() { try { var result = await _jobService.GetInactiveAsync(); return Ok(result); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpGet("GetInactiveJobsExcludeSoftDeleted", Name = "GetInactiveJobsExcludeSoftDeleted")] [ProducesResponseType(typeof(IEnumerable), 200)] [ProducesResponseType(500)] public async Task GetInactiveExcludeSoftDeleted() { try { var result = await _jobService.GetInactiveExcludeSoftDeletedTrackedAsync(); return Ok(result); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpGet("GetSoftDeletedJobs", Name = "GetSoftDeletedJobs")] [ProducesResponseType(typeof(IEnumerable), 200)] [ProducesResponseType(500)] public async Task GetSoftDeleted(DateTime cutOffDate) { try { var result = await _jobService.GetSoftDeletedTrackedAsync(cutOffDate); return Ok(result); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpGet("GetJobByIdExcludingWorkItems", Name = "GetJobByIdExcludingWorkItems")] [ProducesResponseType(typeof(Job), 200)] [ProducesResponseType(500)] public async Task GetJobByIdExcludingWorkItems(int jobId) { try { var result = await _jobService.GetByIdExcludingWorkItemsAsync(jobId); return Ok(result); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpPut("RunJobNow", Name = "RunJobNow")] [ProducesResponseType(typeof(Job), 200)] [ProducesResponseType(500)] public async Task RunJobNow([FromBody] Job job) { try { var result = await _jobService.RunJobNowAsync(job); return Ok(result); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpPut("RunJobNowById", Name = "RunJobNowById")] [ProducesResponseType(typeof(Job), 200)] [ProducesResponseType(500)] public async Task RunJobNowById(int id) { try { var result = await _jobService.RunJobNowByIdAsync(id); return Ok(result); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpPut("DeactivateJob", Name = "DeactivateJob")] [ProducesResponseType(typeof(Job), 200)] [ProducesResponseType(500)] public async Task DeactivateJob([FromBody] Job job) { try { var result = await _jobService.DeactivateJobAsync(job); return Ok(result); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpPut("RescheduleJob", Name = "RescheduleJob")] [ProducesResponseType(typeof(Job), 200)] [ProducesResponseType(500)] public async Task RescheduleJob([FromBody] Job job) { try { var result = await _jobService.RescheduleJobAsync(job); return Ok(result); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpPut("RemoveSoftDeleteById", Name = "RemoveSoftDeleteById")] [ProducesResponseType(typeof(Job), 200)] [ProducesResponseType(500)] public async Task RemoveSoftDeleteById(int id) { try { var result = await _jobService.RemoveSoftDeleteById(id); return Ok(result); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpDelete()] [ProducesResponseType(200)] [ProducesResponseType(400)] [ProducesResponseType(500)] public async Task Delete(int jobId) { try { if (jobId <= 0) { return BadRequest("Invalid Id"); } await _jobService.DeleteAsync(jobId); return Ok(); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpPost("Copy")] [ProducesResponseType(typeof(Job), 200)] [ProducesResponseType(400)] [ProducesResponseType(500)] public async Task Copy(CopyJobRequest request) { try { if (request == null) { return BadRequest("Request cannot be null"); } var job = await _jobService.CopyJobAsync(request); return Ok(job); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } }