From a7ade20e603c96f8799b70ab752433a568629116 Mon Sep 17 00:00:00 2001 From: Bruno Sousa Date: Sat, 10 Feb 2024 07:07:38 -0300 Subject: [PATCH] create poll route adds options when creating poll --- .../migration.sql | 11 +++++++ prisma/schema.prisma | 10 ++++++ src/http/routes/create-poll.ts | 33 +++++++++++++++++++ src/http/server.ts | 23 ++----------- src/lib/prisma.ts | 5 +++ 5 files changed, 61 insertions(+), 21 deletions(-) create mode 100644 prisma/migrations/20240210094848_create_poll_options/migration.sql create mode 100644 src/http/routes/create-poll.ts create mode 100644 src/lib/prisma.ts diff --git a/prisma/migrations/20240210094848_create_poll_options/migration.sql b/prisma/migrations/20240210094848_create_poll_options/migration.sql new file mode 100644 index 0000000..28c40c1 --- /dev/null +++ b/prisma/migrations/20240210094848_create_poll_options/migration.sql @@ -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; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 9665d38..b9dd25a 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -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]) } diff --git a/src/http/routes/create-poll.ts b/src/http/routes/create-poll.ts new file mode 100644 index 0000000..d20ee5d --- /dev/null +++ b/src/http/routes/create-poll.ts @@ -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, + }); + }); +} diff --git a/src/http/server.ts b/src/http/server.ts index 87c8f24..d422cc1 100644 --- a/src/http/server.ts +++ b/src/http/server.ts @@ -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!"); diff --git a/src/lib/prisma.ts b/src/lib/prisma.ts new file mode 100644 index 0000000..2f49681 --- /dev/null +++ b/src/lib/prisma.ts @@ -0,0 +1,5 @@ +import { PrismaClient } from "@prisma/client"; + +export const prisma = new PrismaClient({ + log: ["query"], +});