diff --git a/src/views/link.rs b/src/views/link.rs index 0954283..432b0a2 100644 --- a/src/views/link.rs +++ b/src/views/link.rs @@ -8,7 +8,7 @@ use axum::{ use entity::link; use sea_orm::{entity::prelude::*, ActiveValue, Set}; use tera::Context; -use tracing::error; // use sea_orm::FuckYouForNotKnowingAboutThisTrait; +use tracing::error; use crate::{ data::{AppState, CurrentUser, LinkForm}, @@ -19,14 +19,39 @@ use std::{collections::HashMap, sync::Arc}; /// crud routes for pub fn get_routes() -> Router> { Router::new() - .route("/", get(list).post(|| async { "make a new link" })) + .route("/", get(list).post(create)) .route( "/:name", - patch(|| async { "change a link" }).delete(|| async { "yeet a link" }), + get(|| async { "filled out form with link data" }).patch(update).delete(delete), ) // admins can change/delete all, users their own } +pub async fn detail( + State(state): State>, + Extension(user): Extension, + Path(source): Path, + ) -> String { + let link = match link::Entity::find().filter(link::Column::Source.eq(source)).one(&state.db).await { + Err(_) => return utils::render_status_code(StatusCode::INTERNAL_SERVER_ERROR).await, + Ok(None) => return utils::render_status_code(StatusCode::NOT_FOUND).await, + Ok(Some(l)) => l, + }; + + if (!user.is_admin) && link.user_id != user.id { + return utils::render_status_code(StatusCode::UNAUTHORIZED).await + } + + let mut context = Context::new(); + context.insert("user", &user); + context.insert("link", &link); + + match utils::render_template("link-detail.html", Some(context)).await { + Ok(page) => page, + Err(_) => utils::render_status_code(StatusCode::INTERNAL_SERVER_ERROR).await, + } +} + pub async fn create( State(state): State>, Extension(user): Extension, @@ -194,7 +219,7 @@ pub async fn list( context.insert("user", &user); context.insert("links", &links); - Ok(utils::render_template("", Some(context)) + Ok(utils::render_template("link-list.html", Some(context)) .await .map_err(|e| { error!("oopsie woopsie, we had a little rendering fucky wucky! {e}");