From 628f179e09562109e1fd3e0515cc30a5e67129d3 Mon Sep 17 00:00:00 2001 From: roaming97 Date: Mon, 14 Apr 2025 00:35:24 -0600 Subject: [PATCH] Get single video route --- src/main.rs | 3 ++- src/routes.rs | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 19bba3d..08e0dea 100755 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use axum::{ }; use instance::Instance; use middleware::auth; -use routes::{list_videos, upload_video}; +use routes::{get_video, list_videos, upload_video}; use tokio::signal; use tracing::info; @@ -31,6 +31,7 @@ async fn main() -> Result<(), Box> { .route("/upload", post(upload_video)) .route_layer(axum::middleware::from_fn_with_state(instance.clone(), auth)) .route("/", get(list_videos)) + .route("/{id}", get(get_video)) .with_state(instance); let listener = tokio::net::TcpListener::bind(address).await?; diff --git a/src/routes.rs b/src/routes.rs index 5865f40..8fbbd6a 100755 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,6 +1,6 @@ use axum::{ Json, - extract::{Query, State}, + extract::{Path, Query, State}, http::StatusCode, }; 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, + Path(id): Path, +) -> Result, 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)] pub struct UploadVideoQuery { url: String,