polish error handling for GET /images/by-user
This commit is contained in:
parent
0e4d5210f0
commit
a9cbf77eaa
|
@ -174,7 +174,10 @@ async fn main() {
|
||||||
routes::posts::create,
|
routes::posts::create,
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
.mount("/api", routes![routes::users::api_perms])
|
.mount(
|
||||||
|
"/api",
|
||||||
|
routes![routes::users::api_perms, routes::posts::get_post],
|
||||||
|
)
|
||||||
.mount("/css", FileServer::from("/srv/web/css"))
|
.mount("/css", FileServer::from("/srv/web/css"))
|
||||||
.register("/", catchers![default_catcher])
|
.register("/", catchers![default_catcher])
|
||||||
.launch()
|
.launch()
|
||||||
|
|
|
@ -181,24 +181,35 @@ pub async fn get_images_by_username(
|
||||||
mut db: Connection<Db>,
|
mut db: Connection<Db>,
|
||||||
cookies: &CookieJar<'_>,
|
cookies: &CookieJar<'_>,
|
||||||
username: String,
|
username: String,
|
||||||
) -> Result<Json<Vec<String>>, String> {
|
) -> status::Custom<Result<Json<Vec<String>>, String>> {
|
||||||
let token = cookies.get_private("token");
|
match User::login_status(&mut db, cookies).await {
|
||||||
match token {
|
LoginStatus::LoggedIn(user) => match user.admin || user.username == username {
|
||||||
Some(t) => match User::get_by_token(&mut db, t).await {
|
true => match Image::get_by_username(&mut db, &username).await {
|
||||||
Some(user) => match user.admin || user.username == username {
|
Ok(images) => status::Custom(
|
||||||
true => match Image::get_by_username(&mut db, &username).await {
|
Status::Ok,
|
||||||
Ok(images) => Ok(Json::from(
|
Ok(Json::from(
|
||||||
images.into_iter().map(|i| i.uuid).collect::<Vec<String>>(),
|
images.into_iter().map(|i| i.uuid).collect::<Vec<String>>(),
|
||||||
)),
|
)),
|
||||||
Err(why) => {
|
),
|
||||||
eprintln!("{why:?}");
|
Err(why) => {
|
||||||
Err("Couldn't get that user's images".to_string())
|
eprintln!("{why:?}");
|
||||||
}
|
status::Custom(
|
||||||
},
|
Status::NotFound,
|
||||||
false => Err("You don't have permission to do this".to_string()),
|
Err("Couldn't get that user's images".to_string()),
|
||||||
|
)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
None => Err("Invalid login token".to_string()),
|
false => status::Custom(
|
||||||
|
Status::Unauthorized,
|
||||||
|
Err("You don't have permission to do this".to_string()),
|
||||||
|
),
|
||||||
},
|
},
|
||||||
None => Err("Not logged in".to_string()),
|
LoginStatus::InvalidToken => status::Custom(
|
||||||
|
Status::Unauthorized,
|
||||||
|
Err("Invalid login token.".to_string()),
|
||||||
|
),
|
||||||
|
LoginStatus::NotLoggedIn => {
|
||||||
|
status::Custom(Status::Unauthorized, Err("Not logged in.".to_string()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,12 +12,13 @@ use crate::tables::posts::Post;
|
||||||
use crate::tables::{users::LoginStatus, users::User, Db};
|
use crate::tables::{users::LoginStatus, users::User, Db};
|
||||||
use rocket::http::CookieJar;
|
use rocket::http::CookieJar;
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
use rocket::post;
|
|
||||||
use rocket::response::status;
|
use rocket::response::status;
|
||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
use rocket::serde::Deserialize;
|
use rocket::serde::Deserialize;
|
||||||
|
use rocket::{get, post};
|
||||||
use rocket_db_pools::Connection;
|
use rocket_db_pools::Connection;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
use rocket::serde::Serialize;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
#[serde(crate = "rocket::serde")]
|
#[serde(crate = "rocket::serde")]
|
||||||
|
@ -80,3 +81,22 @@ pub async fn create(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#[derive(Serialize)]
|
||||||
|
#[serde(crate = "rocket::serde")]
|
||||||
|
pub struct ApiPost {
|
||||||
|
title: String,
|
||||||
|
author: String,
|
||||||
|
text_id: String,
|
||||||
|
body: String,
|
||||||
|
timestamp: String,
|
||||||
|
uuid: String,
|
||||||
|
published: bool
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/posts/<id>")]
|
||||||
|
pub async fn get_post(mut db: Connection<Db>, id: String) -> status::Custom<Result<Json<ApiPost>, String>> {
|
||||||
|
// get post by uuid
|
||||||
|
// if none, get post by text id
|
||||||
|
// if none, return none
|
||||||
|
status::Custom(Status::NotImplemented, Err("Not implemented yet.".to_string()))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue