using Oqtane.Models;
using System.Threading.Tasks;
using System.Net.Http;
using System.Linq;
using System.Collections.Generic;
using Oqtane.Documentation;
using Oqtane.Shared;
namespace Oqtane.Services
{
///
/// Service to manage jobs ()
///
public interface IJobService
{
///
/// Returns a list of all jobs
///
///
Task> GetJobsAsync();
///
/// Return a specific job
///
///
///
Task GetJobAsync(int jobId);
///
/// Adds a new job
///
///
///
Task AddJobAsync(Job job);
///
/// Updates an existing job
///
///
///
Task UpdateJobAsync(Job job);
///
/// Delete an existing job
///
///
///
Task DeleteJobAsync(int jobId);
///
/// Starts the given job
///
///
///
Task StartJobAsync(int jobId);
///
/// Stops the given job
///
///
///
Task StopJobAsync(int jobId);
}
[PrivateApi("Don't show in the documentation, as everything should use the Interface")]
public class JobService : ServiceBase, IJobService
{
public JobService(HttpClient http, SiteState siteState) : base(http, siteState) { }
private string Apiurl => CreateApiUrl("Job");
public async Task> GetJobsAsync()
{
List jobs = await GetJsonAsync>(Apiurl);
return jobs.OrderBy(item => item.Name).ToList();
}
public async Task GetJobAsync(int jobId)
{
return await GetJsonAsync($"{Apiurl}/{jobId}");
}
public async Task AddJobAsync(Job job)
{
return await PostJsonAsync(Apiurl, job);
}
public async Task UpdateJobAsync(Job job)
{
return await PutJsonAsync($"{Apiurl}/{job.JobId}", job);
}
public async Task DeleteJobAsync(int jobId)
{
await DeleteAsync($"{Apiurl}/{jobId}");
}
public async Task StartJobAsync(int jobId)
{
await GetAsync($"{Apiurl}/start/{jobId}");
}
public async Task StopJobAsync(int jobId)
{
await GetAsync($"{Apiurl}/stop/{jobId}");
}
}
}