From 90dfc3dec45b3f5d5700f711130d04a5f4d958db Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Sun, 24 Jul 2022 18:05:20 +0200 Subject: create new bot flow --- planetwars-server/src/routes/bots.rs | 12 ++- web/pw-server/src/routes/bots/new.svelte | 98 +++++++++++++++++++++++ web/pw-server/src/routes/matches/new.svelte | 1 - web/pw-server/src/routes/users/[user_name].svelte | 33 +++++++- 4 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 web/pw-server/src/routes/bots/new.svelte diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs index 8de479f..f2bf202 100644 --- a/planetwars-server/src/routes/bots.rs +++ b/planetwars-server/src/routes/bots.rs @@ -134,17 +134,25 @@ pub struct BotParams { name: String, } +// TODO: can we unify this with save_bot? pub async fn create_bot( conn: DatabaseConnection, user: User, params: Json, -) -> (StatusCode, Json) { +) -> Result<(StatusCode, Json), SaveBotError> { + validate_bot_name(¶ms.name)?; + let existing_bot = bots::find_bot_by_name(¶ms.name, &conn) + .optional() + .expect("could not run query"); + if existing_bot.is_some() { + return Err(SaveBotError::BotNameTaken); + } let bot_params = bots::NewBot { owner_id: Some(user.id), name: ¶ms.name, }; let bot = bots::create_bot(&bot_params, &conn).unwrap(); - (StatusCode::CREATED, Json(bot)) + Ok((StatusCode::CREATED, Json(bot))) } // TODO: handle errors diff --git a/web/pw-server/src/routes/bots/new.svelte b/web/pw-server/src/routes/bots/new.svelte new file mode 100644 index 0000000..7cb7229 --- /dev/null +++ b/web/pw-server/src/routes/bots/new.svelte @@ -0,0 +1,98 @@ + + +
+
+

Create new bot

+ + {#if saveErrors.length > 0} +
    + {#each saveErrors as errorText} +
  • {errorText}
  • + {/each} +
+ {/if} + +
+
+ + diff --git a/web/pw-server/src/routes/matches/new.svelte b/web/pw-server/src/routes/matches/new.svelte index ebc0e15..35e792f 100644 --- a/web/pw-server/src/routes/matches/new.svelte +++ b/web/pw-server/src/routes/matches/new.svelte @@ -1,6 +1,5 @@ @@ -25,7 +27,13 @@

{userName}

-

Bots

+ +
+

Bots

+ {#if $currentUser && $currentUser.username == userName} + New bot + {/if} +
    {#each bots as bot}
  • @@ -51,6 +59,29 @@ margin-bottom: 0.5em; } + .bot-list-header { + display: flex; + justify-content: space-between; + align-items: flex-end; + } + + .bot-list-header-title { + margin-bottom: 0px; + } + + .btn-new-bot { + padding: 8px 12px; + border-radius: 4px; + border: 0; + display: block; + color: white; + background-color: rgb(40, 167, 69); + font-weight: 500; + text-decoration: none; + font-size: 11pt; + cursor: pointer; + } + .bot-list { list-style: none; padding: 0; -- cgit v1.2.3