From 5e998dfe8616c8d7a246c41a97dae7110fda7d55 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Mon, 1 Jan 2024 17:27:17 -0800 Subject: [PATCH] move add watch button to template --- src/watches/handlers.rs | 40 +++++++++++------------- src/watches/templates.rs | 9 ++++++ templates/elements/add_watch_button.html | 7 +++++ 3 files changed, 35 insertions(+), 21 deletions(-) create mode 100644 templates/elements/add_watch_button.html diff --git a/src/watches/handlers.rs b/src/watches/handlers.rs index 3518bfa..80e53e0 100644 --- a/src/watches/handlers.rs +++ b/src/watches/handlers.rs @@ -8,7 +8,7 @@ use julid::Julid; use serde::Deserialize; use sqlx::{query, query_as, query_scalar, SqlitePool}; -use super::templates::{AddNewWatchPage, GetWatchPage, SearchWatchesPage}; +use super::templates::{AddNewWatchPage, AddWatchButton, GetWatchPage, SearchWatchesPage}; use crate::{ util::{empty_string_as_none, year_to_epoch}, AuthSession, MyWatchesPage, ShowKind, Watch, WatchQuest, @@ -19,6 +19,10 @@ use crate::{ //-************************************************************************ const GET_SAVED_WATCHES_QUERY: &str = "select * from watches inner join watch_quests quests on quests.user = $1 and quests.watch = watches.id"; +const GET_QUEST_WITH_PUBLICITY_QUERY: &str = + "select * from watch_quests where user = ? and watch = ? and public = ?"; +const GET_QUEST_WITH_WATCHED_QUERY: &str = + "select * from watch_quests where user = ? and watch = ? and watched = ?"; const GET_WATCH_QUERY: &str = "select * from watches where id = $1"; @@ -290,28 +294,22 @@ pub async fn get_watch_status( if let Some(user) = auth.user { let watch = Julid::from_string(&watch).unwrap(); let public = query.public; - let quest: Option = - query_as("select * from watch_quests where user = ? and watch = ? and public = ?") - .bind(user.id) - .bind(watch) - .bind(public) - .fetch_optional(&pool) - .await - .map_err(|e| { - tracing::error!("Got error from checking watch status: {e:?}"); - })?; + let quest: Option = query_as(GET_QUEST_WITH_PUBLICITY_QUERY) + .bind(user.id) + .bind(watch) + .bind(public) + .fetch_optional(&pool) + .await + .map_err(|e| { + tracing::error!("Got error from checking watch status: {e:?}"); + })?; match quest { Some(_) => Ok("✓".into_response()), - _ => Ok(format!( - "
- - - - -
-" - ) + None => Ok(AddWatchButton { + watch, + public, + watched_already: false, + } .into_response()), } } else { diff --git a/src/watches/templates.rs b/src/watches/templates.rs index 6dbf83f..7e11b17 100644 --- a/src/watches/templates.rs +++ b/src/watches/templates.rs @@ -1,4 +1,5 @@ use askama::Template; +use julid::Julid; use serde::{Deserialize, Serialize}; use crate::{OptionalOptionalUser, User, Watch}; @@ -30,3 +31,11 @@ pub struct GetWatchPage { pub struct AddNewWatchPage { pub user: Option, } + +#[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq)] +#[template(path = "elements/add_watch_button.html")] +pub struct AddWatchButton { + pub watch: Julid, + pub public: bool, + pub watched_already: bool, +} diff --git a/templates/elements/add_watch_button.html b/templates/elements/add_watch_button.html new file mode 100644 index 0000000..abf012b --- /dev/null +++ b/templates/elements/add_watch_button.html @@ -0,0 +1,7 @@ +
+ + + + +