From 8715c70d7e9bf0d827a6dfd1abe30e563147fd65 Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Sun, 28 Nov 2021 16:23:04 -0600 Subject: [PATCH] Count up project votes and some more API endpoints --- functions/graphql/resolvers.js | 51 +++++++++++++++++++++++++++++++--- functions/graphql/typeDefs.js | 7 +++-- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/functions/graphql/resolvers.js b/functions/graphql/resolvers.js index aebf446..e9a2f3d 100644 --- a/functions/graphql/resolvers.js +++ b/functions/graphql/resolvers.js @@ -42,14 +42,47 @@ module.exports = { } }); }, - allProjects: async (_source, args, context) => { - const first = args.first || 50; + getCategory: async (_source, args, context) => { + return context.prisma.category.findUnique({ + where: { id: args.id }, + include: { + project: { + take: 5, + orderBy: { votes_count: "desc" } + } + } + }); + }, + newProjects: async (_source, args, context) => { + const take = args.take || 50; const skip = args.skip || 0; return context.prisma.project.findMany({ orderBy: { created_at: 'desc' }, include: { category: true }, skip, - first, + take, + }); + }, + allProjects: async (_source, args, context) => { + const take = args.take || 50; + const skip = args.skip || 0; + return context.prisma.project.findMany({ + orderBy: { votes_count: 'desc' }, + include: { category: true }, + skip, + take, + }); + }, + projectsByCategory: async (_source, args, context) => { + const take = args.take || 50; + const skip = args.skip || 0; + const categoryId = args.category_id; + return context.prisma.project.findMany({ + where: { category_id: categoryId }, + orderBy: { votes_count: 'desc' }, + include: { category: true }, + skip, + take, }); }, getProject: async (_source, args, context) => { @@ -80,7 +113,17 @@ module.exports = { // look for a vote for the payment request and the calculated payment hash const vote = await context.prisma.vote.findFirst({where: { payment_request: args.payment_request, payment_hash: paymentHash}}); // if we find a vote it means the preimage is correct and we update the vote and mark it as paid + // can we write this nicer? if (vote) { + const project = await context.prisma.project.findUnique({ where: { id: vote.project_id }}); + // count up votes cache + await context.prisma.project.update({ + where: {id: project.id }, + data: { + votes_count: project.votes_count = vote.amount_in_sat, + } + }); + // return the current vote return context.prisma.vote.update({ where: { id: vote.id }, data: { @@ -93,4 +136,4 @@ module.exports = { } } }, -}; \ No newline at end of file +}; diff --git a/functions/graphql/typeDefs.js b/functions/graphql/typeDefs.js index 7955391..c96f23b 100644 --- a/functions/graphql/typeDefs.js +++ b/functions/graphql/typeDefs.js @@ -28,9 +28,12 @@ module.exports = gql` } type Query { - allProjects(skip: Int!, first: Int!): [Project]! - getProject(id: Int!): Project + allProjects(skip: Int, take: Int): [Project]! + newProjects(skip: Int, take: Int): [Project]! + projectsByCategory(category_id: Int!, skip: Int, take: Int): [Project]! + getProject(id: Int!): Project! allCategories: [Category]! + getCategory(id: Int!): Category! } type Mutation { vote (project_id: Int!, amount_in_sat: Int!): Vote!