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)"
|
||||
},
|
||||
"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": {
|
||||
"describe": {
|
||||
"columns": [],
|
||||
|
|
|
@ -144,11 +144,6 @@ impl SCDatabase for Database {
|
|||
|
||||
/// get forge isntance data
|
||||
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!(
|
||||
InnerForge,
|
||||
"SELECT
|
||||
|
@ -170,13 +165,38 @@ impl SCDatabase for Database {
|
|||
.await
|
||||
.map_err(|e| DBError::DBError(Box::new(e)))?;
|
||||
|
||||
let f = Forge {
|
||||
hostname: f.hostname,
|
||||
last_crawl_on: f.last_crawl_on,
|
||||
forge_type: ForgeImplementation::from_str(&f.name).unwrap(),
|
||||
};
|
||||
Ok(f.into())
|
||||
}
|
||||
|
||||
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
|
||||
|
@ -456,7 +476,7 @@ impl SCDatabase for Database {
|
|||
}
|
||||
|
||||
/// 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 {
|
||||
/// html link to the repository
|
||||
pub html_url: String,
|
||||
|
@ -499,7 +519,7 @@ ORDER BY
|
|||
LIMIT $1 OFFSET $2
|
||||
;",
|
||||
limit,
|
||||
page,
|
||||
offset,
|
||||
)
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
|
@ -550,3 +570,19 @@ LIMIT $1 OFFSET $2
|
|||
fn now_unix_time_stamp() -> i64 {
|
||||
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