-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
61 additions
and
21 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
prisma/migrations/20240210094848_create_poll_options/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
}); |