aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/database.rs65
-rw-r--r--src/github.rs14
-rw-r--r--src/main.rs11
3 files changed, 78 insertions, 12 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(())
}
}
diff --git a/src/github.rs b/src/github.rs
index ec00420..8c853da 100644
--- a/src/github.rs
+++ b/src/github.rs
@@ -11,13 +11,13 @@ const USER_AGENT: &'static str = concat!(
#[derive(Debug, Deserialize)]
pub struct Repo {
- id: usize,
- name: String,
- description: Option<String>,
- fork: bool,
- git_url: String,
- default_branch: String,
- updated_at: String, // TODO: Maybe parse to date?
+ pub id: i64,
+ pub name: String,
+ pub description: Option<String>,
+ pub fork: bool,
+ pub git_url: String,
+ pub default_branch: String,
+ pub updated_at: String, // TODO: Maybe parse to date?
}
diff --git a/src/main.rs b/src/main.rs
index acbcd23..9734804 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -22,5 +22,14 @@ async fn main() {
let mut db = database::Db::connect("test.db").await.unwrap();
- db.create().await.unwrap();
+ // db.create().await.unwrap();
+
+ // If repo !exists
+ // insert
+ // mirror
+ // Else
+ // Update updated_at
+ // fetch
+
+ dbg!(db.repo_get(2).await.unwrap());
}