diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 7f9db373..c74565b6 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -2740,6 +2740,7 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" name = "pin_packing" version = "0.1.0" dependencies = [ + "anyhow", "async-graphql", "axum", "chrono", diff --git a/backend/pin_packing/Cargo.toml b/backend/pin_packing/Cargo.toml index 2f0e3ae0..8d80bfb0 100644 --- a/backend/pin_packing/Cargo.toml +++ b/backend/pin_packing/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +anyhow = { workspace = true } async-graphql = { workspace = true } axum = { workspace = true } clap = { workspace = true } diff --git a/backend/pin_packing/src/resolvers/pin_library.rs b/backend/pin_packing/src/resolvers/pin_library.rs index 4658916a..5be30b22 100644 --- a/backend/pin_packing/src/resolvers/pin_library.rs +++ b/backend/pin_packing/src/resolvers/pin_library.rs @@ -2,9 +2,14 @@ use crate::tables::{ pin_library::{self, PinStatus}, pin_mount, }; -use async_graphql::{ComplexObject, Context, Object}; +use async_graphql::{ + connection::{query, Connection, Edge, EmptyFields}, + ComplexObject, Context, Object, +}; use opa_client::subject_authorization; -use sea_orm::{ActiveValue, DatabaseConnection, EntityTrait, IntoActiveModel, ModelTrait}; +use sea_orm::{ + ActiveValue, CursorTrait, DatabaseConnection, EntityTrait, IntoActiveModel, ModelTrait, +}; #[ComplexObject] impl pin_library::Model { @@ -23,10 +28,46 @@ impl PinLibraryQuery { async fn library_pins( &self, ctx: &Context<'_>, - ) -> async_graphql::Result> { + after: Option, + before: Option, + first: Option, + last: Option, + ) -> async_graphql::Result> + { subject_authorization!("xchemlab.pin_packing.read_pin_library", ctx).await?; let database = ctx.data::()?; - Ok(pin_library::Entity::find().all(database).await?) + let pin_query = pin_library::Entity::find(); + query( + after, + before, + first, + last, + |after, before, first, last| async move { + let mut pin_cursor = pin_query.cursor_by(pin_library::Column::Barcode); + if let Some(after) = after { + pin_cursor.after(after); + } + if let Some(before) = before { + pin_cursor.before(before); + } + if let Some(first) = first { + pin_cursor.first(first as u64); + } + if let Some(last) = last { + pin_cursor.last(last as u64); + } + + let pins = pin_cursor.all(database).await?; + + let mut connection = Connection::new(true, true); + connection.edges.extend( + pins.into_iter() + .map(|pin| Edge::new(pin.barcode.clone(), pin)), + ); + Ok::<_, async_graphql::Error>(connection) + }, + ) + .await } }