diff --git a/src/lib.rs b/src/lib.rs index 87f46a3..ccba9a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,7 +20,10 @@ pub(crate) mod util; pub mod watches; pub use templates::*; pub use watches::templates::*; -use watches::{ShowKind, Watch}; +use watches::{ + handlers::{get_search_watch, get_watches, post_search_watch, put_add_watch}, + ShowKind, Watch, +}; #[cfg(test)] pub mod test_utils; @@ -37,6 +40,9 @@ pub async fn app(db_pool: SqlitePool, secret: &[u8]) -> Router { .route("/signup_success/:id", get(handle_signup_success)) .route("/login", get(get_login).post(post_login)) .route("/logout", get(get_logout).post(post_logout)) + .route("/watches", get(get_watches)) + .route("/search", get(get_search_watch).post(post_search_watch)) + .route("/add", get(get_search_watch).put(put_add_watch)) .fallback(handle_slash_redir) .layer(middleware::from_fn_with_state( db_pool.clone(), diff --git a/src/watches/handlers.rs b/src/watches/handlers.rs index 2676320..bc103ad 100644 --- a/src/watches/handlers.rs +++ b/src/watches/handlers.rs @@ -6,7 +6,14 @@ use axum::{ use sqlx::{query_as, SqlitePool}; use uuid::Uuid; -use crate::{ShowKind, User, Watch}; +use crate::{AuthContext, GetWatches, ShowKind, User, Watch}; + +//-************************************************************************ +// Constants +//-************************************************************************ + +const GET_WATCHES_QUERY: &str = + "select * from watches left join witch_watch on $1 = witch_watch.witch and watches.id = witch_watch.watch"; //-************************************************************************ // Error types for Watch creation @@ -32,13 +39,29 @@ impl IntoResponse for WatchAddError { } /// Add a Watch to the whole system -pub async fn post_add_watch() {} +pub async fn put_add_watch() {} /// A single Watch pub async fn get_watch() {} /// everything the user has saved -pub async fn get_watches() {} +pub async fn get_watches(auth: AuthContext, State(pool): State) -> impl IntoResponse { + let user = &auth.current_user; + let watches: Vec = if user.is_some() { + query_as(GET_WATCHES_QUERY) + .bind(user.as_ref().unwrap().id) + .fetch_all(&pool) + .await + .unwrap_or_default() + } else { + vec![] + }; + + GetWatches { + watches, + user: user.clone(), + } +} pub async fn get_search_watch() {} diff --git a/src/watches/mod.rs b/src/watches/mod.rs index f92df12..2dd680b 100644 --- a/src/watches/mod.rs +++ b/src/watches/mod.rs @@ -16,6 +16,12 @@ pub enum ShowKind { Unknown = 4, } +impl std::fmt::Display for ShowKind { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + todo!() + } +} + impl Default for ShowKind { fn default() -> Self { Self::Unknown diff --git a/src/watches/templates.rs b/src/watches/templates.rs index 8b13789..1ab4075 100644 --- a/src/watches/templates.rs +++ b/src/watches/templates.rs @@ -1 +1,11 @@ +use askama::Template; +use serde::{Deserialize, Serialize}; +use crate::{User, Watch}; + +#[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq)] +#[template(path = "get_watches.html")] +pub struct GetWatches { + pub watches: Vec, + pub user: Option, +} diff --git a/templates/get_watches.rs b/templates/get_search_watches.html similarity index 100% rename from templates/get_watches.rs rename to templates/get_search_watches.html diff --git a/templates/get_watches.html b/templates/get_watches.html new file mode 100644 index 0000000..408382c --- /dev/null +++ b/templates/get_watches.html @@ -0,0 +1,37 @@ +{% extends "base.html" %} +{% import "macros.html" as m %} + +{% block title %}Welcome to Witch Watch, Bish{% endblock %} + +{% block content %} + +

Whatcha Watchin?

+ +{% match user %} + {% when Some with (usr) %} +

+Hello, {{ usr.username }}! It's nice to see you. +

+
+

Here are your things to watch:

+
+
    + {% for watch in watches %} +
  • {{watch.title}} -- {% call m::get_or_default(watch.release_date, "when??") %}:
  • + {% endfor %} +
+
+

+

+ +
+ +
+

+{% else %} +

+ Heya, why don't you log in or sign up? +

+{% endmatch %} + +{% endblock %} diff --git a/templates/index.html b/templates/index.html index e7c47f2..92c44f3 100644 --- a/templates/index.html +++ b/templates/index.html @@ -9,7 +9,7 @@ {% match user %} {% when Some with (usr) %}

-Hello, {{ usr.username }}! It's nice to see you. + Hello, {{ usr.username }}! It's nice to see you. Let's get watchin'!


diff --git a/templates/macros.html b/templates/macros.html new file mode 100644 index 0000000..78ce461 --- /dev/null +++ b/templates/macros.html @@ -0,0 +1,10 @@ +{% macro get_or_default(val, def) %} + +{% match val %} +{% when Some with (v) %} +{{v}} +{% else %} +{{def}} +{% endmatch %} + +{% endmacro %}