Skip to content

Commit

Permalink
Merge pull request #182 from starknet-id/feat/filter-done-quests-in-t…
Browse files Browse the repository at this point in the history
…rending

feat: filter done quests in trending
  • Loading branch information
Th0rgal authored Jan 22, 2024
2 parents a370ca5 + b697ba3 commit 75f1177
Showing 1 changed file with 77 additions and 10 deletions.
87 changes: 77 additions & 10 deletions src/endpoints/get_trending_quests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,32 @@ use crate::{
utils::get_error,
};
use axum::{
extract::State,
http::StatusCode,
response::{IntoResponse, Json},
extract::{Query, State},
response::IntoResponse,
Json,
};
use futures::StreamExt;
use mongodb::bson::doc;
use mongodb::bson::from_document;
use mongodb::bson::{doc, from_document};
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use starknet::core::types::FieldElement;
use std::sync::Arc;

#[derive(Debug, Serialize, Deserialize)]
pub struct NFTItem {
img: String,
level: u32,
pub struct GetTrendingQuestsQuery {
addr: Option<FieldElement>,
}

pub async fn handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
let pipeline = vec![
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<GetTrendingQuestsQuery>,
) -> impl IntoResponse {
// Addr might not exist
let address = match query.addr {
Some(addr) => addr.to_string(),
None => "".to_string(),
};
let mut pipeline = vec![
doc! {
"$match": {
"disabled": false,
Expand Down Expand Up @@ -51,6 +59,65 @@ pub async fn handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
},
];

// If address is provided, filter out quests that the user has already completed
if !address.is_empty() {
pipeline.extend_from_slice(&[
doc! {
"$lookup": {
"from": "tasks",
"localField": "id",
"foreignField": "quest_id",
"as": "tasks"
}
},
doc! {
"$lookup": doc! {
"from": "completed_tasks",
"let": {
"task_ids": {
"$map": {
"input": "$tasks",
"as": "taskObj",
"in": "$$taskObj.id" // Extract the id from each object in the tasks array
}
}
},
"pipeline" : [
{
"$match": {
"$expr": {
"$and": [
{
"$in": ["$task_id", "$$task_ids"],
},
{
"$eq": ["$address", address],
}
]
}
}
}
],
"as": "completed_tasks"
}
},
doc! {
"$match": {
"$expr": {
"$ne": [
{
"$size": "$tasks",
},
{
"$size": "$completed_tasks",
},
],
},
}
},
]);
}

let collection = state.db.collection::<QuestDocument>("quests");
match collection.aggregate(pipeline, None).await {
Ok(mut cursor) => {
Expand Down

0 comments on commit 75f1177

Please sign in to comment.