allow filtering for had_errors

This commit is contained in:
Ilion Beyst 2022-10-14 14:06:22 +02:00
parent 353ecd4c16
commit b51b7a331d
4 changed files with 36 additions and 2 deletions

View file

@ -8,9 +8,12 @@ use diesel::{PgConnection, QueryResult, RunQueryDsl};
pub struct ListBotMatches { pub struct ListBotMatches {
pub bot_id: i32, pub bot_id: i32,
pub opponent_id: Option<i32>, pub had_errors: Option<bool>,
pub outcome: Option<BotMatchOutcome>, pub outcome: Option<BotMatchOutcome>,
pub opponent_id: Option<i32>,
// pagination options
pub before: Option<NaiveDateTime>, pub before: Option<NaiveDateTime>,
pub after: Option<NaiveDateTime>, pub after: Option<NaiveDateTime>,
pub amount: i64, pub amount: i64,
@ -29,6 +32,12 @@ impl QueryFragment<Pg> for ListBotMatches {
"WHERE bot_id = " "WHERE bot_id = "
)); ));
out.push_bind_param::<Integer, _>(&self.bot_id)?; out.push_bind_param::<Integer, _>(&self.bot_id)?;
if let Some(had_errors) = self.had_errors.as_ref() {
out.push_sql(" AND match_players.had_errors = ");
out.push_bind_param::<Bool, _>(had_errors)?;
}
out.push_sql(") main_player ON matches.id = main_player.match_id"); out.push_sql(") main_player ON matches.id = main_player.match_id");
if let Some(opponent_id) = self.opponent_id.as_ref() { if let Some(opponent_id) = self.opponent_id.as_ref() {
@ -40,6 +49,7 @@ impl QueryFragment<Pg> for ListBotMatches {
"WHERE bot_id = " "WHERE bot_id = "
)); ));
out.push_bind_param::<Integer, _>(opponent_id)?; out.push_bind_param::<Integer, _>(opponent_id)?;
out.push_sql(") other_player ON matches.id = other_player.match_id"); out.push_sql(") other_player ON matches.id = other_player.match_id");
} }

View file

@ -177,6 +177,7 @@ pub fn list_bot_matches(
bot_id: i32, bot_id: i32,
opponent_id: Option<i32>, opponent_id: Option<i32>,
outcome: Option<BotMatchOutcome>, outcome: Option<BotMatchOutcome>,
had_errors: Option<bool>,
amount: i64, amount: i64,
before: Option<NaiveDateTime>, before: Option<NaiveDateTime>,
after: Option<NaiveDateTime>, after: Option<NaiveDateTime>,
@ -184,8 +185,9 @@ pub fn list_bot_matches(
) -> QueryResult<Vec<FullMatchData>> { ) -> QueryResult<Vec<FullMatchData>> {
let lbm = ListBotMatches { let lbm = ListBotMatches {
bot_id, bot_id,
opponent_id,
outcome, outcome,
had_errors,
opponent_id,
before, before,
after, after,
amount, amount,

View file

@ -44,6 +44,7 @@ pub struct ListRecentMatchesParams {
bot: Option<String>, bot: Option<String>,
opponent: Option<String>, opponent: Option<String>,
had_errors: Option<bool>,
outcome: Option<BotMatchOutcome>, outcome: Option<BotMatchOutcome>,
} }
@ -85,6 +86,7 @@ pub async fn list_recent_matches(
bot.id, bot.id,
opponent_id, opponent_id,
params.outcome, params.outcome,
params.had_errors,
count, count,
params.before, params.before,
params.after, params.after,

View file

@ -389,5 +389,25 @@ async fn test_list_matches_with_errors() -> io::Result<()> {
); );
assert_eq!(matches[0]["players"][1]["had_errors"].as_bool(), Some(true)); assert_eq!(matches[0]["players"][1]["had_errors"].as_bool(), Some(true));
// test had_errors filter
// TODO: maybe write a dedicated test for all list_matches options
let response = app
.call(
Request::builder()
.method(http::Method::GET)
.header("Content-Type", "application/json")
.uri(format!("/api/matches?bot=testbot&had_errors=false"))
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
let resp: JsonValue = serde_json::from_slice(&body).unwrap();
let matches = resp["matches"].as_array().unwrap();
assert_eq!(matches.len(), 0);
Ok(()) Ok(())
} }