@@ -31,6 +31,7 @@ use utoipa::{IntoParams, ToSchema};
|
||||
|
||||
use crate::movies::{pool, rescore, Accepted, ApiError, ErrorBody, Release};
|
||||
use crate::owners::Owner;
|
||||
use crate::search::tmdb_client;
|
||||
use crate::state::{AppState, EpisodeCommand};
|
||||
|
||||
/// A series with the status derived from its episodes (§4.2).
|
||||
@@ -38,6 +39,9 @@ use crate::state::{AppState, EpisodeCommand};
|
||||
pub struct Series {
|
||||
pub id: i64,
|
||||
pub tmdb_id: i64,
|
||||
/// The Torznab `tvdbid` (§6.1), when TMDB knows one. Null means indexer
|
||||
/// searches fall back to the title text query.
|
||||
pub tvdb_id: Option<i64>,
|
||||
pub title: String,
|
||||
pub year: Option<i64>,
|
||||
pub original_language: Option<String>,
|
||||
@@ -171,6 +175,7 @@ fn validate_overrides(value: &serde_json::Value) -> Result<(), ApiError> {
|
||||
struct SeriesRow {
|
||||
id: i64,
|
||||
tmdb_id: i64,
|
||||
tvdb_id: Option<i64>,
|
||||
title: String,
|
||||
year: Option<i64>,
|
||||
original_language: Option<String>,
|
||||
@@ -287,6 +292,7 @@ fn with_status(
|
||||
Series {
|
||||
id: row.id,
|
||||
tmdb_id: row.tmdb_id,
|
||||
tvdb_id: row.tvdb_id,
|
||||
title: row.title.clone(),
|
||||
year: row.year,
|
||||
original_language: row.original_language.clone(),
|
||||
@@ -353,7 +359,7 @@ async fn tv_by_series(
|
||||
}
|
||||
|
||||
async fn load_series_row(state: &AppState, id: i64) -> Result<SeriesRow, ApiError> {
|
||||
sqlx::query_as!(SeriesRow, r#"SELECT id AS "id!: i64", tmdb_id AS "tmdb_id!: i64", title AS "title!: String", year, original_language, root_id AS "root_id!: i64", auto_track AS "auto_track!: bool", overrides AS "overrides!: serde_json::Value", upstream_ended AS "upstream_ended!: bool", blocked AS "blocked!: bool" FROM series WHERE id = ?"#, id)
|
||||
sqlx::query_as!(SeriesRow, r#"SELECT id AS "id!: i64", tmdb_id AS "tmdb_id!: i64", tvdb_id, title AS "title!: String", year, original_language, root_id AS "root_id!: i64", auto_track AS "auto_track!: bool", overrides AS "overrides!: serde_json::Value", upstream_ended AS "upstream_ended!: bool", blocked AS "blocked!: bool" FROM series WHERE id = ?"#, id)
|
||||
.fetch_optional(pool(state)?)
|
||||
.await?
|
||||
.ok_or(ApiError::SeriesNotFound)
|
||||
@@ -417,11 +423,11 @@ pub async fn list(
|
||||
Query(query): Query<ListSeriesQuery>,
|
||||
) -> Result<Json<Vec<Series>>, ApiError> {
|
||||
let rows = if let Some(owner_id) = query.owner_id {
|
||||
sqlx::query_as!(SeriesRow, r#"SELECT s.id AS "id!: i64", s.tmdb_id AS "tmdb_id!: i64", s.title AS "title!: String", s.year, s.original_language, s.root_id AS "root_id!: i64", s.auto_track AS "auto_track!: bool", s.overrides AS "overrides!: serde_json::Value", s.upstream_ended AS "upstream_ended!: bool", s.blocked AS "blocked!: bool" FROM series s JOIN title_owners t ON t.title_kind = 'series' AND t.title_id = s.id WHERE t.owner_id = ? ORDER BY s.title, s.year, s.id"#, owner_id)
|
||||
sqlx::query_as!(SeriesRow, r#"SELECT s.id AS "id!: i64", s.tmdb_id AS "tmdb_id!: i64", s.tvdb_id, s.title AS "title!: String", s.year, s.original_language, s.root_id AS "root_id!: i64", s.auto_track AS "auto_track!: bool", s.overrides AS "overrides!: serde_json::Value", s.upstream_ended AS "upstream_ended!: bool", s.blocked AS "blocked!: bool" FROM series s JOIN title_owners t ON t.title_kind = 'series' AND t.title_id = s.id WHERE t.owner_id = ? ORDER BY s.title, s.year, s.id"#, owner_id)
|
||||
.fetch_all(pool(&state)?)
|
||||
.await?
|
||||
} else {
|
||||
sqlx::query_as!(SeriesRow, r#"SELECT id AS "id!: i64", tmdb_id AS "tmdb_id!: i64", title AS "title!: String", year, original_language, root_id AS "root_id!: i64", auto_track AS "auto_track!: bool", overrides AS "overrides!: serde_json::Value", upstream_ended AS "upstream_ended!: bool", blocked AS "blocked!: bool" FROM series ORDER BY title, year, id"#)
|
||||
sqlx::query_as!(SeriesRow, r#"SELECT id AS "id!: i64", tmdb_id AS "tmdb_id!: i64", tvdb_id, title AS "title!: String", year, original_language, root_id AS "root_id!: i64", auto_track AS "auto_track!: bool", overrides AS "overrides!: serde_json::Value", upstream_ended AS "upstream_ended!: bool", blocked AS "blocked!: bool" FROM series ORDER BY title, year, id"#)
|
||||
.fetch_all(pool(&state)?)
|
||||
.await?
|
||||
};
|
||||
@@ -466,9 +472,13 @@ pub async fn create(
|
||||
let overrides = serde_json::to_string(&input.overrides)
|
||||
.map_err(|error| ApiError::Invalid(error.to_string()))?;
|
||||
let title = input.title.trim();
|
||||
// §6.1: the TVDB id is what `t=tvsearch` is addressed by, but TMDB not
|
||||
// knowing one must not block adding the series — the search falls back
|
||||
// to the title text query until a refresh fills it in (#121).
|
||||
let tvdb_id = lookup_tvdb_id(&state, input.tmdb_id).await;
|
||||
let result = sqlx::query!(
|
||||
"INSERT INTO series (tmdb_id, title, year, original_language, root_id, auto_track, upstream_ended, blocked, overrides) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
input.tmdb_id, title, input.year, input.original_language, input.root_id,
|
||||
"INSERT INTO series (tmdb_id, tvdb_id, title, year, original_language, root_id, auto_track, upstream_ended, blocked, overrides) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
input.tmdb_id, tvdb_id, title, input.year, input.original_language, input.root_id,
|
||||
input.auto_track, input.upstream_ended, input.blocked, overrides
|
||||
)
|
||||
.execute(pool(&state)?)
|
||||
@@ -479,6 +489,16 @@ pub async fn create(
|
||||
))
|
||||
}
|
||||
|
||||
/// Best effort: `None` when TMDB has no id or cannot be reached.
|
||||
async fn lookup_tvdb_id(state: &AppState, tmdb_id: i64) -> Option<i64> {
|
||||
let client = tmdb_client(state).ok()?;
|
||||
let ids = client
|
||||
.series_external_ids(u32::try_from(tmdb_id).ok()?)
|
||||
.await
|
||||
.ok()?;
|
||||
ids.tvdb_id.map(i64::from)
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
get, path = "/api/series/{series_id}", tag = "series",
|
||||
params(("series_id" = i64, Path, description = "Series row id")),
|
||||
|
||||
Reference in New Issue
Block a user