-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
48 lines (40 loc) · 1.56 KB
/
plugin.js
File metadata and controls
48 lines (40 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/// <reference path="./global.d.ts" />
'use strict'
/** @param {import('fastify').FastifyInstance} app */
module.exports = async function (app) {
app.graphql.extendSchema(`
extend type Mutation {
savePollWithOptions(poll: PollInput!, pollOptions: [PollOptionInput!]!): Poll
vote(pollId: ID!, optionId: ID!): Int
}
`)
async function savePollWithOptions(poll, pollOptions) {
const newPoll = await app.platformatic.entities.poll.save({ input: poll })
if (newPoll) {
pollOptions.map(async pollOption => {
const pollOptionToSave = { pollId: newPoll.id, ...pollOption }
return await app.platformatic.entities.pollOption.save({ input: pollOptionToSave })
})
}
return newPoll
}
async function vote(pollId, optionId) {
const { db, sql } = app.platformatic
const result = await db.query(sql`
UPDATE poll_options SET vote_counts = vote_counts + 1 WHERE id=${optionId} AND poll_id=${pollId} RETURNING vote_counts
`)
return result[0]?.vote_counts
}
app.graphql.defineResolvers({
Mutation: {
savePollWithOptions: async (_, { poll, pollOptions }) => await savePollWithOptions(poll, pollOptions),
vote: async (_, {pollId, optionId}) => vote(pollId, optionId)
}
})
app.post('/polls/new', async function (request, response) {
return { poll: await savePollWithOptions(request.body.poll, request.body.pollOptions) }
})
app.patch('/polls/:id/vote', async function (request, response) {
return { voteCounts: await vote(request.params.id, request.body.optionId)}
})
}