Skip to content

Commit

Permalink
feat(api): add kit members endpoint, return user object in KitMembership
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcur committed Jan 10, 2024
1 parent 719fa7d commit 7bcba3a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
1 change: 1 addition & 0 deletions astroplant-api/src/bin/astroplant-api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ async fn main() -> anyhow::Result<()> {
.route("/kits/:kit_serial", get(kit::kit_by_serial))
.route("/kits/:kit_serial", patch(kit::patch_kit))
.route("/kits/:kit_serial", delete(kit::delete_kit))
.route("/kits/:kit_serial/members", get(kit::get_members))
.route("/kits/:kit_serial/password", post(kit::reset_password))
.route(
"/kits/:kit_serial/configurations",
Expand Down
43 changes: 43 additions & 0 deletions astroplant-api/src/controllers/kit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use axum::Extension;
use serde::{Deserialize, Serialize};

use crate::database::PgPool;
use crate::models::{KitMembership, User};
use crate::problem::{self, Problem};
use crate::response::{Response, ResponseBuilder};
use crate::utils::deserialize_some;
Expand Down Expand Up @@ -245,3 +246,45 @@ pub async fn delete_kit(
})
.await
}

/// Handles the `GET /kits/{kitSerial}/members` route.
pub async fn get_members(
Extension(pg): Extension<PgPool>,
Path(kit_serial): Path<String>,
user_id: Option<models::UserId>,
) -> Result<Response, Problem> {
let (_, _, kit) = helpers::fut_kit_permission_or_forbidden(
pg.clone(),
user_id,
kit_serial,
crate::authorization::KitAction::View,
)
.await?;

let conn = pg.get().await?;

let kit_id = kit.id;
let members: Vec<(User, KitMembership)> = conn
.interact_flatten_err(move |conn| {
use diesel::prelude::*;
use schema::kit_memberships;
use schema::users;

users::table
.inner_join(kit_memberships::table)
.filter(kit_memberships::kit_id.eq(kit_id))
.get_results(conn)
})
.await?;

let v: Vec<_> = members
.into_iter()
.map(|(user, membership)| {
views::KitMembership::from(membership)
.with_kit(views::Kit::from(kit.clone()))
.with_user(views::User::from(user))
})
.collect();

Ok(ResponseBuilder::ok().body(v))
}
10 changes: 4 additions & 6 deletions astroplant-api/src/controllers/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,18 @@ pub async fn list_kit_memberships(
)
.await?;

let username = user.username.clone();
let user_id = user.get_id();
let conn = pg.get().await?;
let kit_memberships = conn
.interact(move |conn| {
models::KitMembership::memberships_with_kit_of_user_id(conn, user.get_id())
})
.interact(move |conn| models::KitMembership::memberships_with_kit_of_user_id(conn, user_id))
.await??;

let v: Vec<views::KitMembership<String, views::Kit>> = kit_memberships
let v: Vec<views::KitMembership<views::User, views::Kit>> = kit_memberships
.into_iter()
.map(|(kit, membership)| {
views::KitMembership::from(membership)
.with_kit(views::Kit::from(kit))
.with_user(username.clone())
.with_user(views::User::from(user.clone()))
})
.collect();
Ok(ResponseBuilder::ok().body(v))
Expand Down
30 changes: 29 additions & 1 deletion openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,34 @@ paths:
$ref: "#/components/responses/ErrorRateLimit"
'500':
$ref: "#/components/responses/ErrorInternalServer"
"/kits/{kitSerial}/members":
get:
summary: Members of a specific kit.
operationId: getKitMembers
security:
- bearerAuth: []
tags:
- kits
parameters:
- name: kitSerial
in: path
required: true
description: The serial of the kit to get the members of.
schema:
type: string
responses:
'200':
description: The kit's members.
content:
application/json:
schema:
$ref: "#/components/schemas/KitMembership"
'401':
$ref: "#/components/responses/ErrorUnauthorized"
'429':
$ref: "#/components/responses/ErrorRateLimit"
'500':
$ref: "#/components/responses/ErrorInternalServer"
"/kits/{kitSerial}/aggregate-measurements":
get:
summary: Aggregate measurements made by a kit.
Expand Down Expand Up @@ -1274,7 +1302,7 @@ components:
type: integer
format: int32
user:
type: string
$ref: "#/components/schemas/User"
kit:
$ref: "#/components/schemas/Kit"
accessConfigure:
Expand Down

0 comments on commit 7bcba3a

Please sign in to comment.