summaryrefslogtreecommitdiff
path: root/server/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/main.rs')
-rw-r--r--server/src/main.rs145
1 files changed, 84 insertions, 61 deletions
diff --git a/server/src/main.rs b/server/src/main.rs
index 9f7204a..413545b 100644
--- a/server/src/main.rs
+++ b/server/src/main.rs
@@ -1,13 +1,14 @@
-#[macro_use] extern crate rocket;
+#[macro_use]
+extern crate rocket;
-use futures::{stream::TryStreamExt, future::TryFutureExt};
+use futures::{future::TryFutureExt, stream::TryStreamExt};
-use rocket::{Rocket, Build, futures};
use rocket::fairing::{self, AdHoc};
-use rocket::request::{self, Outcome, Request, FromRequest};
-use rocket::response::{status::NotFound};
+use rocket::request::{self, FromRequest, Outcome, Request};
+use rocket::response::status::NotFound;
use rocket::serde::json::Json;
-use rocket_db_pools::{sqlx, Database, Connection};
+use rocket::{futures, Build, Rocket};
+use rocket_db_pools::{sqlx, Connection, Database};
mod api_model;
@@ -30,7 +31,9 @@ impl<'r> FromRequest<'r> for User {
type Error = UserError;
async fn from_request(_req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
- Outcome::Success(User{ username: String::from("foo") })
+ Outcome::Success(User {
+ username: String::from("foo"),
+ })
}
}
@@ -51,7 +54,7 @@ impl TryFrom<u8> for Role {
match value {
0 => Ok(Role::Reviewer),
1 => Ok(Role::Watcher),
- _ => Err("Invalid role")
+ _ => Err("Invalid role"),
}
}
}
@@ -65,30 +68,36 @@ impl TryFrom<u8> for api_model::ReviewState {
1 => Ok(api_model::ReviewState::Open),
2 => Ok(api_model::ReviewState::Dropped),
3 => Ok(api_model::ReviewState::Closed),
- _ => Err("Invalid review state")
+ _ => Err("Invalid review state"),
}
}
}
#[get("/projects?<limit>&<offset>")]
-async fn projects<'r>(mut db: Connection<Db>, _user: User, limit: Option<u32>, offset: Option<u32>) -> Json<api_model::Projects> {
+async fn projects<'r>(
+ mut db: Connection<Db>,
+ _user: User,
+ limit: Option<u32>,
+ offset: Option<u32>,
+) -> Json<api_model::Projects> {
let uw_offset = offset.unwrap_or(0);
let uw_limit = limit.unwrap_or(10);
let entries = sqlx::query!(
"SELECT id,title FROM projects ORDER BY title,id LIMIT ? OFFSET ?",
- uw_limit, uw_offset)
- .fetch(&mut **db)
- .map_ok(|r| api_model::ProjectEntry {
- id: r.id,
- title: r.title,
- })
- .try_collect::<Vec<_>>()
- .await
- .unwrap();
+ uw_limit,
+ uw_offset
+ )
+ .fetch(&mut **db)
+ .map_ok(|r| api_model::ProjectEntry {
+ id: r.id,
+ title: r.title,
+ })
+ .try_collect::<Vec<_>>()
+ .await
+ .unwrap();
- let count = sqlx::query!(
- "SELECT COUNT(id) AS count FROM projects")
+ let count = sqlx::query!("SELECT COUNT(id) AS count FROM projects")
.fetch_one(&mut **db)
.map_ok(|r| r.count)
.await
@@ -96,20 +105,22 @@ async fn projects<'r>(mut db: Connection<Db>, _user: User, limit: Option<u32>, o
let u32_count = u32::try_from(count).unwrap();
- Json(
- api_model::Projects {
- offset: uw_offset,
- limit: uw_limit,
- total_count: u32_count,
- more: uw_offset + uw_limit < u32_count,
- projects: entries,
- },
- )
+ Json(api_model::Projects {
+ offset: uw_offset,
+ limit: uw_limit,
+ total_count: u32_count,
+ more: uw_offset + uw_limit < u32_count,
+ projects: entries,
+ })
}
#[get("/project/<projectid>")]
-async fn project<'r>(mut db: Connection<Db>, _user: User, projectid: u64) -> Result<Json<api_model::Project>, NotFound<String>> {
- let members = sqlx::query!(
+async fn project<'r>(
+ mut db: Connection<Db>,
+ _user: User,
+ projectid: u64,
+) -> Result<Json<api_model::Project>, NotFound<String>> {
+ let members = sqlx::query!(
"SELECT id, username, name, active FROM users JOIN project_users ON project_users.user=users.id WHERE project_users.project=?",
projectid)
.fetch(&mut **db)
@@ -125,23 +136,30 @@ async fn project<'r>(mut db: Connection<Db>, _user: User, projectid: u64) -> Res
let project = sqlx::query!(
"SELECT id,title,description FROM projects WHERE id=?",
- projectid)
- .fetch_one(&mut **db)
- .map_ok(|r| api_model::Project {
- id: r.id,
- title: r.title,
- description: r.description,
- members: members,
- })
- .await
- .map_err(|e| NotFound(e.to_string()))
- .unwrap();
+ projectid
+ )
+ .fetch_one(&mut **db)
+ .map_ok(|r| api_model::Project {
+ id: r.id,
+ title: r.title,
+ description: r.description,
+ members: members,
+ })
+ .await
+ .map_err(|e| NotFound(e.to_string()))
+ .unwrap();
Ok(Json(project))
}
#[get("/reviews/<projectid>?<limit>&<offset>")]
-async fn reviews<'r>(mut db: Connection<Db>, _user: User, projectid: u64, limit: Option<u32>, offset: Option<u32>) -> Json<api_model::Reviews> {
+async fn reviews<'r>(
+ mut db: Connection<Db>,
+ _user: User,
+ projectid: u64,
+ limit: Option<u32>,
+ offset: Option<u32>,
+) -> Json<api_model::Reviews> {
let uw_offset = offset.unwrap_or(0);
let uw_limit = limit.unwrap_or(10);
let entries = sqlx::query!(
@@ -166,27 +184,31 @@ async fn reviews<'r>(mut db: Connection<Db>, _user: User, projectid: u64, limit:
let count = sqlx::query!(
"SELECT COUNT(id) AS count FROM reviews WHERE project=?",
- projectid)
- .fetch_one(&mut **db)
- .map_ok(|r| r.count)
- .await
- .unwrap();
+ projectid
+ )
+ .fetch_one(&mut **db)
+ .map_ok(|r| r.count)
+ .await
+ .unwrap();
let u32_count = u32::try_from(count).unwrap();
- Json(
- api_model::Reviews {
- offset: uw_offset,
- limit: uw_limit,
- total_count: u32_count,
- more: uw_offset + uw_limit < u32_count,
- reviews: entries,
- },
- )
+ Json(api_model::Reviews {
+ offset: uw_offset,
+ limit: uw_limit,
+ total_count: u32_count,
+ more: uw_offset + uw_limit < u32_count,
+ reviews: entries,
+ })
}
#[get("/review/<projectid>/<reviewid>")]
-async fn review<'r>(mut db: Connection<Db>, _user: User, projectid: u64, reviewid: u64) -> Result<Json<api_model::Review>, NotFound<String>> {
+async fn review<'r>(
+ mut db: Connection<Db>,
+ _user: User,
+ projectid: u64,
+ reviewid: u64,
+) -> Result<Json<api_model::Review>, NotFound<String>> {
let mut users = sqlx::query!(
"SELECT id,username,name,active,review_users.role AS role FROM users JOIN review_users ON review_users.user=id WHERE review_users.review=? ORDER BY role,username,id",
reviewid)
@@ -204,7 +226,8 @@ async fn review<'r>(mut db: Connection<Db>, _user: User, projectid: u64, reviewi
.await
.unwrap();
- let first_reviewer = users.iter()
+ let first_reviewer = users
+ .iter()
.position(|u| matches!(u.role, Role::Reviewer))
.unwrap_or(users.len());
let mut reviewers: Vec<api_model::User> = Vec::with_capacity(first_reviewer);
@@ -250,7 +273,7 @@ async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result {
error!("Failed to initialize database: {}", e);
Err(rocket)
}
- }
+ },
None => Err(rocket),
}
}