diff --git a/assets/ww.css b/assets/ww.css index 1941e67..0c4dad8 100644 --- a/assets/ww.css +++ b/assets/ww.css @@ -8,7 +8,7 @@ table { } th { - background-color: darkgray; + background-color: ghostwhite; } th, td { @@ -16,7 +16,7 @@ th, td { padding: 8px; } -tr:nth-child(odd) {background-color: ghostwhite;} +tr:nth-child(even) {background-color: ghostwhite;} #header { text-align: end; diff --git a/src/watches/handlers.rs b/src/watches/handlers.rs index 85c0948..d0a046f 100644 --- a/src/watches/handlers.rs +++ b/src/watches/handlers.rs @@ -3,6 +3,7 @@ use axum::{ http::StatusCode, response::{IntoResponse, Redirect, Response}, }; +use http::HeaderValue; use julid::Julid; use serde::Deserialize; use sqlx::{query, query_as, query_scalar, SqlitePool}; @@ -37,22 +38,22 @@ const EMPTY_SEARCH_QUERY_STRUCT: SearchQuery = SearchQuery { //-************************************************************************ #[Error] -pub struct WatchAddError(#[from] WatchAddErrorKind); +pub struct AddError(#[from] AddErrorKind); #[Error] #[non_exhaustive] -pub enum WatchAddErrorKind { +pub enum AddErrorKind { UnknownDBError, NotSignedIn, } -impl IntoResponse for WatchAddError { +impl IntoResponse for AddError { fn into_response(self) -> Response { match &self.0 { - WatchAddErrorKind::UnknownDBError => { + AddErrorKind::UnknownDBError => { (StatusCode::INTERNAL_SERVER_ERROR, format!("{self}")).into_response() } - WatchAddErrorKind::NotSignedIn => ( + AddErrorKind::NotSignedIn => ( StatusCode::OK, "Ope, you need to sign in first!".to_string(), ) @@ -94,7 +95,7 @@ pub struct PostAddNewWatch { #[derive(Debug, Default, Deserialize, PartialEq, Eq)] pub struct PostAddExistingWatch { - pub id: String, + pub watch: String, pub public: bool, pub watched_already: bool, } @@ -107,18 +108,12 @@ pub async fn get_add_new_watch(auth: AuthSession) -> impl IntoResponse { AddNewWatchPage { user: auth.user } } -struct QuestQuest { - pub user: Julid, - pub is_public: bool, - pub already_watched: bool, -} - /// Add a Watch to your watchlist (side effects system-add) pub async fn post_add_new_watch( auth: AuthSession, State(pool): State, Form(form): Form, -) -> Result { +) -> Result { if let Some(user) = auth.user { { let release_date = year_to_epoch(form.year.as_deref()); @@ -130,31 +125,27 @@ pub async fn post_add_new_watch( added_by: user.id, ..Default::default() }; - let quest = QuestQuest { + + let watch_id = add_new_watch_impl(&pool, &watch).await?; + let quest = WatchQuest { user: user.id, is_public: !form.private, already_watched: form.watched_already, + watch: watch_id, }; - - let watch_id = add_new_watch_impl(&pool, &watch, Some(quest)).await?; + add_watch_quest_impl(&pool, &quest) + .await + .map_err(|_| AddErrorKind::UnknownDBError)?; let location = format!("/watch/{watch_id}"); Ok(Redirect::to(&location)) } } else { - Err(WatchAddErrorKind::NotSignedIn.into()) + Err(AddErrorKind::NotSignedIn.into()) } } -async fn add_new_watch_impl( - db_pool: &SqlitePool, - watch: &Watch, - quest: Option, -) -> Result { - let mut tx = db_pool - .begin() - .await - .map_err(|_| WatchAddErrorKind::UnknownDBError)?; +async fn add_new_watch_impl(db_pool: &SqlitePool, watch: &Watch) -> Result { let watch_id: Julid = query_scalar(ADD_WATCH_QUERY) .bind(&watch.title) .bind(watch.kind) @@ -162,44 +153,43 @@ async fn add_new_watch_impl( .bind(&watch.metadata_url) .bind(watch.added_by) .bind(watch.length) - .fetch_one(&mut *tx) + .fetch_one(db_pool) .await .map_err(|err| { tracing::error!("Got error: {err}"); - WatchAddErrorKind::UnknownDBError + AddErrorKind::UnknownDBError })?; - - if let Some(quest) = quest { - query(ADD_WATCH_QUEST_QUERY) - .bind(quest.user) - .bind(watch_id) - .bind(quest.is_public) - .bind(quest.already_watched) - .execute(&mut *tx) - .await - .map_err(|err| { - tracing::error!("Got error: {err}"); - WatchAddErrorKind::UnknownDBError - })?; - } - tx.commit().await.map_err(|err| { - tracing::error!("Got error: {err}"); - WatchAddErrorKind::UnknownDBError - })?; - Ok(watch_id) } /// Add a Watch to your watchlist by selecting it with a checkbox pub async fn post_add_watch_quest( - _auth: AuthSession, - State(_pool): State, - Form(_form): Form, -) -> impl IntoResponse { - todo!() + auth: AuthSession, + State(pool): State, + Form(form): Form, +) -> Result { + if let Some(user) = auth.user { + let quest = WatchQuest { + user: user.id, + watch: Julid::from_string(&form.watch).unwrap(), + is_public: form.public, + already_watched: form.watched_already, + }; + add_watch_quest_impl(&pool, &quest) + .await + .map_err(|_| AddErrorKind::UnknownDBError)?; + let resp = "✓"; + Ok(resp.into_response()) + } else { + let resp = Redirect::to("/login"); + let mut resp = resp.into_response(); + resp.headers_mut() + .insert("HX-Redirect", HeaderValue::from_str("/login").unwrap()); + Ok(resp) + } } -pub async fn _add_watch_quest_impl(pool: &SqlitePool, quest: &WatchQuest) -> Result<(), ()> { +pub async fn add_watch_quest_impl(pool: &SqlitePool, quest: &WatchQuest) -> Result<(), ()> { query(ADD_WATCH_QUEST_QUERY) .bind(quest.user) .bind(quest.watch) diff --git a/templates/header_with_user.html b/templates/header_with_user.html index 85a2f7d..c093547 100644 --- a/templates/header_with_user.html +++ b/templates/header_with_user.html @@ -13,7 +13,7 @@ -{% when None %} +{% when None %} diff --git a/templates/watch-search-result.html b/templates/watch-search-result.html index 4b10842..4af5e11 100644 --- a/templates/watch-search-result.html +++ b/templates/watch-search-result.html @@ -2,4 +2,13 @@ {{watch.title}} {{watch.kind}} {% call m::get_or_default(watch.year(), "when??") -%} + +
+ + + + +
+