Skip to content

Commit

Permalink
server and prisma config; create poll route
Browse files Browse the repository at this point in the history
  • Loading branch information
brunodsousa committed Feb 10, 2024
1 parent 17a9fff commit de1ef64
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
9 changes: 9 additions & 0 deletions prisma/migrations/20240210004355_create_poll/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- CreateTable
CREATE TABLE "Poll" (
"id" TEXT NOT NULL,
"title" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "Poll_pkey" PRIMARY KEY ("id")
);
3 changes: 3 additions & 0 deletions prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
15 changes: 15 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model Poll {
id String @id @default(uuid())
title String
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
}
29 changes: 29 additions & 0 deletions src/http/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import fastify, { FastifyReply, FastifyRequest } from "fastify";
import { PrismaClient } from "@prisma/client";
import { z } from "zod";

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.listen({ port: 3333 }).then(() => {
console.log("HTTP server running!");
});

0 comments on commit de1ef64

Please sign in to comment.