From 88754d9f68bb9628e912315888451db802c20c52 Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Sat, 2 Mar 2024 18:57:48 +0200 Subject: [PATCH] include created_at and updated_at in mod payloads --- Cargo.lock | 9 ++++++--- Cargo.toml | 1 + src/types/models/mod_entity.rs | 28 ++++++++++++++++++++++++---- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d0a9b47..e63b3eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -505,14 +505,16 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.31" +version = "0.4.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38" +checksum = "5bc015644b92d5890fab7489e49d21f879d5c990186827d42ec511919404f38b" dependencies = [ "android-tzdata", "iana-time-zone", + "js-sys", "num-traits", - "windows-targets 0.48.5", + "wasm-bindgen", + "windows-targets 0.52.0", ] [[package]] @@ -1024,6 +1026,7 @@ version = "0.1.0" dependencies = [ "actix-web", "anyhow", + "chrono", "clap", "dotenvy", "env_logger", diff --git a/Cargo.toml b/Cargo.toml index dfd7f0e..d68a5f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,3 +28,4 @@ sha256 = "1.5.0" semver = "1.0.21" clap = { version = "4.5.1", features = ["derive"] } regex = "1.10.3" +chrono = "0.4.34" diff --git a/src/types/models/mod_entity.rs b/src/types/models/mod_entity.rs index 46bbf27..c6d0824 100644 --- a/src/types/models/mod_entity.rs +++ b/src/types/models/mod_entity.rs @@ -14,9 +14,13 @@ use crate::{ }, }; use actix_web::web::Bytes; +use chrono::SecondsFormat; use reqwest::Client; use serde::Serialize; -use sqlx::{PgConnection, Postgres, QueryBuilder}; +use sqlx::{ + types::chrono::{DateTime, Utc}, + PgConnection, Postgres, QueryBuilder, +}; use std::{collections::HashMap, io::Cursor, str::FromStr}; use super::{ @@ -38,6 +42,8 @@ pub struct Mod { pub tags: Vec, pub about: Option, pub changelog: Option, + pub created_at: String, + pub updated_at: String, } #[derive(Serialize, Debug)] @@ -58,6 +64,8 @@ struct ModRecord { featured: bool, about: Option, changelog: Option, + created_at: DateTime, + updated_at: DateTime, } #[derive(sqlx::FromRow)] @@ -79,6 +87,8 @@ struct ModRecordGetOne { mod_id: String, about: Option, changelog: Option, + created_at: DateTime, + updated_at: DateTime, } impl Mod { @@ -111,8 +121,8 @@ impl Mod { } } let mut builder: QueryBuilder = QueryBuilder::new( - "SELECT q.id, q.repository, q.about, q.changelog, q.download_count, q.featured - FROM (SELECT m.id, m.repository, m.about, m.changelog, m.download_count, m.featured, m.updated_at, + "SELECT q.id, q.repository, q.about, q.changelog, q.download_count, q.featured, q.created_at, q.updated_at + FROM (SELECT m.id, m.repository, m.about, m.changelog, m.download_count, m.featured, m.created_at, m.updated_at, row_number() over (partition by m.id order by mv.id desc) rn FROM mods m INNER JOIN mod_versions mv ON m.id = mv.mod_id INNER JOIN mod_gd_versions mgv ON mgv.mod_id = mv.id " @@ -320,6 +330,8 @@ impl Mod { versions: vec![version], tags, developers: devs, + created_at: x.created_at.to_rfc3339_opts(SecondsFormat::Secs, true), + updated_at: x.updated_at.to_rfc3339_opts(SecondsFormat::Secs, true), about: None, changelog: None, } @@ -361,6 +373,8 @@ impl Mod { versions: version, tags, developers: devs, + created_at: x.created_at.to_rfc3339_opts(SecondsFormat::Secs, true), + updated_at: x.updated_at.to_rfc3339_opts(SecondsFormat::Secs, true), about: x.about, changelog: x.changelog, } @@ -450,7 +464,7 @@ impl Mod { let records: Vec = sqlx::query_as!( ModRecordGetOne, "SELECT - m.id, m.repository, m.about, m.changelog, m.featured, m.download_count as mod_download_count, + m.id, m.repository, m.about, m.changelog, m.featured, m.download_count as mod_download_count, m.created_at, m.updated_at, mv.id as version_id, mv.name, mv.description, mv.version, mv.download_link, mv.download_count as mod_version_download_count, mv.hash, mv.geode, mv.early_load, mv.api, mv.mod_id FROM mods m @@ -508,6 +522,12 @@ impl Mod { versions, tags, developers: devs, + created_at: records[0] + .created_at + .to_rfc3339_opts(SecondsFormat::Secs, true), + updated_at: records[0] + .updated_at + .to_rfc3339_opts(SecondsFormat::Secs, true), about: records[0].about.clone(), changelog: records[0].changelog.clone(), };