Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

32 update prisma to represent the api spec #104

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
4 changes: 0 additions & 4 deletions .dockerignore

This file was deleted.

5 changes: 0 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
name: CI
on: [push, pull_request]
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
JWT_TOKEN: ${{ secrets.JWT_TOKEN }}
JWT_EXPIRY: ${{ secrets.JWT_EXPIRY }}
jobs:
test:
runs-on: ubuntu-latest
Expand All @@ -14,4 +10,3 @@ jobs:
node-version: 'lts/*'
- run: npm ci
- run: npx eslint src
- run: npx prisma migrate reset --force --skip-seed
21 changes: 0 additions & 21 deletions .github/workflows/deploy.yml

This file was deleted.

4 changes: 0 additions & 4 deletions .husky/pre-commit

This file was deleted.

36 changes: 0 additions & 36 deletions Dockerfile

This file was deleted.

File renamed without changes.
42 changes: 0 additions & 42 deletions fly.toml

This file was deleted.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"scripts": {
"start": "node src/index.js",
"dev": "nodemon src/index.js",
"prepare": "husky install",
"db-reset": "prisma migrate reset",
"lint": "eslint .",
"lint:fix": "eslint --fix",
Expand Down Expand Up @@ -36,13 +35,12 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^5.1.0",
"husky": "^7.0.4",
"nodemon": "^2.0.15",
"prettier": "^2.6.2",
"prisma": "^3.12.0"
"prisma": "^3.15.2"
},
"dependencies": {
"@prisma/client": "^3.12.0",
"@prisma/client": "^3.15.2",
"bcrypt": "^5.0.1",
"cors": "^2.8.5",
"dotenv": "^16.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- CreateTable
CREATE TABLE "Comment" (
"id" SERIAL NOT NULL,
"content" TEXT NOT NULL,
"userId" INTEGER NOT NULL,
"postId" INTEGER NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- AddForeignKey
ALTER TABLE "Comment" ADD CONSTRAINT "Comment_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Comment" ADD CONSTRAINT "Comment_postId_fkey" FOREIGN KEY ("postId") REFERENCES "Post"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
Warnings:

- Added the required column `createdAt` to the `Cohort` table without a default value. This is not possible if the table is not empty.
- Added the required column `updatedAt` to the `Cohort` table without a default value. This is not possible if the table is not empty.
- Added the required column `createdAt` to the `Post` table without a default value. This is not possible if the table is not empty.
- Added the required column `updatedAt` to the `Post` table without a default value. This is not possible if the table is not empty.

*/
-- AlterTable
ALTER TABLE "Cohort" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL,
ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL;

-- AlterTable
ALTER TABLE "Post" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL,
ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL;
24 changes: 20 additions & 4 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ model User {
cohortId Int?
cohort Cohort? @relation(fields: [cohortId], references: [id])
posts Post[]
comments Comment[]
deliveryLogs DeliveryLog[]
}

Expand All @@ -41,14 +42,29 @@ model Profile {
model Cohort {
id Int @id @default(autoincrement())
users User[]
createdAt DateTime
updatedAt DateTime
deliveryLogs DeliveryLog[]
}

model Post {
id Int @id @default(autoincrement())
content String
userId Int
user User @relation(fields: [userId], references: [id])
id Int @id @default(autoincrement())
content String
createdAt DateTime
updatedAt DateTime
userId Int
user User @relation(fields: [userId], references: [id])
comments Comment[]
}

model Comment {
id Int @id @default(autoincrement())
content String
userId Int
user User @relation(fields: [userId], references: [id])
postId Int
post Post @relation(fields: [postId], references: [id])
createdAt DateTime @default(now())
}

model DeliveryLog {
Expand Down
35 changes: 35 additions & 0 deletions src/controllers/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { sendDataResponse, sendMessageResponse } from '../utils/responses.js'
import Comment from '../domain/comment.js'

export const createComment = async (req, res) => {
const { postId, content } = req.body

if (!postId || !content) {
return sendDataResponse(res, 400, {
message: 'Post ID and content are required'
})
}

try {
const comment = await Comment.create({
postId: parseInt(postId),
userId: req.user.id,
content
})

return sendDataResponse(res, 201, { comment })
} catch (error) {
return sendMessageResponse(res, 500, 'Unable to create comment')
}
}

export const getCommentsByPost = async (req, res) => {
const { postId } = req.params

try {
const comments = await Comment.findByPostId(parseInt(postId))
return sendDataResponse(res, 200, { comments })
} catch (error) {
return sendMessageResponse(res, 500, 'Unable to retrieve comments')
}
}
28 changes: 28 additions & 0 deletions src/domain/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import dbClient from '../utils/dbClient.js'

export default class Comment {
static async create({ postId, userId, content }) {
const newComment = await dbClient.comment.create({
data: {
postId,
userId,
content
},
include: {
user: { select: { id: true, email: true } },
post: { select: { id: true } }
}
})
return newComment
}

static async findByPostId(postId) {
return dbClient.comment.findMany({
where: { postId },
include: {
user: { select: { id: true, email: true, profile: true } }
},
orderBy: { createdAt: 'asc' }
})
}
}
10 changes: 10 additions & 0 deletions src/routes/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Router } from 'express'
import { createComment, getCommentsByPost } from '../controllers/comment.js'
import { validateAuthentication } from '../middleware/auth.js'

const router = Router()

router.post('/', validateAuthentication, createComment)
router.get('/post/:postId', validateAuthentication, getCommentsByPost)

export default router
2 changes: 2 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import cors from 'cors'
import userRouter from './routes/user.js'
import postRouter from './routes/post.js'
import authRouter from './routes/auth.js'
import commentRouter from './routes/comment.js'
import cohortRouter from './routes/cohort.js'
import deliveryLogRouter from './routes/deliveryLog.js'

Expand All @@ -25,6 +26,7 @@ app.use('/users', userRouter)
app.use('/posts', postRouter)
app.use('/cohorts', cohortRouter)
app.use('/logs', deliveryLogRouter)
app.use('/comments', commentRouter)
app.use('/', authRouter)

app.get('*', (req, res) => {
Expand Down
Loading