Files
monorepo/LCU.API.Internal.Dispatcher/LCU.Api.Internal.Dispatcher/Controllers/JobController.cs
T
2026-03-24 15:26:03 -05:00

304 lines
9.2 KiB
C#

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<Job>), 200)]
[ProducesResponseType(500)]
public async Task<IActionResult> GetFilteredJobs([FromBody] Filter filter)
{
try
{
var filterResult = await _jobService.GetFilteredJobs(filter);
var result = new PagedResponse<Job>(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<Job>), 200)]
[ProducesResponseType(500)]
public async Task<IActionResult> 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<Job>), 200)]
[ProducesResponseType(500)]
public async Task<IActionResult> 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<Job>), 200)]
[ProducesResponseType(500)]
public async Task<IActionResult> 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<Job>), 200)]
[ProducesResponseType(500)]
public async Task<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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);
}
}
}