Browse Source

add link detail view

main
xenua 2 years ago
parent
commit
72a55912a7
Signed by: xenua
GPG Key ID: 8F93B68BD37255B8
  1. 33
      src/views/link.rs

33
src/views/link.rs

@ -8,7 +8,7 @@ use axum::{
use entity::link; use entity::link;
use sea_orm::{entity::prelude::*, ActiveValue, Set}; use sea_orm::{entity::prelude::*, ActiveValue, Set};
use tera::Context; use tera::Context;
use tracing::error; // use sea_orm::FuckYouForNotKnowingAboutThisTrait; use tracing::error;
use crate::{ use crate::{
data::{AppState, CurrentUser, LinkForm}, data::{AppState, CurrentUser, LinkForm},
@ -19,14 +19,39 @@ use std::{collections::HashMap, sync::Arc};
/// crud routes for /// crud routes for
pub fn get_routes() -> Router<Arc<AppState>> { pub fn get_routes() -> Router<Arc<AppState>> {
Router::new() Router::new()
.route("/", get(list).post(|| async { "make a new link" })) .route("/", get(list).post(create))
.route( .route(
"/:name", "/: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 // admins can change/delete all, users their own
} }
pub async fn detail(
State(state): State<Arc<AppState>>,
Extension(user): Extension<CurrentUser>,
Path(source): Path<String>,
) -> 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( pub async fn create(
State(state): State<Arc<AppState>>, State(state): State<Arc<AppState>>,
Extension(user): Extension<CurrentUser>, Extension(user): Extension<CurrentUser>,
@ -194,7 +219,7 @@ pub async fn list(
context.insert("user", &user); context.insert("user", &user);
context.insert("links", &links); context.insert("links", &links);
Ok(utils::render_template("", Some(context)) Ok(utils::render_template("link-list.html", Some(context))
.await .await
.map_err(|e| { .map_err(|e| {
error!("oopsie woopsie, we had a little rendering fucky wucky! {e}"); error!("oopsie woopsie, we had a little rendering fucky wucky! {e}");

Loading…
Cancel
Save