nxtgauge-backend-rust/apps/video_editors/src/admin.rs

80 lines
2.4 KiB
Rust
Raw Normal View History

use contracts::ProfessionState;
use db::models::video_editor::VideoEditorProfile;
use axum::{extract::{Path, State}, http::StatusCode, response::IntoResponse, routing::get, Json, Router};
use serde::Serialize;
use uuid::Uuid;
#[derive(Serialize)]
pub struct AdminVideoEditorList {
pub id: Uuid,
pub user_id: Uuid,
pub display_name: Option<String>,
pub bio: Option<String>,
pub location: Option<String>,
pub status: String,
pub created_at: chrono::DateTime<chrono::Utc>,
pub updated_at: chrono::DateTime<chrono::Utc>,
}
impl From<VideoEditorProfile> for AdminVideoEditorList {
fn from(p: VideoEditorProfile) -> Self {
Self {
id: p.id,
user_id: p.user_id,
display_name: p.display_name,
bio: p.bio,
location: p.location,
status: p.status,
created_at: p.created_at,
updated_at: p.updated_at,
}
}
}
pub fn router() -> Router<ProfessionState> {
Router::new()
.route("/", get(list_video_editors))
.route("/{id}", get(get_video_editor))
}
async fn list_video_editors(
State(state): State<ProfessionState>,
) -> Result<impl IntoResponse, (StatusCode, String)> {
let editors = sqlx::query_as::<_, VideoEditorProfile>(
r#"
SELECT id, user_id, display_name, bio, location, custom_data, status, created_at, updated_at
FROM video_editor_profiles
ORDER BY created_at DESC
LIMIT 100
"#,
)
.fetch_all(&state.pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("DB error: {e}")))?;
let list: Vec<AdminVideoEditorList> = editors.into_iter().map(|p| p.into()).collect();
Ok(Json(list))
}
async fn get_video_editor(
State(state): State<ProfessionState>,
Path(id): Path<Uuid>,
) -> Result<impl IntoResponse, (StatusCode, String)> {
let editor = sqlx::query_as::<_, VideoEditorProfile>(
r#"
SELECT id, user_id, display_name, bio, location, custom_data, status, created_at, updated_at
FROM video_editor_profiles
WHERE id = $1
"#,
)
.bind(id)
.fetch_optional(&state.pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("DB error: {e}")))?;
match editor {
Some(e) => Ok(Json(AdminVideoEditorList::from(e))),
None => Err((StatusCode::NOT_FOUND, "Video Editor not found".to_string())),
}
}