diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-07-23 23:40:25 +0200 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-07-23 23:40:25 +0200 |
commit | 4a582e8079178a7ac11f2a492e7988fcdaa210cd (patch) | |
tree | 1403b3e409e184bb53e0349d7e68e673fe7d7b3c /planetwars-server/src/db/bots.rs | |
parent | f19a70e710b8bf4605625516aa7e4c0cc7ace2e4 (diff) | |
download | planetwars.dev-4a582e8079178a7ac11f2a492e7988fcdaa210cd.tar.xz planetwars.dev-4a582e8079178a7ac11f2a492e7988fcdaa210cd.zip |
store active version id in bots table
Diffstat (limited to 'planetwars-server/src/db/bots.rs')
-rw-r--r-- | planetwars-server/src/db/bots.rs | 48 |
1 files changed, 41 insertions, 7 deletions
diff --git a/planetwars-server/src/db/bots.rs b/planetwars-server/src/db/bots.rs index a112a9a..a0a31b0 100644 --- a/planetwars-server/src/db/bots.rs +++ b/planetwars-server/src/db/bots.rs @@ -16,6 +16,7 @@ pub struct Bot { pub id: i32, pub owner_id: Option<i32>, pub name: String, + pub active_version: Option<i32>, } pub fn create_bot(new_bot: &NewBot, conn: &PgConnection) -> QueryResult<Bot> { @@ -38,11 +39,34 @@ pub fn find_bot_by_name(name: &str, conn: &PgConnection) -> QueryResult<Bot> { bots::table.filter(bots::name.eq(name)).first(conn) } +pub fn find_bot_with_version_by_name( + bot_name: &str, + conn: &PgConnection, +) -> QueryResult<(Bot, BotVersion)> { + bots::table + .inner_join(bot_versions::table.on(bots::active_version.eq(bot_versions::id.nullable()))) + .filter(bots::name.eq(bot_name)) + .first(conn) +} + +pub fn all_active_bots_with_version(conn: &PgConnection) -> QueryResult<Vec<(Bot, BotVersion)>> { + bots::table + .inner_join(bot_versions::table.on(bots::active_version.eq(bot_versions::id.nullable()))) + .get_results(conn) +} + pub fn find_all_bots(conn: &PgConnection) -> QueryResult<Vec<Bot>> { - // TODO: filter out bots that cannot be run (have no valid code bundle associated with them) bots::table.get_results(conn) } +/// Find all bots that have an associated active version. +/// These are the bots that can be run. +pub fn find_active_bots(conn: &PgConnection) -> QueryResult<Vec<Bot>> { + bots::table + .filter(bots::active_version.is_not_null()) + .get_results(conn) +} + #[derive(Insertable)] #[table_name = "bot_versions"] pub struct NewBotVersion<'a> { @@ -69,15 +93,25 @@ pub fn create_bot_version( .get_result(conn) } -pub fn find_bot_versions(bot_id: i32, conn: &PgConnection) -> QueryResult<Vec<BotVersion>> { +pub fn set_active_version( + bot_id: i32, + version_id: Option<i32>, + conn: &PgConnection, +) -> QueryResult<()> { + diesel::update(bots::table.filter(bots::id.eq(bot_id))) + .set(bots::active_version.eq(version_id)) + .execute(conn)?; + Ok(()) +} + +pub fn find_bot_version(version_id: i32, conn: &PgConnection) -> QueryResult<BotVersion> { bot_versions::table - .filter(bot_versions::bot_id.eq(bot_id)) - .get_results(conn) + .filter(bot_versions::id.eq(version_id)) + .first(conn) } -pub fn active_bot_version(bot_id: i32, conn: &PgConnection) -> QueryResult<BotVersion> { +pub fn find_bot_versions(bot_id: i32, conn: &PgConnection) -> QueryResult<Vec<BotVersion>> { bot_versions::table .filter(bot_versions::bot_id.eq(bot_id)) - .order(bot_versions::created_at.desc()) - .first(conn) + .get_results(conn) } |