bot upload PoC
This commit is contained in:
parent
da3d7ed02d
commit
5265e19507
2 changed files with 32 additions and 0 deletions
|
@ -90,6 +90,7 @@ pub async fn api(configuration: Configuration) -> Router {
|
||||||
get(routes::matches::get_match_log),
|
get(routes::matches::get_match_log),
|
||||||
)
|
)
|
||||||
.route("/submit_bot", post(routes::demo::submit_bot))
|
.route("/submit_bot", post(routes::demo::submit_bot))
|
||||||
|
.route("/save_bot", post(routes::bots::save_bot))
|
||||||
.layer(AddExtensionLayer::new(db_pool));
|
.layer(AddExtensionLayer::new(db_pool));
|
||||||
api
|
api
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use axum::extract::{Multipart, Path};
|
use axum::extract::{Multipart, Path};
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
use axum::Json;
|
use axum::Json;
|
||||||
|
use diesel::OptionalExtension;
|
||||||
use rand::distributions::Alphanumeric;
|
use rand::distributions::Alphanumeric;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
@ -10,9 +11,39 @@ use std::path::PathBuf;
|
||||||
|
|
||||||
use crate::db::bots::{self, CodeBundle};
|
use crate::db::bots::{self, CodeBundle};
|
||||||
use crate::db::users::User;
|
use crate::db::users::User;
|
||||||
|
use crate::modules::bots::save_code_bundle;
|
||||||
use crate::{DatabaseConnection, BOTS_DIR};
|
use crate::{DatabaseConnection, BOTS_DIR};
|
||||||
use bots::Bot;
|
use bots::Bot;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct SaveBotParams {
|
||||||
|
pub bot_name: String,
|
||||||
|
pub code: String,
|
||||||
|
}
|
||||||
|
pub async fn save_bot(
|
||||||
|
Json(params): Json<SaveBotParams>,
|
||||||
|
conn: DatabaseConnection,
|
||||||
|
) -> Result<(), StatusCode> {
|
||||||
|
// TODO: authorization
|
||||||
|
let res = bots::find_bot_by_name(¶ms.bot_name, &conn)
|
||||||
|
.optional()
|
||||||
|
.expect("could not run query");
|
||||||
|
let bot = match res {
|
||||||
|
Some(bot) => bot,
|
||||||
|
None => {
|
||||||
|
let new_bot = bots::NewBot {
|
||||||
|
owner_id: None,
|
||||||
|
name: ¶ms.bot_name,
|
||||||
|
};
|
||||||
|
let bot = bots::create_bot(&new_bot, &conn).expect("could not create bot");
|
||||||
|
bot
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let _code_bundle =
|
||||||
|
save_code_bundle(¶ms.code, Some(bot.id), &conn).expect("failed to save code bundle");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct BotParams {
|
pub struct BotParams {
|
||||||
name: String,
|
name: String,
|
||||||
|
|
Loading…
Reference in a new issue