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},
|
|
|
|
};
|
2023-06-10 22:32:50 +00:00
|
|
|
use serde::Deserialize;
|
2023-06-08 16:17:11 +00:00
|
|
|
use sqlx::{query_as, SqlitePool};
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2023-06-08 22:45:34 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-10 22:32:50 +00:00
|
|
|
#[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)
|
2023-06-08 22:45:34 +00:00
|
|
|
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() {}
|
|
|
|
|
2023-06-06 21:08:49 +00:00
|
|
|
/// A single Watch
|
|
|
|
pub async fn get_watch() {}
|
|
|
|
|
|
|
|
/// everything the user has saved
|
2023-06-08 22:45:34 +00:00
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|
2023-06-06 21:08:49 +00:00
|
|
|
|
2023-06-10 22:32:50 +00:00
|
|
|
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-06 21:08:49 +00:00
|
|
|
|
2023-06-08 16:17:11 +00:00
|
|
|
pub async fn post_search_watch() {}
|