From 3e902bb56e9626c8296e9836aa9ae19d82d48394 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Fri, 31 Dec 2021 11:24:25 +0100 Subject: [PATCH] list uploaded code bundles on bot page --- planetwars-server/src/db/bots.rs | 6 +++++ planetwars-server/src/routes/bots.rs | 16 +++++++++--- web/pw-server/package.json | 11 ++++---- web/pw-server/src/routes/bots/[bot_id].svelte | 25 ++++++++++++++----- 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/planetwars-server/src/db/bots.rs b/planetwars-server/src/db/bots.rs index d21d9dc..eb66c05 100644 --- a/planetwars-server/src/db/bots.rs +++ b/planetwars-server/src/db/bots.rs @@ -57,3 +57,9 @@ pub fn create_code_bundle( .values(new_code_bundle) .get_result(conn) } + +pub fn find_bot_code_bundles(bot_id: i32, conn: &PgConnection) -> QueryResult> { + code_bundles::table + .filter(code_bundles::bot_id.eq(bot_id)) + .get_results(conn) +} diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs index 83127cd..f722e52 100644 --- a/planetwars-server/src/routes/bots.rs +++ b/planetwars-server/src/routes/bots.rs @@ -5,6 +5,7 @@ use axum::Json; use rand::distributions::Alphanumeric; use rand::Rng; use serde::{Deserialize, Serialize}; +use serde_json::{json, value::Value as JsonValue}; use std::io::Cursor; use std::path::{self, PathBuf}; @@ -35,10 +36,17 @@ pub async fn create_bot( } // TODO: handle errors -pub async fn get_bot(conn: DatabaseConnection, Path(bot_id): Path) -> impl IntoResponse { - bots::find_bot(bot_id, &conn) - .map(Json) - .map_err(|_| StatusCode::NOT_FOUND) +pub async fn get_bot( + conn: DatabaseConnection, + Path(bot_id): Path, +) -> Result, StatusCode> { + let bot = bots::find_bot(bot_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?; + let bundles = bots::find_bot_code_bundles(bot.id, &conn) + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + Ok(Json(json!({ + "bot": bot, + "bundles": bundles, + }))) } pub async fn get_my_bots( diff --git a/web/pw-server/package.json b/web/pw-server/package.json index 29d59f6..b8a9216 100644 --- a/web/pw-server/package.json +++ b/web/pw-server/package.json @@ -12,13 +12,12 @@ "format": "prettier --ignore-path .gitignore --write --plugin-search-dir=. ." }, "devDependencies": { + "@originjs/vite-plugin-commonjs": "^1.0.1", "@sveltejs/adapter-auto": "next", "@sveltejs/kit": "next", + "@sveltejs/vite-plugin-svelte": "^1.0.0-next.30", "@typescript-eslint/eslint-plugin": "^4.31.1", "@typescript-eslint/parser": "^4.31.1", - "@originjs/vite-plugin-commonjs": "^1.0.1", - "@sveltejs/vite-plugin-svelte": "^1.0.0-next.30", - "vite-plugin-wasm-pack": "^0.1.9", "eslint": "^7.32.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-svelte3": "^3.2.1", @@ -28,10 +27,12 @@ "svelte-check": "^2.2.6", "svelte-preprocess": "^4.9.4", "tslib": "^2.3.1", - "typescript": "^4.4.3" + "typescript": "^4.4.3", + "vite-plugin-wasm-pack": "^0.1.9" }, "dependencies": { + "dayjs": "^1.10.7", "moment": "^2.29.1" }, "type": "module" -} \ No newline at end of file +} diff --git a/web/pw-server/src/routes/bots/[bot_id].svelte b/web/pw-server/src/routes/bots/[bot_id].svelte index 1d7994d..2556d73 100644 --- a/web/pw-server/src/routes/bots/[bot_id].svelte +++ b/web/pw-server/src/routes/bots/[bot_id].svelte @@ -1,19 +1,22 @@