Get single video route

This commit is contained in:
roaming97 2025-04-14 00:35:24 -06:00
parent bdee736864
commit 628f179e09
2 changed files with 16 additions and 2 deletions

View File

@ -4,7 +4,7 @@ use axum::{
}; };
use instance::Instance; use instance::Instance;
use middleware::auth; use middleware::auth;
use routes::{list_videos, upload_video}; use routes::{get_video, list_videos, upload_video};
use tokio::signal; use tokio::signal;
use tracing::info; use tracing::info;
@ -31,6 +31,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.route("/upload", post(upload_video)) .route("/upload", post(upload_video))
.route_layer(axum::middleware::from_fn_with_state(instance.clone(), auth)) .route_layer(axum::middleware::from_fn_with_state(instance.clone(), auth))
.route("/", get(list_videos)) .route("/", get(list_videos))
.route("/{id}", get(get_video))
.with_state(instance); .with_state(instance);
let listener = tokio::net::TcpListener::bind(address).await?; let listener = tokio::net::TcpListener::bind(address).await?;

View File

@ -1,6 +1,6 @@
use axum::{ use axum::{
Json, Json,
extract::{Query, State}, extract::{Path, Query, State},
http::StatusCode, http::StatusCode,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -61,6 +61,19 @@ pub async fn list_videos(
})) }))
} }
/// Get a single video from the database by its ID
pub async fn get_video(
State(state): State<Instance>,
Path(id): Path<i64>,
) -> Result<Json<Video>, StatusCode> {
sqlx::query_as!(Video, "SELECT * FROM video WHERE id = ?", id)
.fetch_optional(&state.pool)
.await
.map_or(Err(StatusCode::INTERNAL_SERVER_ERROR), |video| {
video.map_or(Err(StatusCode::NOT_FOUND), |v| Ok(Json(v)))
})
}
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct UploadVideoQuery { pub struct UploadVideoQuery {
url: String, url: String,