what2watch/src/watches/handlers.rs

94 lines
2.4 KiB
Rust
Raw Normal View History

2023-06-10 18:32:40 +00:00
use std::path::Path;
2023-06-08 16:17:11 +00:00
use axum::{
2023-06-10 18:32:40 +00:00
extract::{Form, Query, State},
2023-06-08 16:17:11 +00:00
http::StatusCode,
response::{IntoResponse, Response},
};
use serde::Deserialize;
2023-06-08 16:17:11 +00:00
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";
2023-06-08 16:17:11 +00:00
//-************************************************************************
// 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,
}
2023-06-10 18:32:40 +00:00
//-************************************************************************
// handlers
//-************************************************************************
/// Add a new Watch to the whole system (also adds to your watchlist)
pub async fn put_add_watch() {}
2023-06-05 23:32:42 +00:00
2023-06-10 18:32:40 +00:00
/// 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();
}
2023-06-08 16:17:11 +00:00
pub async fn post_search_watch() {}