Skip to content

Commit

Permalink
👽️ Test api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
KONFeature committed Jul 25, 2024
1 parent 5012451 commit 59663f6
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 11 deletions.
28 changes: 17 additions & 11 deletions packages/ponder/ponder.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,31 @@ export default createSchema((p) => ({
detachTimestamp: p.bigint().optional(),

capResets: p.many("CampaignCapReset.campaignId"),
stats: p.many("PressCampaignStats.campaignId"),
},
{
contentIndex: p.index("contentId"),
}
),
PressCampaignStats: p.createTable({
id: p.hex(),
PressCampaignStats: p.createTable(
{
id: p.hex(),

campaignId: p.hex().references("Campaign.id"),
campaign: p.one("campaignId"),
campaignId: p.hex().references("Campaign.id"),
campaign: p.one("campaignId"),

totalInteractions: p.bigint(),
openInteractions: p.bigint(),
readInteractions: p.bigint(),
referredInteractions: p.bigint(),
createReferredLinkInteractions: p.bigint(),
totalInteractions: p.bigint(),
openInteractions: p.bigint(),
readInteractions: p.bigint(),
referredInteractions: p.bigint(),
createReferredLinkInteractions: p.bigint(),

totalRewards: p.bigint(),
}),
totalRewards: p.bigint(),
},
{
campaignIndex: p.index("campaignId"),
}
),
CampaignCapReset: p.createTable(
{
id: p.string(), // campaign address + timestamp
Expand Down
73 changes: 73 additions & 0 deletions packages/ponder/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { ponder } from "@/generated";
import { graphql } from "@ponder/core";
import { eq } from "@ponder/core";
import type { Address } from "viem";

// This is the entry point for the Graphql api
ponder.use("/graphql", graphql());

/* -------------------------------------------------------------------------- */
/* Rest campaign APIs */
/* -------------------------------------------------------------------------- */

// Get all the campaign for a wallet
ponder.use("/admin/:wallet/campaigns", async (ctx) => {
// Extract wallet
const wallet = ctx.req.param("wallet");

// Get the tables we will query
const { ContentAdministrator, Campaign } = ctx.tables;

// Perform the sql query
const campaigns = await ctx.db
.select({
contentId: ContentAdministrator.contentId,
isContentOwner: ContentAdministrator.isOwner,
id: Campaign.id,
name: Campaign.name,
version: Campaign.version,
attached: Campaign.attached,
attachTimestamp: Campaign.attachTimestamp,
detachTimestamp: Campaign.detachTimestamp,
})
.from(ContentAdministrator)
.innerJoin(Campaign, eq(ContentAdministrator.contentId, Campaign.id))
.where(eq(ContentAdministrator.user, wallet as Address));

// Return the result as json
return ctx.json(campaigns);
});

// Get all the campaign stats for a wallet
ponder.use("/admin/:wallet/campaigns/stats", async (ctx) => {
// Extract wallet
const wallet = ctx.req.param("wallet");

// Get the tables we will query
const { ContentAdministrator, Campaign, PressCampaignStats } = ctx.tables;

// Perform the sql query
const campaignsStats = await ctx.db
.select({
contentId: ContentAdministrator.contentId,
isContentOwner: ContentAdministrator.isOwner,
id: Campaign.id,
totalInteractions: PressCampaignStats.totalInteractions,
openInteractions: PressCampaignStats.openInteractions,
readInteractions: PressCampaignStats.readInteractions,
referredInteractions: PressCampaignStats.referredInteractions,
createReferredLinkInteractions:
PressCampaignStats.createReferredLinkInteractions,
totalRewards: PressCampaignStats.totalRewards,
})
.from(ContentAdministrator)
.innerJoin(Campaign, eq(ContentAdministrator.contentId, Campaign.id))
.innerJoin(
PressCampaignStats,
eq(Campaign.contentId, PressCampaignStats.campaignId)
)
.where(eq(ContentAdministrator.user, wallet as Address));

// Return the result as json
return ctx.json(campaignsStats);
});

0 comments on commit 59663f6

Please sign in to comment.