Skip to content

Commit

Permalink
open drop queries
Browse files Browse the repository at this point in the history
  • Loading branch information
imabdulbasit committed Sep 15, 2023
1 parent 9f3100a commit fd86a9c
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 6 deletions.
47 changes: 45 additions & 2 deletions api/src/dataloaders/collection_mints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ use std::{borrow::Cow, collections::HashMap};

use async_graphql::{dataloader::Loader as DataLoader, FieldError, Result};
use poem::async_trait;
use sea_orm::prelude::*;
use sea_orm::{prelude::*, JoinType, QuerySelect};

use crate::{db::Connection, entities::collection_mints, objects::CollectionMint};
use crate::{
db::Connection,
entities::{collection_mints, collections, drops, sea_orm_active_enums::CreationStatus},
objects::CollectionMint,
};

#[derive(Debug, Clone)]
pub struct Loader {
Expand All @@ -28,6 +32,7 @@ impl DataLoader<Uuid> for Loader {
.filter(
collection_mints::Column::CollectionId.is_in(keys.iter().map(ToOwned::to_owned)),
)
.filter(collection_mints::Column::CreationStatus.ne(CreationStatus::Queued))
.all(self.db.get())
.await?;

Expand Down Expand Up @@ -113,3 +118,41 @@ impl DataLoader<Uuid> for CollectionMintLoader {
.collect())
}
}

/// Dataloader for queued mints for a drop
#[derive(Debug, Clone)]
pub struct QueuedMintsLoader {
pub db: Connection,
}

impl QueuedMintsLoader {
#[must_use]
pub fn new(db: Connection) -> Self {
Self { db }
}
}

#[async_trait]
impl DataLoader<Uuid> for QueuedMintsLoader {
type Error = FieldError;
type Value = Vec<CollectionMint>;

async fn load(&self, keys: &[Uuid]) -> Result<HashMap<Uuid, Self::Value>, Self::Error> {
let drop_mints = drops::Entity::find()
.select_with(collection_mints::Entity)
.join(
JoinType::InnerJoin,
collection_mints::Relation::Collections.def(),
)
.join(JoinType::InnerJoin, collections::Relation::Drop.def())
.filter(collection_mints::Column::CreationStatus.eq(CreationStatus::Queued))
.filter(drops::Column::Id.is_in(keys.iter().map(ToOwned::to_owned)))
.all(self.db.get())
.await?;

Ok(drop_mints
.into_iter()
.map(|(drop, mints)| (drop.id, mints.into_iter().map(Into::into).collect()))
.collect())
}
}
2 changes: 1 addition & 1 deletion api/src/dataloaders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub use collection::Loader as CollectionLoader;
pub use collection_drop::Loader as CollectionDropLoader;
pub use collection_mints::{
CollectionMintLoader, Loader as CollectionMintsLoader,
OwnerLoader as CollectionMintsOwnerLoader,
OwnerLoader as CollectionMintsOwnerLoader, QueuedMintsLoader,
};
pub use creators::Loader as CreatorsLoader;
pub use drop::Loader as DropLoader;
Expand Down
5 changes: 4 additions & 1 deletion api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use dataloaders::{
CollectionMintMintHistoryLoader, CollectionMintTransfersLoader, CollectionMintsLoader,
CollectionMintsOwnerLoader, CreatorsLoader, DropLoader, DropMintHistoryLoader, HoldersLoader,
MetadataJsonAttributesLoader, MetadataJsonLoader, MintCreatorsLoader, MinterMintHistoryLoader,
ProjectCollectionLoader, ProjectCollectionsLoader, ProjectDropsLoader,
ProjectCollectionLoader, ProjectCollectionsLoader, ProjectDropsLoader, QueuedMintsLoader,
SwitchCollectionHistoryLoader, UpdateMintHistoryLoader,
};
use db::Connection;
Expand Down Expand Up @@ -291,6 +291,7 @@ pub struct AppContext {
collection_mint_mint_history_loader: DataLoader<CollectionMintMintHistoryLoader>,
collection_mint_transfers_loader: DataLoader<CollectionMintTransfersLoader>,
switch_collection_history_loader: DataLoader<SwitchCollectionHistoryLoader>,
queued_mints_loader: DataLoader<QueuedMintsLoader>,
}

impl AppContext {
Expand Down Expand Up @@ -342,6 +343,7 @@ impl AppContext {
DataLoader::new(CollectionMintTransfersLoader::new(db.clone()), tokio::spawn);
let switch_collection_history_loader =
DataLoader::new(SwitchCollectionHistoryLoader::new(db.clone()), tokio::spawn);
let queued_mints_loader = DataLoader::new(QueuedMintsLoader::new(db.clone()), tokio::spawn);

Self {
db,
Expand Down Expand Up @@ -369,6 +371,7 @@ impl AppContext {
collection_mint_mint_history_loader,
collection_mint_transfers_loader,
switch_collection_history_loader,
queued_mints_loader,
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions api/src/objects/drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use async_graphql::{Context, Enum, Error, Object, Result};
use hub_core::chrono::Utc;
use sea_orm::entity::prelude::*;

use super::Collection;
use super::{Collection, CollectionMint};
use crate::{
entities::{collections, drops, mint_histories, sea_orm_active_enums::CreationStatus},
AppContext,
Expand Down Expand Up @@ -88,10 +88,11 @@ impl Drop {
let paused_at = self.drop.paused_at;
let shutdown_at = self.drop.shutdown_at;

let total_mints = self.collection.total_mints;
let minted = self
.collection
.supply
.map(|supply| supply == self.collection.total_mints);
.map(|supply| supply == total_mints && total_mints > 0);

match (
scheduled,
Expand Down Expand Up @@ -125,6 +126,15 @@ impl Drop {
}
}

async fn queued_mints(&self, ctx: &Context<'_>) -> Result<Option<Vec<CollectionMint>>> {
let AppContext {
queued_mints_loader,
..
} = ctx.data::<AppContext>()?;

queued_mints_loader.load_one(self.drop.id).await
}

#[graphql(deprecation = "Use `mint_histories` under `Collection` Object instead.")]
/// A list of all NFT purchases from this drop.
async fn purchases(&self, ctx: &Context<'_>) -> Result<Option<Vec<mint_histories::Model>>> {
Expand Down
2 changes: 2 additions & 0 deletions migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ mod m20230905_100852_add_type_to_drop;
mod m20230910_204731_add_queued_variant_to_mints_status;
mod m20230910_212742_make_owner_address_optional_for_mint;
mod m20230911_144938_make_compressed_column_optional;
mod m20230915_111128_create_mints_creation_status_idx;

pub struct Migrator;

Expand Down Expand Up @@ -121,6 +122,7 @@ impl MigratorTrait for Migrator {
Box::new(m20230910_204731_add_queued_variant_to_mints_status::Migration),
Box::new(m20230910_212742_make_owner_address_optional_for_mint::Migration),
Box::new(m20230911_144938_make_compressed_column_optional::Migration),
Box::new(m20230915_111128_create_mints_creation_status_idx::Migration),
]
}
}
26 changes: 26 additions & 0 deletions migration/src/m20230915_111128_create_mints_creation_status_idx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use sea_orm_migration::prelude::*;

use crate::m20230220_223223_create_collection_mints_table::CollectionMints;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_index(
IndexCreateStatement::new()
.name("collection-mints-creation-status-idx")
.table(CollectionMints::Table)
.col(CollectionMints::CreationStatus)
.index_type(IndexType::BTree)
.to_owned(),
)
.await
}

async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> {
Ok(())
}
}

0 comments on commit fd86a9c

Please sign in to comment.