diff --git a/.dockerignore b/.dockerignore deleted file mode 100644 index 79297ed5..00000000 --- a/.dockerignore +++ /dev/null @@ -1,4 +0,0 @@ -Dockerfile -.dockerignore -node_modules -.git diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 56cddc0a..1e2ff854 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -14,4 +10,3 @@ jobs: node-version: 'lts/*' - run: npm ci - run: npx eslint src - - run: npx prisma migrate reset --force --skip-seed diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml deleted file mode 100644 index 471052aa..00000000 --- a/.github/workflows/deploy.yml +++ /dev/null @@ -1,21 +0,0 @@ -name: Fly Deploy -on: - push: - branches: - - main -env: - FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} - DATABASE_URL: ${{ secrets.DATABASE_URL }} - JWT_SECRET: ${{ secrets.JWT_SECRET }} - JWT_EXPIRY: ${{ secrets.JWT_EXPIRY }} -jobs: - deploy: - name: Deploy app to fly.io - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: superfly/flyctl-actions/setup-flyctl@master - - run: echo DATABASE_URL=$DATABASE_URL >> .env - - run: echo JWT_SECRET=$JWT_SECRET >> .env - - run: echo JWT_EXPIRY=$JWT_EXPIRY >> .env - - run: flyctl deploy --remote-only diff --git a/.husky/pre-commit b/.husky/pre-commit deleted file mode 100644 index a41979b3..00000000 --- a/.husky/pre-commit +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -. "$(dirname "$0")/_/husky.sh" - -npx eslint src \ No newline at end of file diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index b0c8ba77..00000000 --- a/Dockerfile +++ /dev/null @@ -1,36 +0,0 @@ -FROM debian:bullseye as builder - -ARG NODE_VERSION=18.12.1 - -RUN apt-get update; apt install -y curl -RUN curl https://get.volta.sh | bash -ENV VOLTA_HOME /root/.volta -ENV PATH /root/.volta/bin:$PATH -RUN volta install node@${NODE_VERSION} - -####################################################################### - -RUN mkdir /app -WORKDIR /app - -# NPM will not install any package listed in "devDependencies" when NODE_ENV is set to "production", -# to install all modules: "npm install --production=false". -# Ref: https://docs.npmjs.com/cli/v9/commands/npm-install#description - -ENV NODE_ENV production - -COPY . . - -RUN npm install -FROM debian:bullseye - -LABEL fly_launch_runtime="nodejs" - -COPY --from=builder /root/.volta /root/.volta -COPY --from=builder /app /app - -WORKDIR /app -ENV NODE_ENV production -ENV PATH /root/.volta/bin:$PATH - -CMD [ "npm", "run", "start" ] diff --git a/.env.example b/env.example similarity index 100% rename from .env.example rename to env.example diff --git a/fly.toml b/fly.toml deleted file mode 100644 index eac058e7..00000000 --- a/fly.toml +++ /dev/null @@ -1,42 +0,0 @@ -# fly.toml file generated for team-dev-backend-api on 2022-11-30T11:30:34Z - -app = "team-dev-backend-api" -kill_signal = "SIGINT" -kill_timeout = 5 -processes = [] - -[env] - PORT = "8080" - -[experimental] - allowed_public_ports = [] - auto_rollback = true - -[deploy] - release_command = "npx prisma migrate deploy" - -[[services]] - http_checks = [] - internal_port = 8080 - processes = ["app"] - protocol = "tcp" - script_checks = [] - [services.concurrency] - hard_limit = 25 - soft_limit = 20 - type = "connections" - - [[services.ports]] - force_https = true - handlers = ["http"] - port = 80 - - [[services.ports]] - handlers = ["tls", "http"] - port = 443 - - [[services.tcp_checks]] - grace_period = "1s" - interval = "15s" - restart_limit = 0 - timeout = "2s" diff --git a/package.json b/package.json index b5997d22..ae8a6d92 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/prisma/migrations/20241028210717_add_comments_to_posts/migration.sql b/prisma/migrations/20241028210717_add_comments_to_posts/migration.sql new file mode 100644 index 00000000..0651634d --- /dev/null +++ b/prisma/migrations/20241028210717_add_comments_to_posts/migration.sql @@ -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; diff --git a/prisma/migrations/20241029071000_prisma_update_24_10_29_08_03_iris_flystam/migration.sql b/prisma/migrations/20241029071000_prisma_update_24_10_29_08_03_iris_flystam/migration.sql new file mode 100644 index 00000000..61ef7be7 --- /dev/null +++ b/prisma/migrations/20241029071000_prisma_update_24_10_29_08_03_iris_flystam/migration.sql @@ -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; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 72ec5632..255d96b6 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -25,6 +25,7 @@ model User { cohortId Int? cohort Cohort? @relation(fields: [cohortId], references: [id]) posts Post[] + comments Comment[] deliveryLogs DeliveryLog[] } @@ -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 { diff --git a/src/controllers/comment.js b/src/controllers/comment.js new file mode 100644 index 00000000..d2d678a9 --- /dev/null +++ b/src/controllers/comment.js @@ -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') + } +} diff --git a/src/domain/comment.js b/src/domain/comment.js new file mode 100644 index 00000000..da47d1d0 --- /dev/null +++ b/src/domain/comment.js @@ -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' } + }) + } +} diff --git a/src/routes/comment.js b/src/routes/comment.js new file mode 100644 index 00000000..fe4b31c0 --- /dev/null +++ b/src/routes/comment.js @@ -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 diff --git a/src/server.js b/src/server.js index a3f67eeb..3573e461 100644 --- a/src/server.js +++ b/src/server.js @@ -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' @@ -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) => {