aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-07-24 16:45:29 +0200
committerIlion Beyst <ilion.beyst@gmail.com>2022-07-24 16:45:29 +0200
commitccfe86729e3a454e3fdf529abd7063ceb8fa859f (patch)
tree0c60dfbc76c1e2c2ba6c71a5201fde18690969f5 /planetwars-server/src
parent33664eff2c93136658b7f863c95e1bfda91141ee (diff)
downloadplanetwars.dev-ccfe86729e3a454e3fdf529abd7063ceb8fa859f.tar.xz
planetwars.dev-ccfe86729e3a454e3fdf529abd7063ceb8fa859f.zip
add bot detail page
Diffstat (limited to 'planetwars-server/src')
-rw-r--r--planetwars-server/src/db/users.rs6
-rw-r--r--planetwars-server/src/lib.rs4
-rw-r--r--planetwars-server/src/routes/bots.rs23
3 files changed, 25 insertions, 8 deletions
diff --git a/planetwars-server/src/db/users.rs b/planetwars-server/src/db/users.rs
index 1098204..ebb2268 100644
--- a/planetwars-server/src/db/users.rs
+++ b/planetwars-server/src/db/users.rs
@@ -57,6 +57,12 @@ pub fn create_user(credentials: &Credentials, conn: &PgConnection) -> QueryResul
.get_result::<User>(conn)
}
+pub fn find_user(user_id: i32, db_conn: &PgConnection) -> QueryResult<User> {
+ users::table
+ .filter(users::id.eq(user_id))
+ .first::<User>(db_conn)
+}
+
pub fn find_user_by_name(username: &str, db_conn: &PgConnection) -> QueryResult<User> {
users::table
.filter(users::username.eq(username))
diff --git a/planetwars-server/src/lib.rs b/planetwars-server/src/lib.rs
index ccf7cfc..3ad0c88 100644
--- a/planetwars-server/src/lib.rs
+++ b/planetwars-server/src/lib.rs
@@ -124,9 +124,9 @@ pub fn api() -> Router {
"/bots",
get(routes::bots::list_bots).post(routes::bots::create_bot),
)
- .route("/bots/:bot_id", get(routes::bots::get_bot))
+ .route("/bots/:bot_name", get(routes::bots::get_bot))
.route(
- "/bots/:bot_id/upload",
+ "/bots/:bot_name/upload",
post(routes::bots::upload_code_multipart),
)
.route("/matches", get(routes::matches::list_matches))
diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs
index 896359c..8de479f 100644
--- a/planetwars-server/src/routes/bots.rs
+++ b/planetwars-server/src/routes/bots.rs
@@ -20,6 +20,8 @@ use crate::modules::bots::save_code_string;
use crate::{DatabaseConnection, GlobalConfig};
use bots::Bot;
+use super::users::UserData;
+
#[derive(Serialize, Deserialize, Debug)]
pub struct SaveBotParams {
pub bot_name: String,
@@ -148,14 +150,23 @@ pub async fn create_bot(
// TODO: handle errors
pub async fn get_bot(
conn: DatabaseConnection,
- Path(bot_id): Path<i32>,
+ Path(bot_name): Path<String>,
) -> Result<Json<JsonValue>, StatusCode> {
- let bot = bots::find_bot(bot_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?;
- let bundles =
+ let bot = db::bots::find_bot_by_name(&bot_name, &conn).map_err(|_| StatusCode::NOT_FOUND)?;
+ let owner: Option<UserData> = match bot.owner_id {
+ Some(user_id) => {
+ let user = db::users::find_user(user_id, &conn)
+ .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
+ Some(user.into())
+ }
+ None => None,
+ };
+ let versions =
bots::find_bot_versions(bot.id, &conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(Json(json!({
"bot": bot,
- "bundles": bundles,
+ "owner": owner,
+ "versions": versions,
})))
}
@@ -187,13 +198,13 @@ pub async fn get_ranking(conn: DatabaseConnection) -> Result<Json<Vec<RankedBot>
pub async fn upload_code_multipart(
conn: DatabaseConnection,
user: User,
- Path(bot_id): Path<i32>,
+ Path(bot_name): Path<String>,
mut multipart: Multipart,
Extension(config): Extension<Arc<GlobalConfig>>,
) -> Result<Json<BotVersion>, StatusCode> {
let bots_dir = PathBuf::from(&config.bots_directory);
- let bot = bots::find_bot(bot_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?;
+ let bot = bots::find_bot_by_name(&bot_name, &conn).map_err(|_| StatusCode::NOT_FOUND)?;
if Some(user.id) != bot.owner_id {
return Err(StatusCode::FORBIDDEN);