Skip to content

Commit

Permalink
get poll route
Browse files Browse the repository at this point in the history
  • Loading branch information
brunodsousa committed Feb 10, 2024
1 parent a7ade20 commit 948f363
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/http/routes/get-poll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import z from "zod";
import { prisma } from "../../lib/prisma";

export async function getPoll(app: FastifyInstance) {
app.get(
"/polls/:pollId",
async (request: FastifyRequest, reply: FastifyReply) => {
const getPollParams = z.object({
pollId: z.string().uuid(),
});

const { pollId } = getPollParams.parse(request.params);

const poll = await prisma.poll.findUnique({
where: {
id: pollId,
},
include: {
options: {
select: {
id: true,
title: true,
},
},
},
});

return reply.send({ poll });
}
);
}
2 changes: 2 additions & 0 deletions src/http/server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import fastify, { FastifyReply, FastifyRequest } from "fastify";
import { createPoll } from "./routes/create-poll";
import { getPoll } from "./routes/get-poll";

const app = fastify();

app.register(createPoll);
app.register(getPoll);

app.listen({ port: 3333 }).then(() => {
console.log("HTTP server running!");
Expand Down

0 comments on commit 948f363

Please sign in to comment.