Skip to content

Instantly share code, notes, and snippets.

@felipesere
Last active January 6, 2020 20:17
Show Gist options
  • Save felipesere/cd9b56d6c248d513d72768a2f4082abe to your computer and use it in GitHub Desktop.
Save felipesere/cd9b56d6c248d513d72768a2f4082abe to your computer and use it in GitHub Desktop.
// What I write right now
pub fn all(conn: &Conn) -> Result<Vec<FullStoredRepo>> {
use schema::repos::dsl::*;
let rs: Vec<StoredRepo> = repos.load(conn).with_context(|| "getting all repos")?;
let ids: Vec<i32> = rs.iter().map(|r| r.id).collect();
use schema::issues::dsl::{issues, repo_id as issue_repo_id};
let isx = issues
.filter(issue_repo_id.eq_any(ids.clone()))
.load::<StoredIssue>(conn)?
.grouped_by(&rs[..]);
use schema::pull_requests::dsl::{pull_requests, repo_id as pr_repo_id};
let prs = pull_requests
.filter(pr_repo_id.eq_any(ids.clone()))
.load::<StoredPullRequest>(conn)?
.grouped_by(&rs[..]);
use schema::repo_activity_log::dsl::{repo_activity_log, repo_id as activity_repo_id};
let activities = repo_activity_log
.filter(activity_repo_id.eq_any(ids.clone()))
.load::<StoredRepoEvent>(conn)?
.grouped_by(&rs[..]);
Result::Ok(
rs.into_iter()
.zip(isx)
.zip(prs)
.zip(activities)
.map(|(((repo, issue), prs), events)| FullStoredRepo {
id: repo.id,
title: repo.title.clone(),
issues: issue,
prs,
events,
})
.collect(),
)
}
// What I would like to write
pub fn all(conn: &Conn) -> Result<Vec<FullStoredRepo>> {
use schema::repos::dsl::{repos, id as repo_id};
let rs: Vec<StoredRepo> = repos.load(conn).with_context(|| "getting all repos")?;
let ids: Vec<i32> = rs.iter().map(|r| r.id).collect();
use schema::issues::dsl::issues;
let isx = issues
.filter(repo_id.eq_any(ids.clone())) // NOTE: repo_id is a use-alias to the id of the repo table
.load::<StoredIssue>(conn)?
.grouped_by(&rs[..]);
use schema::pull_requests::dsl::pull_requests;
let prs = pull_requests
.filter(repo_id.eq_any(ids.clone())) // NOTE: same
.load::<StoredPullRequest>(conn)?
.grouped_by(&rs[..]);
use schema::repo_activity_log::dsl::repo_activity_log;
let activities = repo_activity_log
.filter(repo_id.eq_any(ids.clone())) // NOTE: same
.load::<StoredRepoEvent>(conn)?
.grouped_by(&rs[..]);
Result::Ok(
rs.into_iter()
.zip(isx)
.zip(prs)
.zip(activities)
.map(|(((repo, issue), prs), events)| FullStoredRepo {
id: repo.id,
title: repo.title.clone(),
issues: issue,
prs,
events,
})
.collect(),
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment