From 8ae6bd6799b31ca35dbdafda3882eb818b25dfb4 Mon Sep 17 00:00:00 2001 From: zhengow <313407040@qq.com> Date: Fri, 12 Aug 2022 20:30:42 +0800 Subject: [PATCH] feat: topOffers resolver --- src/server-extension/model/topOffers.model.ts | 30 +++++++++++++++++++ src/server-extension/query/topOffers.ts | 13 ++++++++ src/server-extension/resolvers/index.ts | 5 +++- .../resolvers/topOffersResolver.ts | 21 +++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/server-extension/model/topOffers.model.ts create mode 100644 src/server-extension/query/topOffers.ts create mode 100644 src/server-extension/resolvers/topOffersResolver.ts diff --git a/src/server-extension/model/topOffers.model.ts b/src/server-extension/model/topOffers.model.ts new file mode 100644 index 0000000..13a101c --- /dev/null +++ b/src/server-extension/model/topOffers.model.ts @@ -0,0 +1,30 @@ +import { Field, ObjectType } from 'type-graphql'; + +@ObjectType() +export class TopOffers { + @Field(() => String, { nullable: false }) + id!: string; + + @Field(() => String, { nullable: true}) + name!: string; + + @Field(() => String, { nullable: false, name: 'collectionId' }) + collection_id!: string; + + @Field(() => String, { nullable: true, name: 'collectionName' }) + collection_name!: string; + + @Field(() => Number, { nullable: false, name: 'totalCount' }) + total_count!: number; + + @Field(() => Number, { nullable: false, name: 'totalAmount' }) + total_amount!: number; + + @Field(() => Date, { nullable: false }) + latest_offer_timestamp!: Date; + + // constructor + constructor(props: Partial) { + Object.assign(this, props); + } +} diff --git a/src/server-extension/query/topOffers.ts b/src/server-extension/query/topOffers.ts new file mode 100644 index 0000000..538174b --- /dev/null +++ b/src/server-extension/query/topOffers.ts @@ -0,0 +1,13 @@ +export const topOffers = `SELECT + ne.id as id, + ne.name as name, + ce.name as collection_name, + ce.id as collection_id, + o.total_amount as total_amount, + o.total_count as total_count, + o.latest_offer_timestamp as latest_offer_timestamp +FROM (select nft_id, count(id) as total_count, sum(price) as total_amount, max(created_at) as latest_offer_timestamp from offer GROUP BY nft_id) o + LEFT JOIN nft_entity ne on o.nft_id = ne.id + LEFT JOIN collection_entity ce on ne.collection_id = ce.id +; +`; diff --git a/src/server-extension/resolvers/index.ts b/src/server-extension/resolvers/index.ts index 10ecc55..b6507f6 100644 --- a/src/server-extension/resolvers/index.ts +++ b/src/server-extension/resolvers/index.ts @@ -1,5 +1,8 @@ import { WalletResolver } from './walletResolver'; import { OfferStatsResolver } from './offerStatsResolver'; +import { TopOffersResolver } from './topOffersResolver'; import { EventResolver } from './event'; -export { WalletResolver, OfferStatsResolver, EventResolver }; +export { + WalletResolver, OfferStatsResolver, EventResolver, TopOffersResolver, +}; diff --git a/src/server-extension/resolvers/topOffersResolver.ts b/src/server-extension/resolvers/topOffersResolver.ts new file mode 100644 index 0000000..a9531fd --- /dev/null +++ b/src/server-extension/resolvers/topOffersResolver.ts @@ -0,0 +1,21 @@ +import { Query, Resolver } from 'type-graphql'; +import type { EntityManager } from 'typeorm'; +import { Event } from '../../model'; +import { makeQuery } from '../utils'; +import { topOffers } from '../query/topOffers'; +import { TopOffers } from '../model/topOffers.model'; + +@Resolver() +export class TopOffersResolver { + constructor(private tx: () => Promise) {} + + @Query(() => [TopOffers]) + async topOffers(): Promise { + const result: TopOffers[] = await makeQuery( + this.tx, + Event, + topOffers, + ); + return result; + } +}