what2watch/src/watches/handlers.rs

93 lines
2.4 KiB
Rust

use std::path::Path;
use axum::{
extract::{Form, Query, State},
http::StatusCode,
response::{IntoResponse, Response},
};
use serde::Deserialize;
use sqlx::{query_as, SqlitePool};
use uuid::Uuid;
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]
pub struct WatchAddError(#[from] WatchAddErrorKind);
#[Error]
#[non_exhaustive]
pub enum WatchAddErrorKind {
UnknownDBError,
}
impl IntoResponse for WatchAddError {
fn into_response(self) -> Response {
match self.0 {
WatchAddErrorKind::UnknownDBError => {
(StatusCode::INTERNAL_SERVER_ERROR, format!("{self}")).into_response()
}
}
}
}
#[derive(Debug, Default, Clone, Deserialize)]
pub struct SimpleSearchQuery {
search: String,
}
//-************************************************************************
// handlers
//-************************************************************************
/// Add a new Watch to the whole system (also adds to your watchlist)
pub async fn put_add_watch() {}
/// Add a Watch to your watchlist
pub async fn post_add_watch() {}
/// A single Watch
pub async fn get_watch() {}
/// everything the user has saved
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(
_auth: AuthContext,
State(pool): State<SqlitePool>,
search: Option<Query<SimpleSearchQuery>>,
) {
let search = match search {
Some(Query(SimpleSearchQuery { search })) => search,
None => "".to_owned(),
};
let search = search.trim();
}
pub async fn post_search_watch() {}