Support flix collections

This commit is contained in:
2025-12-08 20:33:13 -08:00
parent c2fb43de30
commit dd2cf5c942
15 changed files with 277 additions and 137 deletions
+12
View File
@@ -0,0 +1,12 @@
use clap::Subcommand;
#[derive(Subcommand)]
pub enum AddCommand {
/// Process a flix collection
Collection {
#[arg(value_name = "TITLE")]
title: String,
#[arg(value_name = "OVERVIEW")]
overview: String,
},
}
+47 -5
View File
@@ -3,6 +3,7 @@ use std::path::PathBuf;
use anyhow::{Result, anyhow};
use clap::{Parser, Subcommand};
pub mod flix;
pub mod tmdb;
#[derive(Parser)]
@@ -57,17 +58,17 @@ pub enum Command {
/// Add new items to the database
Add {
#[command(subcommand)]
command: BackendCommand,
command: AddCommand,
},
/// Update an existing item in the database
Update {
#[command(subcommand)]
command: BackendCommand,
command: UpdateCommand,
},
/// Delete an existing item in the database
Delete {
#[command(subcommand)]
command: BackendCommand,
command: DeleteCommand,
},
/// Create a toml backup of the database
Backup {
@@ -84,7 +85,12 @@ pub enum Command {
}
#[derive(Subcommand)]
pub enum BackendCommand {
pub enum AddCommand {
/// Use the flix backend
Flix {
#[command(subcommand)]
command: flix::AddCommand,
},
/// Use the TMDB backend
Tmdb {
#[command(subcommand)]
@@ -92,7 +98,43 @@ pub enum BackendCommand {
},
}
impl From<tmdb::Command> for BackendCommand {
impl From<flix::AddCommand> for AddCommand {
fn from(value: flix::AddCommand) -> Self {
Self::Flix { command: value }
}
}
impl From<tmdb::Command> for AddCommand {
fn from(value: tmdb::Command) -> Self {
Self::Tmdb { command: value }
}
}
#[derive(Subcommand)]
pub enum UpdateCommand {
/// Use the TMDB backend
Tmdb {
#[command(subcommand)]
command: tmdb::Command,
},
}
impl From<tmdb::Command> for UpdateCommand {
fn from(value: tmdb::Command) -> Self {
Self::Tmdb { command: value }
}
}
#[derive(Subcommand)]
pub enum DeleteCommand {
/// Use the TMDB backend
Tmdb {
#[command(subcommand)]
command: tmdb::Command,
},
}
impl From<tmdb::Command> for DeleteCommand {
fn from(value: tmdb::Command) -> Self {
Self::Tmdb { command: value }
}
+13 -15
View File
@@ -7,7 +7,7 @@ use clap::Parser;
use tokio::fs;
mod cli;
use cli::{BackendCommand, Cli, Command};
use cli::{AddCommand, Cli, Command, DeleteCommand, UpdateCommand};
mod config;
use config::Config;
@@ -53,11 +53,14 @@ async fn exec_init(database_path: String) -> Result<()> {
Ok(())
}
async fn exec_add(client: Client, database_path: String, command: BackendCommand) -> Result<()> {
async fn exec_add(client: Client, database_path: String, command: AddCommand) -> Result<()> {
let database = db::open(database_path).await?;
match command {
BackendCommand::Tmdb { command } => {
AddCommand::Flix { command } => {
run::flix::add(database.as_ref(), command).await?;
}
AddCommand::Tmdb { command } => {
run::tmdb::add(client, database.as_ref(), command).await?;
}
}
@@ -65,11 +68,11 @@ async fn exec_add(client: Client, database_path: String, command: BackendCommand
Ok(())
}
async fn exec_update(client: Client, database_path: String, command: BackendCommand) -> Result<()> {
async fn exec_update(client: Client, database_path: String, command: UpdateCommand) -> Result<()> {
let database = db::open(database_path).await?;
match command {
BackendCommand::Tmdb { command } => {
UpdateCommand::Tmdb { command } => {
run::tmdb::update(client, database.as_ref(), command).await?;
}
}
@@ -77,16 +80,11 @@ async fn exec_update(client: Client, database_path: String, command: BackendComm
Ok(())
}
async fn exec_delete(client: Client, database_path: String, command: BackendCommand) -> Result<()> {
let database = db::open(database_path).await?;
match command {
BackendCommand::Tmdb { command } => {
run::tmdb::delete(client, database.as_ref(), command).await?;
}
}
Ok(())
async fn exec_delete(client: Client, database_path: String, command: DeleteCommand) -> Result<()> {
_ = client;
_ = database_path;
_ = command;
unimplemented!()
}
async fn exec_backup(database_path: String, output: PathBuf) -> Result<()> {
+41
View File
@@ -0,0 +1,41 @@
use flix::db::entity;
use flix::model::id::CollectionId;
use anyhow::Result;
use sea_orm::ActiveValue::{NotSet, Set};
use sea_orm::{ActiveModelTrait, DatabaseConnection, DbErr, TransactionError, TransactionTrait};
use crate::cli::flix::AddCommand;
pub async fn add(db: &DatabaseConnection, command: AddCommand) -> Result<()> {
match command {
AddCommand::Collection { title, overview } => {
let result: Result<CollectionId, TransactionError<DbErr>> = db
.transaction(|txn| {
let title = title.clone();
Box::pin(async move {
let flix = entity::info::collections::ActiveModel {
id: NotSet,
title: Set(title),
overview: Set(overview),
}
.insert(txn)
.await?;
Ok(flix.id)
})
})
.await;
let flix_id = match result {
Ok(id) => id,
Err(TransactionError::Connection(err)) => Err(err)?,
Err(TransactionError::Transaction(err)) => Err(err)?,
};
println!("Created Collection: {} [{}]", title, flix_id.into_raw());
Ok(())
}
}
}
+1
View File
@@ -1 +1,2 @@
pub mod flix;
pub mod tmdb;
+22 -11
View File
@@ -9,7 +9,7 @@ use flix::tmdb::model::id::{
};
use anyhow::{Context, Result, bail};
use chrono::Utc;
use chrono::{Datelike, Utc};
use sea_orm::ActiveValue::{NotSet, Set};
use sea_orm::{
ActiveModelTrait, DatabaseConnection, DbErr, EntityTrait, TransactionError, TransactionTrait,
@@ -35,6 +35,8 @@ pub async fn add(client: Client, db: &DatabaseConnection, command: Command) -> R
.await
.with_context(|| format!("collections().get_details({})", id.into_raw()))?;
let title = collection.title.clone();
let result: Result<CollectionId, TransactionError<DbErr>> = db
.transaction(|txn| {
Box::pin(async move {
@@ -65,7 +67,7 @@ pub async fn add(client: Client, db: &DatabaseConnection, command: Command) -> R
Err(TransactionError::Connection(err)) => Err(err)?,
Err(TransactionError::Transaction(err)) => Err(err)?,
};
println!("Created Collection: {}", flix_id.into_raw());
println!("Created Collection: {} [{}]", title, flix_id.into_raw());
Ok(())
}
@@ -83,6 +85,9 @@ pub async fn add(client: Client, db: &DatabaseConnection, command: Command) -> R
.await
.with_context(|| format!("movies().get_details({})", id.into_raw()))?;
let title = movie.title.clone();
let year = movie.release_date.year();
let result: Result<MovieId, TransactionError<DbErr>> = db
.transaction(|txn| {
Box::pin(async move {
@@ -116,7 +121,12 @@ pub async fn add(client: Client, db: &DatabaseConnection, command: Command) -> R
Err(TransactionError::Connection(err)) => Err(err)?,
Err(TransactionError::Transaction(err)) => Err(err)?,
};
println!("Created Movie: {}", flix_id.into_raw());
println!(
"Created Movie: {} ({}) [{}]",
title,
year,
flix_id.into_raw(),
);
Ok(())
}
@@ -136,6 +146,9 @@ pub async fn add(client: Client, db: &DatabaseConnection, command: Command) -> R
let mut seasons = Vec::new();
let mut episodes = HashMap::new();
let title = show.title.clone();
let year = show.first_air_date.year();
for season in 1..=show.number_of_seasons {
let season = client
.seasons()
@@ -264,7 +277,12 @@ pub async fn add(client: Client, db: &DatabaseConnection, command: Command) -> R
Err(TransactionError::Connection(err)) => Err(err)?,
Err(TransactionError::Transaction(err)) => Err(err)?,
};
println!("Created Show: {}", flix_id.into_raw());
println!(
"Created Show: {} ({}) [{}]",
title,
year,
flix_id.into_raw()
);
Ok(())
}
@@ -498,10 +516,3 @@ pub async fn update(client: Client, database: &DatabaseConnection, command: Comm
_ = command;
unimplemented!("updates")
}
pub async fn delete(client: Client, database: &DatabaseConnection, command: Command) -> Result<()> {
_ = client;
_ = database;
_ = command;
unimplemented!("deletions")
}