aboutsummaryrefslogtreecommitdiffstats
path: root/src/database.rs
diff options
context:
space:
mode:
authorTeddy Wing2021-05-30 00:13:47 +0200
committerTeddy Wing2021-05-30 00:14:27 +0200
commit768b67c966e9a78864b450b6051974d096f1d412 (patch)
tree7da6f88c3f0b39a2a614db9e9a28502a5084a425 /src/database.rs
parent8130bc32fa81821e5511500f2f64bd964c58f3db (diff)
downloadreflectub-768b67c966e9a78864b450b6051974d096f1d412.tar.bz2
database: Add a way to get a single repo
Diffstat (limited to 'src/database.rs')
-rw-r--r--src/database.rs65
1 files changed, 61 insertions, 4 deletions
diff --git a/src/database.rs b/src/database.rs
index 7ce2c52..a49675f 100644
--- a/src/database.rs
+++ b/src/database.rs
@@ -1,8 +1,17 @@
-use sqlx::{self, ConnectOptions, Connection, Executor};
+use sqlx::{self, ConnectOptions, Connection, Executor, Row};
-use crate::github::Repo;
+use crate::github::Repo as GithubRepo;
+#[derive(Debug)]
+pub struct Repo {
+ id: Option<i64>,
+ name: Option<String>,
+ updated_at: Option<String>,
+}
+
+
+#[derive(Debug)]
pub struct Db {
connection: sqlx::SqliteConnection,
}
@@ -43,15 +52,63 @@ impl Db {
Ok(())
}
- pub fn repo_insert(repo: &Repo) -> Result<(), Box<dyn std::error::Error>> {
+ pub async fn repo_get(
+ &mut self,
+ id: i64,
+ ) -> Result<Repo, Box<dyn std::error::Error>> {
+ let mut tx = self.connection.begin().await?;
+
+ let row = sqlx::query("SELECT id, name FROM repositories where id = ?")
+ .bind(id)
+ .fetch_one(&mut tx)
+ .await?;
+
+ tx.commit().await?;
+
+ if row.is_empty() {
+ return Err("not found".into());
+ }
+
+ Ok(
+ Repo {
+ id: Some(row.get(0)),
+ name: Some(row.get(1)),
+ updated_at: None,
+ }
+ )
+ }
+
+ pub async fn repo_insert(
+ &mut self,
+ repos: &[GithubRepo],
+ ) -> Result<(), Box<dyn std::error::Error>> {
+ let mut tx = self.connection.begin().await?;
+
+ for repo in repos {
+ sqlx::query(r#"
+ INSERT INTO repositories
+ (id, name, updated_at)
+ VALUES
+ (?, ?, ?)
+ "#)
+ .bind(repo.id)
+ .bind(&repo.name)
+ .bind(&repo.updated_at)
+ .execute(&mut tx)
+ .await?;
+ }
+
+ tx.commit().await?;
+
Ok(())
}
pub fn repo_is_updated() -> Result<bool, Box<dyn std::error::Error>> {
+ // select id from repositories where updated_at > datetime("2020-07-13T17:57:56Z");
Ok(false)
}
- pub fn repo_update(repo: &Repo) -> Result<(), Box<dyn std::error::Error>> {
+ pub fn repo_update(repo: &GithubRepo) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
}