add working endpoints for getting your watches

This commit is contained in:
Joe Ardent 2023-06-08 15:45:34 -07:00
parent 1e9d97bd19
commit c44c89005c
8 changed files with 97 additions and 5 deletions

View file

@ -20,7 +20,10 @@ pub(crate) mod util;
pub mod watches; pub mod watches;
pub use templates::*; pub use templates::*;
pub use watches::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)] #[cfg(test)]
pub mod test_utils; 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("/signup_success/:id", get(handle_signup_success))
.route("/login", get(get_login).post(post_login)) .route("/login", get(get_login).post(post_login))
.route("/logout", get(get_logout).post(post_logout)) .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) .fallback(handle_slash_redir)
.layer(middleware::from_fn_with_state( .layer(middleware::from_fn_with_state(
db_pool.clone(), db_pool.clone(),

View file

@ -6,7 +6,14 @@ use axum::{
use sqlx::{query_as, SqlitePool}; use sqlx::{query_as, SqlitePool};
use uuid::Uuid; 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 // Error types for Watch creation
@ -32,13 +39,29 @@ impl IntoResponse for WatchAddError {
} }
/// Add a Watch to the whole system /// Add a Watch to the whole system
pub async fn post_add_watch() {} pub async fn put_add_watch() {}
/// A single Watch /// A single Watch
pub async fn get_watch() {} pub async fn get_watch() {}
/// everything the user has saved /// everything the user has saved
pub async fn get_watches() {} pub async fn get_watches(auth: AuthContext, State(pool): State<SqlitePool>) -> impl IntoResponse {
let user = &auth.current_user;
let watches: Vec<Watch> = 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() {} pub async fn get_search_watch() {}

View file

@ -16,6 +16,12 @@ pub enum ShowKind {
Unknown = 4, Unknown = 4,
} }
impl std::fmt::Display for ShowKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!()
}
}
impl Default for ShowKind { impl Default for ShowKind {
fn default() -> Self { fn default() -> Self {
Self::Unknown Self::Unknown

View file

@ -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<Watch>,
pub user: Option<User>,
}

View file

@ -0,0 +1,37 @@
{% extends "base.html" %}
{% import "macros.html" as m %}
{% block title %}Welcome to Witch Watch, Bish{% endblock %}
{% block content %}
<h1>Whatcha Watchin?</h1>
{% match user %}
{% when Some with (usr) %}
<p>
Hello, {{ usr.username }}! It's nice to see you.
</p>
</br>
<p>Here are your things to watch:</p>
<div class="watchlist">
<ul>
{% for watch in watches %}
<li><span class="watchtitle">{{watch.title}}</span> -- {% call m::get_or_default(watch.release_date, "when??") %}: </li>
{% endfor %}
</ul>
</div>
<p>
<form action="/search" enctype="application/x-www-form-urlencoded" method="post">
<label for="search">Looking for something else to watch?</label>
<input type="text" name="search" id="search"></br>
<input type="submit" value="Let's go!">
</form>
</p>
{% else %}
<p>
Heya, why don't you <a href="/login">log in</a> or <a href="/signup">sign up</a>?
</p>
{% endmatch %}
{% endblock %}

View file

@ -9,7 +9,7 @@
{% match user %} {% match user %}
{% when Some with (usr) %} {% when Some with (usr) %}
<p> <p>
Hello, {{ usr.username }}! It's nice to see you. Hello, {{ usr.username }}! It's nice to see you. <a href="watches">Let's get watchin'!</a>
</p> </p>
</br> </br>
<p> <p>

10
templates/macros.html Normal file
View file

@ -0,0 +1,10 @@
{% macro get_or_default(val, def) %}
{% match val %}
{% when Some with (v) %}
{{v}}
{% else %}
{{def}}
{% endmatch %}
{% endmacro %}