aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src/db/ratings.rs
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-05-17 21:13:29 +0200
committerIlion Beyst <ilion.beyst@gmail.com>2022-05-17 21:13:29 +0200
commit30de8107b499741808150db61abecd623bf1581b (patch)
tree8184324e170dc50cbaf1003588fa1df136454c1d /planetwars-server/src/db/ratings.rs
parenta7d56ba0f54273920743109fe1a6541030f3c003 (diff)
downloadplanetwars.dev-30de8107b499741808150db61abecd623bf1581b.tar.xz
planetwars.dev-30de8107b499741808150db61abecd623bf1581b.zip
implement leaderboard endpoint
Diffstat (limited to 'planetwars-server/src/db/ratings.rs')
-rw-r--r--planetwars-server/src/db/ratings.rs29
1 files changed, 28 insertions, 1 deletions
diff --git a/planetwars-server/src/db/ratings.rs b/planetwars-server/src/db/ratings.rs
index bee8548..8262fed 100644
--- a/planetwars-server/src/db/ratings.rs
+++ b/planetwars-server/src/db/ratings.rs
@@ -1,7 +1,8 @@
use diesel::{prelude::*, PgConnection, QueryResult};
use serde::{Deserialize, Serialize};
-use crate::schema::{bots, ratings};
+use crate::db::bots::Bot;
+use crate::schema::{bots, ratings, users};
#[derive(Queryable, Debug, Insertable, PartialEq, Serialize, Deserialize)]
pub struct Rating {
@@ -25,3 +26,29 @@ pub fn set_rating(bot_id: i32, rating: f64, db_conn: &PgConnection) -> QueryResu
.set(ratings::rating.eq(rating))
.execute(db_conn)
}
+
+#[derive(Queryable, Serialize, Deserialize)]
+pub struct Author {
+ id: i32,
+ username: String,
+}
+
+#[derive(Queryable, Serialize, Deserialize)]
+pub struct RankedBot {
+ pub bot: Bot,
+ pub author: Option<Author>,
+ pub rating: f64,
+}
+
+pub fn get_bot_ranking(db_conn: &PgConnection) -> QueryResult<Vec<RankedBot>> {
+ bots::table
+ .left_join(users::table)
+ .inner_join(ratings::table)
+ .select((
+ bots::all_columns,
+ (users::id, users::username).nullable(),
+ ratings::rating,
+ ))
+ .order_by(ratings::rating.desc())
+ .get_results(db_conn)
+}