Refactor SonarrController to share methods betweeen endpoints
This commit is contained in:
@@ -27,8 +27,8 @@ public class SonarrController : Controller
|
||||
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
}
|
||||
|
||||
private async Task<ObjectResult> GetSonarrSeriesInfo(SeriesInfo seriesInfo){
|
||||
HttpHelper httpHelper = new(ServerType.Sonarr);
|
||||
private async Task<ObjectResult> GetSeriesInfo(SeriesInfo seriesInfo, ServerType serverType){
|
||||
HttpHelper httpHelper = new(serverType);
|
||||
var responseBody = await httpHelper.SendHttpRequestAsync(
|
||||
HttpMethod.Get,
|
||||
$"/api/v3/series?tvdbId={Uri.EscapeDataString(seriesInfo.TvdbId ?? string.Empty)}"
|
||||
@@ -45,8 +45,8 @@ public class SonarrController : Controller
|
||||
return Ok(series);
|
||||
}
|
||||
|
||||
private async Task<ObjectResult> GetSonarrEpisodeInfo(SonarrSeries sonarrSeries){
|
||||
HttpHelper httpHelper = new(ServerType.Sonarr);
|
||||
private async Task<ObjectResult> GetEpisodeInfo(SonarrSeries sonarrSeries, ServerType serverType){
|
||||
HttpHelper httpHelper = new(serverType);
|
||||
var responseBody = await httpHelper.SendHttpRequestAsync(
|
||||
HttpMethod.Get,
|
||||
$"/api/v3/episode?seriesId={sonarrSeries.Id.ToString(CultureInfo.InvariantCulture)}"
|
||||
@@ -82,8 +82,8 @@ public class SonarrController : Controller
|
||||
return Ok(new EpisodeIdLists(episodeIds, episodeFileIds));
|
||||
}
|
||||
|
||||
[HttpPost("deleteSeriesFromSonarr")]
|
||||
public async Task<IActionResult> DeleteSeriesFromSonarr([FromBody] SeriesInfo seriesInfo){
|
||||
[HttpPost("deleteSeriesFromAnimeSonarr")]
|
||||
public async Task<IActionResult> DeleteSeriesFromAnimeSonarr([FromBody] SeriesInfo seriesInfo){
|
||||
|
||||
if (seriesInfo == null || string.IsNullOrEmpty(seriesInfo.TvdbId))
|
||||
{
|
||||
@@ -92,7 +92,7 @@ public class SonarrController : Controller
|
||||
|
||||
try
|
||||
{
|
||||
var sonarrSeriesInfoResult = await GetSonarrSeriesInfo(seriesInfo).ConfigureAwait(false);
|
||||
var sonarrSeriesInfoResult = await GetSeriesInfo(seriesInfo, ServerType.SonarrAnime).ConfigureAwait(false);
|
||||
|
||||
if(sonarrSeriesInfoResult.StatusCode != StatusCodes.Status200OK || sonarrSeriesInfoResult.Value is not SonarrSeries){
|
||||
return sonarrSeriesInfoResult;
|
||||
@@ -107,7 +107,7 @@ public class SonarrController : Controller
|
||||
TvdbId: retrievedSeries.TvdbId
|
||||
);
|
||||
|
||||
var episodesToPurgeResult = await GetSonarrEpisodeInfo(staleSeries).ConfigureAwait(false);
|
||||
var episodesToPurgeResult = await GetEpisodeInfo(staleSeries, ServerType.SonarrAnime).ConfigureAwait(false);
|
||||
if (episodesToPurgeResult.StatusCode != StatusCodes.Status200OK || episodesToPurgeResult.Value is not EpisodeIdLists)
|
||||
{
|
||||
return sonarrSeriesInfoResult;
|
||||
@@ -115,9 +115,9 @@ public class SonarrController : Controller
|
||||
|
||||
EpisodeIdLists episodesToPurge = (EpisodeIdLists)episodesToPurgeResult.Value;
|
||||
|
||||
await UnmonitorSeasons(staleSeries).ConfigureAwait(false);
|
||||
await UnmonitorEpisodeIds(episodesToPurge.EpisodeIds).ConfigureAwait(false);
|
||||
await DeleteEpisodeFiles(episodesToPurge.EpisodeFileIds).ConfigureAwait(false);
|
||||
await UnmonitorSeasons(staleSeries, ServerType.SonarrAnime).ConfigureAwait(false);
|
||||
await UnmonitorEpisodeIds(episodesToPurge.EpisodeIds, ServerType.SonarrAnime).ConfigureAwait(false);
|
||||
await DeleteEpisodeFiles(episodesToPurge.EpisodeFileIds, ServerType.SonarrAnime).ConfigureAwait(false);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
@@ -127,13 +127,58 @@ public class SonarrController : Controller
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<ObjectResult> UnmonitorSeasons(SonarrSeries staleSeries){
|
||||
[HttpPost("deleteSeriesFromSonarr")]
|
||||
public async Task<IActionResult> DeleteSeriesFromSonarr([FromBody] SeriesInfo seriesInfo){
|
||||
|
||||
if (seriesInfo == null || string.IsNullOrEmpty(seriesInfo.TvdbId))
|
||||
{
|
||||
return BadRequest("Invalid series information provided.");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var sonarrSeriesInfoResult = await GetSeriesInfo(seriesInfo, ServerType.Sonarr).ConfigureAwait(false);
|
||||
|
||||
if(sonarrSeriesInfoResult.StatusCode != StatusCodes.Status200OK || sonarrSeriesInfoResult.Value is not SonarrSeries){
|
||||
return sonarrSeriesInfoResult;
|
||||
}
|
||||
|
||||
SonarrSeries retrievedSeries = (SonarrSeries)sonarrSeriesInfoResult.Value;
|
||||
SonarrSeries staleSeries = new(
|
||||
Id: retrievedSeries.Id,
|
||||
Title: retrievedSeries.Title,
|
||||
Seasons: [.. seriesInfo.Seasons.Select(season => new Season(SeasonNumber: int.Parse(season, CultureInfo.InvariantCulture)))],
|
||||
Ended: retrievedSeries.Ended,
|
||||
TvdbId: retrievedSeries.TvdbId
|
||||
);
|
||||
|
||||
var episodesToPurgeResult = await GetEpisodeInfo(staleSeries, ServerType.Sonarr).ConfigureAwait(false);
|
||||
if (episodesToPurgeResult.StatusCode != StatusCodes.Status200OK || episodesToPurgeResult.Value is not EpisodeIdLists)
|
||||
{
|
||||
return sonarrSeriesInfoResult;
|
||||
}
|
||||
|
||||
EpisodeIdLists episodesToPurge = (EpisodeIdLists)episodesToPurgeResult.Value;
|
||||
|
||||
await UnmonitorSeasons(staleSeries, ServerType.Sonarr).ConfigureAwait(false);
|
||||
await UnmonitorEpisodeIds(episodesToPurge.EpisodeIds, ServerType.Sonarr).ConfigureAwait(false);
|
||||
await DeleteEpisodeFiles(episodesToPurge.EpisodeFileIds, ServerType.Sonarr).ConfigureAwait(false);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"An unexpected error occurred. {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<ObjectResult> UnmonitorSeasons(SonarrSeries staleSeries, ServerType serverType){
|
||||
if (staleSeries == null)
|
||||
{
|
||||
return BadRequest("No stale series provided.");
|
||||
}
|
||||
|
||||
HttpHelper httpHelper = new(ServerType.Sonarr);
|
||||
HttpHelper httpHelper = new(serverType);
|
||||
var series = await httpHelper.SendHttpRequestAsync(
|
||||
HttpMethod.Get,
|
||||
$"/api/v3/series/{staleSeries.Id}"
|
||||
@@ -179,14 +224,14 @@ public class SonarrController : Controller
|
||||
return Ok(responseBody);
|
||||
}
|
||||
|
||||
private async Task<ObjectResult> DeleteEpisodeFiles(IReadOnlyList<int> episodeFileIds)
|
||||
private async Task<ObjectResult> DeleteEpisodeFiles(IReadOnlyList<int> episodeFileIds, ServerType serverType)
|
||||
{
|
||||
if (episodeFileIds == null || episodeFileIds.Count == 0)
|
||||
{
|
||||
return BadRequest("No episode file IDs provided.");
|
||||
}
|
||||
|
||||
HttpHelper httpHelper = new(ServerType.Sonarr);
|
||||
HttpHelper httpHelper = new(serverType);
|
||||
var responseBody = await httpHelper.SendHttpRequestAsync(
|
||||
HttpMethod.Delete,
|
||||
"/api/v3/episodefile/bulk",
|
||||
@@ -196,7 +241,7 @@ public class SonarrController : Controller
|
||||
return Ok(responseBody);
|
||||
}
|
||||
|
||||
private async Task<ObjectResult> UnmonitorEpisodeIds(IReadOnlyList<int> episodeIds)
|
||||
private async Task<ObjectResult> UnmonitorEpisodeIds(IReadOnlyList<int> episodeIds, ServerType serverType)
|
||||
{
|
||||
if (episodeIds == null || episodeIds.Count == 0)
|
||||
{
|
||||
@@ -204,7 +249,7 @@ public class SonarrController : Controller
|
||||
}
|
||||
|
||||
|
||||
HttpHelper httpHelper = new(ServerType.Sonarr);
|
||||
HttpHelper httpHelper = new(serverType);
|
||||
var responseBody = await httpHelper.SendHttpRequestAsync(
|
||||
HttpMethod.Put,
|
||||
"/api/v3/episode/monitor",
|
||||
|
||||
Reference in New Issue
Block a user