-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from MiSArch/user-specific-queries
User specific queries
- Loading branch information
Showing
6 changed files
with
81 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use async_graphql::{ComplexObject, Context, Error, Result, SimpleObject}; | ||
use bson::{doc, Document, Uuid}; | ||
use mongodb::{options::FindOptions, Collection, Database}; | ||
use mongodb_cursor_pagination::{error::CursorError, FindResult, PaginatedCursor}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::{ | ||
base_connection::{BaseConnection, FindResultWrapper}, | ||
order_datatypes::CommonOrderInput, | ||
wishlist::Wishlist, | ||
wishlist_connection::WishlistConnection, | ||
}; | ||
|
||
/// Type of a user owning wishlists. | ||
#[derive(Debug, Serialize, Deserialize, Hash, Eq, PartialEq, Clone, SimpleObject)] | ||
#[graphql(complex)] | ||
pub struct User { | ||
/// UUID of the user. | ||
pub _id: Uuid, | ||
} | ||
|
||
#[ComplexObject] | ||
impl User { | ||
/// Retrieves wishlists of user. | ||
async fn wishlists<'a>( | ||
&self, | ||
ctx: &Context<'a>, | ||
#[graphql(desc = "Describes that the `first` N wishlists should be retrieved.")] | ||
first: Option<u32>, | ||
#[graphql(desc = "Describes how many wishlists should be skipped at the beginning.")] | ||
skip: Option<u64>, | ||
#[graphql(desc = "Specifies the order in which wishlists are retrieved.")] order_by: Option< | ||
CommonOrderInput, | ||
>, | ||
) -> Result<WishlistConnection> { | ||
let db_client = ctx.data_unchecked::<Database>(); | ||
let collection: Collection<Wishlist> = db_client.collection::<Wishlist>("wishlists"); | ||
let wishlist_order = order_by.unwrap_or_default(); | ||
let sorting_doc = doc! {wishlist_order.field.unwrap_or_default().as_str(): i32::from(wishlist_order.direction.unwrap_or_default())}; | ||
let find_options = FindOptions::builder() | ||
.skip(skip) | ||
.limit(first.map(|v| i64::from(v))) | ||
.sort(sorting_doc) | ||
.build(); | ||
let document_collection = collection.clone_with_type::<Document>(); | ||
let filter = doc! {"user._id": self._id}; | ||
let maybe_find_results: Result<FindResult<Wishlist>, CursorError> = | ||
PaginatedCursor::new(Some(find_options.clone()), None, None) | ||
.find(&document_collection, Some(&filter)) | ||
.await; | ||
match maybe_find_results { | ||
Ok(find_results) => { | ||
let find_result_wrapper = FindResultWrapper(find_results); | ||
let connection = Into::<BaseConnection<Wishlist>>::into(find_result_wrapper); | ||
Ok(Into::<WishlistConnection>::into(connection)) | ||
} | ||
Err(_) => return Err(Error::new("Retrieving wishlists failed in MongoDB.")), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters