summaryrefslogtreecommitdiff
path: root/server/src/main.rs
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2025-01-03 01:28:54 +0100
committerJoel Klinghed <the_jk@spawned.biz>2025-01-03 01:28:54 +0100
commit7494db93b9262c3d8330fd11631e711a1642b8fc (patch)
tree565534ddaa990a861c0ef8a9439f7656fce7f132 /server/src/main.rs
parent4b1f7fec1cf9d427234ff5bded79a6d18d5c88ce (diff)
Add initital tests
Also add /users endpoint.
Diffstat (limited to 'server/src/main.rs')
-rw-r--r--server/src/main.rs83
1 files changed, 71 insertions, 12 deletions
diff --git a/server/src/main.rs b/server/src/main.rs
index 54ad279..53cdb89 100644
--- a/server/src/main.rs
+++ b/server/src/main.rs
@@ -3,6 +3,7 @@ extern crate rocket;
use futures::{future::TryFutureExt, stream::TryStreamExt};
use rocket::fairing::{self, AdHoc};
+use rocket::figment::Figment;
use rocket::http::Status;
use rocket::response::status::{Custom, NotFound};
use rocket::serde::json::Json;
@@ -12,6 +13,9 @@ use sqlx::Acquire;
use utoipa::OpenApi;
use utoipa_swagger_ui::SwaggerUi;
+#[cfg(test)]
+mod tests;
+
mod api_model;
mod auth;
mod db_utils;
@@ -34,6 +38,7 @@ struct Db(sqlx::MySqlPool);
project_user_del,
reviews,
review,
+ users,
),
modifiers(&AuthApiAddon),
)]
@@ -574,6 +579,56 @@ async fn review(
Ok(Json(review))
}
+#[utoipa::path(
+ responses(
+ (status = 200, description = "Get all users", body = api_model::Users),
+ ),
+ security(
+ ("session" = []),
+ ),
+)]
+#[get("/users?<limit>&<offset>")]
+async fn users(
+ mut db: Connection<Db>,
+ _session: auth::Session,
+ limit: Option<u32>,
+ offset: Option<u32>,
+) -> Json<api_model::Users> {
+ let uw_offset = offset.unwrap_or(0);
+ let uw_limit = limit.unwrap_or(10);
+ let entries = sqlx::query!(
+ "SELECT id,username,name,active FROM users ORDER BY username LIMIT ? OFFSET ?",
+ uw_limit,
+ uw_offset
+ )
+ .fetch(&mut **db)
+ .map_ok(|r| api_model::User {
+ id: r.id,
+ username: r.username,
+ name: r.name,
+ active: r.active != 0,
+ })
+ .try_collect::<Vec<_>>()
+ .await
+ .unwrap();
+
+ let count = sqlx::query!("SELECT COUNT(id) AS count FROM users")
+ .fetch_one(&mut **db)
+ .map_ok(|r| r.count)
+ .await
+ .unwrap();
+
+ let u32_count = u32::try_from(count).unwrap();
+
+ Json(api_model::Users {
+ offset: uw_offset,
+ limit: uw_limit,
+ total_count: u32_count,
+ more: uw_offset + uw_limit < u32_count,
+ users: entries,
+ })
+}
+
async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result {
match Db::fetch(&rocket) {
Some(db) => match sqlx::migrate!().run(&**db).await {
@@ -587,17 +642,9 @@ async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result {
}
}
-#[rocket::main]
-async fn main() -> Result<(), rocket::Error> {
+fn rocket_from_config(figment: Figment) -> Rocket<Build> {
let basepath = "/api/v1";
-
- let mut api = MainApi::openapi();
- api.merge(auth::AuthApi::openapi());
- api.servers = Some(vec![utoipa::openapi::ServerBuilder::new()
- .url(basepath)
- .build()]);
-
- let _rocket = rocket::build()
+ rocket::custom(figment)
.attach(Db::init())
.attach(AdHoc::try_on_ignite("Database Migrations", run_migrations))
.mount(
@@ -612,14 +659,26 @@ async fn main() -> Result<(), rocket::Error> {
project_user_update,
project_user_del,
reviews,
- review
+ review,
+ users,
],
)
+ .attach(auth::stage(basepath))
+}
+
+#[rocket::main]
+async fn main() -> Result<(), rocket::Error> {
+ let mut api = MainApi::openapi();
+ api.merge(auth::AuthApi::openapi());
+ api.servers = Some(vec![utoipa::openapi::ServerBuilder::new()
+ .url("/api/v1")
+ .build()]);
+
+ let _rocket = rocket_from_config(rocket::Config::figment())
.mount(
"/",
SwaggerUi::new("/openapi/ui/<_..>").url("/openapi/openapi.json", api),
)
- .attach(auth::stage(basepath))
.launch()
.await?;