aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-07-24 18:05:20 +0200
committerIlion Beyst <ilion.beyst@gmail.com>2022-07-24 18:05:20 +0200
commit90dfc3dec45b3f5d5700f711130d04a5f4d958db (patch)
tree74acb864dc364c363835d4a5a66b0e2f7fabd6df /planetwars-server
parent99987f84446ce4fb9457696de070434706ae1a7b (diff)
downloadplanetwars.dev-90dfc3dec45b3f5d5700f711130d04a5f4d958db.tar.xz
planetwars.dev-90dfc3dec45b3f5d5700f711130d04a5f4d958db.zip
create new bot flow
Diffstat (limited to 'planetwars-server')
-rw-r--r--planetwars-server/src/routes/bots.rs12
1 files changed, 10 insertions, 2 deletions
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<BotParams>,
-) -> (StatusCode, Json<Bot>) {
+) -> Result<(StatusCode, Json<Bot>), SaveBotError> {
+ validate_bot_name(&params.name)?;
+ let existing_bot = bots::find_bot_by_name(&params.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: &params.name,
};
let bot = bots::create_bot(&bot_params, &conn).unwrap();
- (StatusCode::CREATED, Json(bot))
+ Ok((StatusCode::CREATED, Json(bot)))
}
// TODO: handle errors