I'm not that good with Diesel yet am I
Some checks failed
continuous-integration/drone the build failed

This commit is contained in:
Jef Roosens 2021-06-28 18:37:26 +02:00
parent 4faf19aae5
commit e1282dacb0
Signed by: Jef Roosens
GPG key ID: 955C0660072F691F
10 changed files with 87 additions and 3 deletions

View file

@ -4,13 +4,15 @@ extern crate rocket;
#[macro_use]
extern crate diesel_migrations;
mod routes;
use rocket::{fairing::AdHoc, Build, Rocket};
use rocket_sync_db_pools::{database, diesel};
embed_migrations!();
#[database("postgres_hilde")]
struct HildeDbConn(diesel::PgConnection);
pub struct HildeDbConn(diesel::PgConnection);
async fn run_db_migrations(rocket: Rocket<Build>) -> Result<Rocket<Build>, Rocket<Build>> {
let conn = HildeDbConn::get_one(&rocket)
@ -31,4 +33,5 @@ fn rocket() -> _ {
"Run database migrations",
run_db_migrations,
))
.mount("/", routes::routes())
}

13
src/hilde/routes/mod.rs Normal file
View file

@ -0,0 +1,13 @@
use rocket::Route;
use libhilde::packages::generate_release_file;
use crate::HildeDbConn;
pub fn routes() -> Vec<Route> {
routes![get_dist_release]
}
// NOTE: for now, only dists without slashes are supported
#[get("/dists/<dist>/Release")]
async fn get_dist_release(conn: HildeDbConn, dist: String) -> String {
conn.run(|c| generate_release_file(dist, c)).await
}

View file

@ -0,0 +1,5 @@
#[macro_use]
extern crate diesel;
pub mod packages;
pub mod schema;

21
src/libhilde/packages.rs Normal file
View file

@ -0,0 +1,21 @@
use diesel::{PgConnection, Queryable, QueryDsl, ExpressionMethods, RunQueryDsl};
use crate::schema::distributions::dsl::*;
#[derive(Queryable)]
struct Distribution {
id: uuid::Uuid,
name: String,
description: Option<String>,
origin: Option<String>,
label: Option<String>,
version: Option<String>,
suite: Option<String>,
codename: Option<String>
}
/// Generate a Release file for a given distribution
pub fn generate_release_file(dist: String, conn: &PgConnection) -> String {
let results = distributions.filter(name.eq(dist)).load::<Distribution>(conn).unwrap();
format!("{}", results.len())
}

12
src/libhilde/schema.rs Normal file
View file

@ -0,0 +1,12 @@
table! {
distributions (id) {
id -> Uuid,
name -> Text,
description -> Nullable<Text>,
origin -> Nullable<Text>,
label -> Nullable<Text>,
version -> Nullable<Text>,
suite -> Nullable<Text>,
codename -> Nullable<Text>,
}
}