diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2021-12-30 23:40:37 +0100 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2021-12-30 23:40:37 +0100 |
commit | f5e8b4093a1527967423a3af70f2b95d4b05008f (patch) | |
tree | fd000cd7689e54b9efc73f2913ec4d1817c098b0 /planetwars-server/src | |
parent | c6ca7cf2d1238c05f75a53934ea2f6c91efc3646 (diff) | |
download | planetwars.dev-f5e8b4093a1527967423a3af70f2b95d4b05008f.tar.xz planetwars.dev-f5e8b4093a1527967423a3af70f2b95d4b05008f.zip |
prototype bots pages
Diffstat (limited to 'planetwars-server/src')
-rw-r--r-- | planetwars-server/src/db/bots.rs | 6 | ||||
-rw-r--r-- | planetwars-server/src/lib.rs | 1 | ||||
-rw-r--r-- | planetwars-server/src/routes/bots.rs | 17 | ||||
-rw-r--r-- | planetwars-server/src/routes/users.rs | 1 |
4 files changed, 22 insertions, 3 deletions
diff --git a/planetwars-server/src/db/bots.rs b/planetwars-server/src/db/bots.rs index bc9cb11..d21d9dc 100644 --- a/planetwars-server/src/db/bots.rs +++ b/planetwars-server/src/db/bots.rs @@ -28,6 +28,12 @@ pub fn find_bot(id: i32, conn: &PgConnection) -> QueryResult<Bot> { bots::table.find(id).first(conn) } +pub fn find_bots_by_owner(owner_id: i32, conn: &PgConnection) -> QueryResult<Vec<Bot>> { + bots::table + .filter(bots::owner_id.eq(owner_id)) + .get_results(conn) +} + #[derive(Insertable)] #[table_name = "code_bundles"] pub struct NewCodeBundle<'a> { diff --git a/planetwars-server/src/lib.rs b/planetwars-server/src/lib.rs index 260d668..b3aac5f 100644 --- a/planetwars-server/src/lib.rs +++ b/planetwars-server/src/lib.rs @@ -34,6 +34,7 @@ pub async fn api() -> Router { .route("/login", post(routes::users::login)) .route("/users/me", get(routes::users::current_user)) .route("/bots", post(routes::bots::create_bot)) + .route("/bots/my_bots", get(routes::bots::get_my_bots)) .route("/bots/:bot_id", get(routes::bots::get_bot)) .route("/bots/:bot_id/upload", post(routes::bots::upload_bot_code)) .layer(AddExtensionLayer::new(pool)); diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs index da09669..033c683 100644 --- a/planetwars-server/src/routes/bots.rs +++ b/planetwars-server/src/routes/bots.rs @@ -1,5 +1,6 @@ use axum::extract::{Path, RawBody}; use axum::http::StatusCode; +use axum::response::IntoResponse; use axum::Json; use rand::Rng; use serde::{Deserialize, Serialize}; @@ -30,9 +31,19 @@ pub async fn create_bot( } // TODO: handle errors -pub async fn get_bot(conn: DatabaseConnection, Path(bot_id): Path<i32>) -> Json<Bot> { - let bot = bots::find_bot(bot_id, &conn).unwrap(); - Json(bot) +pub async fn get_bot(conn: DatabaseConnection, Path(bot_id): Path<i32>) -> impl IntoResponse { + bots::find_bot(bot_id, &conn) + .map(Json) + .map_err(|_| StatusCode::NOT_FOUND) +} + +pub async fn get_my_bots( + conn: DatabaseConnection, + user: User, +) -> Result<Json<Vec<Bot>>, StatusCode> { + bots::find_bots_by_owner(user.id, &conn) + .map(Json) + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR) } // TODO: proper error handling diff --git a/planetwars-server/src/routes/users.rs b/planetwars-server/src/routes/users.rs index fc77d7b..bc30b28 100644 --- a/planetwars-server/src/routes/users.rs +++ b/planetwars-server/src/routes/users.rs @@ -19,6 +19,7 @@ where async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> { let conn = DatabaseConnection::from_request(req).await?; + let TypedHeader(Authorization(bearer)) = AuthorizationHeader::from_request(req) .await .map_err(|_| (StatusCode::UNAUTHORIZED, "".to_string()))?; |