Updated query builder with bindings

This commit is contained in:
2024-12-21 22:31:11 -05:00
parent 7718cf19c3
commit 2cd2715d0d
8 changed files with 169 additions and 86 deletions

View File

@@ -36,7 +36,6 @@ enum TrackDiceOperator {
GreaterThanEqual,
}
// Implementing the ToString trait for converting the enum to a string
impl Display for TrackDiceOperator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -110,8 +109,7 @@ impl QueryDiceTrack {
// )
// )
.build();
let items: Vec<QueryDiceTrack> = sqlx::query_as(&query)
.fetch_all(pool).await?;
let items: Vec<QueryDiceTrack> = sqlx::query_as(&query.0).fetch_all(pool).await?;
Ok(items)
}
@@ -131,7 +129,7 @@ impl InsertDiceTrack {
) VALUES (
$1, $2, $3, $4, $5, $6
) RETURNING *",
TABLE_NAME
TABLE_NAME
);
let item: QueryDiceTrack = match sqlx::query_as(&query)
.bind(self.guild_id)
@@ -140,9 +138,11 @@ impl InsertDiceTrack {
.bind(self.user_id)
.bind(self.value)
.bind(&self.operator)
.fetch_optional(pool).await? {
.fetch_optional(pool)
.await?
{
Some(result) => result,
None => return Err(Error::new(500, "Error storing".to_string()))
None => return Err(Error::new(500, "Error storing".to_string())),
};
Ok(item)
}
@@ -154,7 +154,6 @@ pub async fn add_track_dice(
Path(guild_id): Path<u64>,
Json(payload): Json<DiceTrackPayload>,
) -> SirenResult<Json<QueryDiceTrack>> {
// Check if the user exists in the cache
let owner_id = credential.user_id();
let owner_id = match state.cache.user(owner_id) {
@@ -179,9 +178,9 @@ pub async fn add_track_dice(
operator: match payload.operator {
None => None,
Some(s) => Some(s.to_string()),
}
},
};
let dice_track = dice.insert().await?;
Ok(Json(dice_track))
}
}