feat: sqlx postgres: impl interface to get all forges
This commit is contained in:
parent
1ec46670ec
commit
7e1903f807
2 changed files with 79 additions and 13 deletions
|
@ -282,6 +282,36 @@
|
||||||
},
|
},
|
||||||
"query": "INSERT INTO \n starchart_users (\n hostname_id, username, html_url,\n profile_photo_html_url, added_on, last_crawl_on\n ) \n VALUES (\n (SELECT ID FROM starchart_forges WHERE hostname = $1), $2, $3, $4, $5, $6)"
|
"query": "INSERT INTO \n starchart_users (\n hostname_id, username, html_url,\n profile_photo_html_url, added_on, last_crawl_on\n ) \n VALUES (\n (SELECT ID FROM starchart_forges WHERE hostname = $1), $2, $3, $4, $5, $6)"
|
||||||
},
|
},
|
||||||
|
"c0439c4b2d683c516bd29780cd1e39a7bc75adaebdb450b864eb0b424f401b0c": {
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "hostname",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "last_crawl_on",
|
||||||
|
"ordinal": 1,
|
||||||
|
"type_info": "Int64"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "name",
|
||||||
|
"ordinal": 2,
|
||||||
|
"type_info": "Text"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Right": 2
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"query": "SELECT\n\t\thostname,\n\t\tlast_crawl_on,\n\t\tstarchart_forge_type.name\n FROM\n starchart_forges\n INNER JOIN\n starchart_forge_type\n ON\n starchart_forges.forge_type = starchart_forge_type.id\n ORDER BY\n starchart_forges.ID\n LIMIT $1 OFFSET $2;\n "
|
||||||
|
},
|
||||||
"e00c8a8b0dbeb4a89a673864055c137365c2ae7bc5daf677bdacb20f21d0fcb2": {
|
"e00c8a8b0dbeb4a89a673864055c137365c2ae7bc5daf677bdacb20f21d0fcb2": {
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [],
|
"columns": [],
|
||||||
|
|
|
@ -144,11 +144,6 @@ impl SCDatabase for Database {
|
||||||
|
|
||||||
/// get forge isntance data
|
/// get forge isntance data
|
||||||
async fn get_forge(&self, hostname: &str) -> DBResult<Forge> {
|
async fn get_forge(&self, hostname: &str) -> DBResult<Forge> {
|
||||||
struct InnerForge {
|
|
||||||
hostname: String,
|
|
||||||
last_crawl_on: Option<i64>,
|
|
||||||
name: String,
|
|
||||||
}
|
|
||||||
let f = sqlx::query_as!(
|
let f = sqlx::query_as!(
|
||||||
InnerForge,
|
InnerForge,
|
||||||
"SELECT
|
"SELECT
|
||||||
|
@ -170,13 +165,38 @@ impl SCDatabase for Database {
|
||||||
.await
|
.await
|
||||||
.map_err(|e| DBError::DBError(Box::new(e)))?;
|
.map_err(|e| DBError::DBError(Box::new(e)))?;
|
||||||
|
|
||||||
let f = Forge {
|
Ok(f.into())
|
||||||
hostname: f.hostname,
|
}
|
||||||
last_crawl_on: f.last_crawl_on,
|
|
||||||
forge_type: ForgeImplementation::from_str(&f.name).unwrap(),
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(f)
|
/// Get all forges
|
||||||
|
async fn get_all_forges(&self, offset: u32, limit: u32) -> DBResult<Vec<Forge>> {
|
||||||
|
let mut inter_forges = sqlx::query_as!(
|
||||||
|
InnerForge,
|
||||||
|
"SELECT
|
||||||
|
hostname,
|
||||||
|
last_crawl_on,
|
||||||
|
starchart_forge_type.name
|
||||||
|
FROM
|
||||||
|
starchart_forges
|
||||||
|
INNER JOIN
|
||||||
|
starchart_forge_type
|
||||||
|
ON
|
||||||
|
starchart_forges.forge_type = starchart_forge_type.id
|
||||||
|
ORDER BY
|
||||||
|
starchart_forges.ID
|
||||||
|
LIMIT $1 OFFSET $2;
|
||||||
|
",
|
||||||
|
limit,
|
||||||
|
offset
|
||||||
|
)
|
||||||
|
.fetch_all(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| DBError::DBError(Box::new(e)))?;
|
||||||
|
|
||||||
|
let mut forges: Vec<Forge> = Vec::with_capacity(inter_forges.len());
|
||||||
|
inter_forges.drain(0..).for_each(|f| forges.push(f.into()));
|
||||||
|
|
||||||
|
Ok(forges)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// check if a forge instance exists
|
/// check if a forge instance exists
|
||||||
|
@ -456,7 +476,7 @@ impl SCDatabase for Database {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get all repositories
|
/// Get all repositories
|
||||||
async fn get_all_repositories(&self, page: u32, limit: u32) -> DBResult<Vec<Repository>> {
|
async fn get_all_repositories(&self, offset: u32, limit: u32) -> DBResult<Vec<Repository>> {
|
||||||
struct InnerRepository {
|
struct InnerRepository {
|
||||||
/// html link to the repository
|
/// html link to the repository
|
||||||
pub html_url: String,
|
pub html_url: String,
|
||||||
|
@ -499,7 +519,7 @@ ORDER BY
|
||||||
LIMIT $1 OFFSET $2
|
LIMIT $1 OFFSET $2
|
||||||
;",
|
;",
|
||||||
limit,
|
limit,
|
||||||
page,
|
offset,
|
||||||
)
|
)
|
||||||
.fetch_all(&self.pool)
|
.fetch_all(&self.pool)
|
||||||
.await
|
.await
|
||||||
|
@ -550,3 +570,19 @@ LIMIT $1 OFFSET $2
|
||||||
fn now_unix_time_stamp() -> i64 {
|
fn now_unix_time_stamp() -> i64 {
|
||||||
OffsetDateTime::now_utc().unix_timestamp()
|
OffsetDateTime::now_utc().unix_timestamp()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct InnerForge {
|
||||||
|
hostname: String,
|
||||||
|
last_crawl_on: Option<i64>,
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<InnerForge> for Forge {
|
||||||
|
fn from(f: InnerForge) -> Self {
|
||||||
|
Self {
|
||||||
|
hostname: f.hostname,
|
||||||
|
last_crawl_on: f.last_crawl_on,
|
||||||
|
forge_type: ForgeImplementation::from_str(&f.name).unwrap(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue