list uploaded code bundles on bot page

This commit is contained in:
Ilion Beyst 2021-12-31 11:24:25 +01:00
parent 0f27ca80fb
commit 3e902bb56e
4 changed files with 43 additions and 15 deletions

View file

@ -57,3 +57,9 @@ pub fn create_code_bundle(
.values(new_code_bundle) .values(new_code_bundle)
.get_result(conn) .get_result(conn)
} }
pub fn find_bot_code_bundles(bot_id: i32, conn: &PgConnection) -> QueryResult<Vec<CodeBundle>> {
code_bundles::table
.filter(code_bundles::bot_id.eq(bot_id))
.get_results(conn)
}

View file

@ -5,6 +5,7 @@ use axum::Json;
use rand::distributions::Alphanumeric; use rand::distributions::Alphanumeric;
use rand::Rng; use rand::Rng;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::{json, value::Value as JsonValue};
use std::io::Cursor; use std::io::Cursor;
use std::path::{self, PathBuf}; use std::path::{self, PathBuf};
@ -35,10 +36,17 @@ pub async fn create_bot(
} }
// TODO: handle errors // TODO: handle errors
pub async fn get_bot(conn: DatabaseConnection, Path(bot_id): Path<i32>) -> impl IntoResponse { pub async fn get_bot(
bots::find_bot(bot_id, &conn) conn: DatabaseConnection,
.map(Json) Path(bot_id): Path<i32>,
.map_err(|_| StatusCode::NOT_FOUND) ) -> Result<Json<JsonValue>, 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( pub async fn get_my_bots(

View file

@ -12,13 +12,12 @@
"format": "prettier --ignore-path .gitignore --write --plugin-search-dir=. ." "format": "prettier --ignore-path .gitignore --write --plugin-search-dir=. ."
}, },
"devDependencies": { "devDependencies": {
"@originjs/vite-plugin-commonjs": "^1.0.1",
"@sveltejs/adapter-auto": "next", "@sveltejs/adapter-auto": "next",
"@sveltejs/kit": "next", "@sveltejs/kit": "next",
"@sveltejs/vite-plugin-svelte": "^1.0.0-next.30",
"@typescript-eslint/eslint-plugin": "^4.31.1", "@typescript-eslint/eslint-plugin": "^4.31.1",
"@typescript-eslint/parser": "^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": "^7.32.0",
"eslint-config-prettier": "^8.3.0", "eslint-config-prettier": "^8.3.0",
"eslint-plugin-svelte3": "^3.2.1", "eslint-plugin-svelte3": "^3.2.1",
@ -28,9 +27,11 @@
"svelte-check": "^2.2.6", "svelte-check": "^2.2.6",
"svelte-preprocess": "^4.9.4", "svelte-preprocess": "^4.9.4",
"tslib": "^2.3.1", "tslib": "^2.3.1",
"typescript": "^4.4.3" "typescript": "^4.4.3",
"vite-plugin-wasm-pack": "^0.1.9"
}, },
"dependencies": { "dependencies": {
"dayjs": "^1.10.7",
"moment": "^2.29.1" "moment": "^2.29.1"
}, },
"type": "module" "type": "module"

View file

@ -1,19 +1,22 @@
<script lang="ts" context="module"> <script lang="ts" context="module">
import { get_session_token } from "$lib/auth"; import { get_session_token } from "$lib/auth";
import { mount_component } from "svelte/internal";
export async function load({ page }) { export async function load({ page }) {
const token = get_session_token(); const token = get_session_token();
const res = await fetch(`/api/bots/${page.params["bot_id"]}`, { const res = await fetch(`/api/bots/${page.params["bot_id"]}`, {
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
"Authorization": `Bearer ${token}`, Authorization: `Bearer ${token}`,
}, },
}); });
if (res.ok) { if (res.ok) {
const data = await res.json();
return { return {
props: { props: {
bot: await res.json(), bot: data["bot"],
bundles: data["bundles"],
}, },
}; };
} }
@ -26,7 +29,10 @@
</script> </script>
<script lang="ts"> <script lang="ts">
import dayjs from "dayjs";
export let bot: object; export let bot: object;
export let bundles: object[];
let files; let files;
@ -41,7 +47,7 @@
method: "POST", method: "POST",
headers: { headers: {
// the content type header will be set by the browser // the content type header will be set by the browser
"Authorization": `Bearer ${token}`, Authorization: `Bearer ${token}`,
}, },
body: formData, body: formData,
}); });
@ -54,9 +60,16 @@
{bot["name"]} {bot["name"]}
</div> </div>
<div>Upload code</div> <div>Upload code</div>
<form on:submit|preventDefault={submitCode}> <form on:submit|preventDefault={submitCode}>
<input type="file" bind:files/> <input type="file" bind:files />
<button type="submit">Submit</button> <button type="submit">Submit</button>
</form> </form>
<ul>
{#each bundles as bundle}
<li>
bundle created at {dayjs(bundle["created_at"]).format("YYYY-MM-DD HH:mm")}
</li>
{/each}
</ul>