summaryrefslogtreecommitdiff
path: root/src/repository/models.rs
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-03-13 10:17:02 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-03-13 10:17:02 +0100
commit44a35e651741afb6c417da47d636e4380cdd225f (patch)
tree72302a7bea06350b1e7175a6dddb08daebbbb135 /src/repository/models.rs
parent48a9ac895b6e8b01622810ec4bf2f3a423426ca3 (diff)
feat: add get all repos from db
Diffstat (limited to 'src/repository/models.rs')
-rw-r--r--src/repository/models.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/repository/models.rs b/src/repository/models.rs
new file mode 100644
index 0000000..ec5559b
--- /dev/null
+++ b/src/repository/models.rs
@@ -0,0 +1,36 @@
+use crate::db::get_client;
+use crate::errors::AppError;
+use chrono::NaiveDateTime;
+use deadpool_postgres::Pool;
+use serde::{Deserialize, Serialize};
+use tokio_pg_mapper::FromTokioPostgresRow;
+use tokio_pg_mapper_derive::PostgresMapper;
+use uuid::Uuid;
+
+#[derive(Serialize, Deserialize, PostgresMapper)]
+#[pg_mapper(table = "repository")]
+pub struct Repository {
+ pub id: Uuid,
+ pub url: String,
+ pub created_at: NaiveDateTime,
+ pub updated_at: NaiveDateTime,
+ pub uploader_ip: String,
+}
+
+impl Repository {
+ pub async fn find_all(pool: Pool) -> Result<Vec<Repository>, AppError> {
+ let client = get_client(pool.clone()).await.unwrap();
+ let statement = client
+ .prepare("SELECT * FROM repository ORDER BY updated_at DESC")
+ .await?;
+
+ let repos = client
+ .query(&statement, &[])
+ .await?
+ .iter()
+ .map(|row| Repository::from_row_ref(row).unwrap())
+ .collect::<Vec<Repository>>();
+
+ Ok(repos)
+ }
+}