Skip to content

Commit

Permalink
create poll route
Browse files Browse the repository at this point in the history
adds options when creating poll
  • Loading branch information
brunodsousa committed Feb 10, 2024
1 parent de1ef64 commit a7ade20
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 21 deletions.
11 changes: 11 additions & 0 deletions prisma/migrations/20240210094848_create_poll_options/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- CreateTable
CREATE TABLE "PollOption" (
"id" TEXT NOT NULL,
"title" TEXT NOT NULL,
"pollId" TEXT NOT NULL,

CONSTRAINT "PollOption_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "PollOption" ADD CONSTRAINT "PollOption_pollId_fkey" FOREIGN KEY ("pollId") REFERENCES "Poll"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
10 changes: 10 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@ model Poll {
title String
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
options PollOption[]
}

model PollOption {
id String @id @default(uuid())
title String
pollId String
poll Poll @relation(fields: [pollId], references: [id])
}
33 changes: 33 additions & 0 deletions src/http/routes/create-poll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import z from "zod";
import { prisma } from "../../lib/prisma";

export async function createPoll(app: FastifyInstance) {
app.post("/polls", async (request: FastifyRequest, reply: FastifyReply) => {
const createPollBody = z.object({
title: z.string(),
options: z.array(z.string()),
});

const { title, options } = createPollBody.parse(request.body);

const poll = await prisma.poll.create({
data: {
title,
options: {
createMany: {
data: options.map((option) => {
return {
title: option,
};
}),
},
},
},
});

return reply.status(201).send({
pollId: poll.id,
});
});
}
23 changes: 2 additions & 21 deletions src/http/server.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
import fastify, { FastifyReply, FastifyRequest } from "fastify";
import { PrismaClient } from "@prisma/client";
import { z } from "zod";
import { createPoll } from "./routes/create-poll";

const app = fastify();

const prisma = new PrismaClient();

app.post("/polls", async (request: FastifyRequest, reply: FastifyReply) => {
const createPollBody = z.object({
title: z.string(),
});

const { title } = createPollBody.parse(request.body);

const poll = await prisma.poll.create({
data: {
title,
},
});

return reply.status(201).send({
pollId: poll.id,
});
});
app.register(createPoll);

app.listen({ port: 3333 }).then(() => {
console.log("HTTP server running!");
Expand Down
5 changes: 5 additions & 0 deletions src/lib/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { PrismaClient } from "@prisma/client";

export const prisma = new PrismaClient({
log: ["query"],
});

0 comments on commit a7ade20

Please sign in to comment.