Track and sort by release date of game_versions tags (#95)

This commit is contained in:
Aeledfyr
2020-10-31 23:06:47 -05:00
committed by GitHub
parent a4ba6d1444
commit da79386cc3
5 changed files with 108 additions and 55 deletions

View File

@@ -272,6 +272,7 @@ impl<'a> LoaderBuilder<'a> {
pub struct GameVersionBuilder<'a> {
pub version: Option<&'a str>,
pub version_type: Option<&'a str>,
pub date: Option<&'a chrono::DateTime<chrono::Utc>>,
}
impl GameVersion {
@@ -330,6 +331,7 @@ impl GameVersion {
let result = sqlx::query!(
"
SELECT version FROM game_versions
ORDER BY created DESC
"
)
.fetch_many(exec)
@@ -348,6 +350,7 @@ impl GameVersion {
"
SELECT version FROM game_versions
WHERE type = $1
ORDER BY created DESC
",
version_type
)
@@ -417,20 +420,32 @@ impl<'a> GameVersionBuilder<'a> {
}
}
pub fn created(self, created: &'a chrono::DateTime<chrono::Utc>) -> GameVersionBuilder<'a> {
Self {
date: Some(created),
..self
}
}
pub async fn insert<'b, E>(self, exec: E) -> Result<GameVersionId, DatabaseError>
where
E: sqlx::Executor<'b, Database = sqlx::Postgres>,
{
// This looks like a mess, but it *should* work
// This allows game versions to be partially updated without
// replacing the unspecified fields with defaults.
let result = sqlx::query!(
"
INSERT INTO game_versions (version, type)
VALUES ($1, $2)
INSERT INTO game_versions (version, type, created)
VALUES ($1, COALESCE($2, 'other'), COALESCE($3, timezone('utc', now())))
ON CONFLICT (version) DO UPDATE
SET type = excluded.type
SET type = COALESCE($2, game_versions.type),
created = COALESCE($3, game_versions.created)
RETURNING id
",
self.version,
self.version_type,
self.date.map(chrono::DateTime::naive_utc),
)
.fetch_one(exec)
.await?;