From 587456017459a0ecc5edb3bfb7a9a564c1d8fb34 Mon Sep 17 00:00:00 2001 From: Carlo M Date: Mon, 21 Oct 2024 06:42:24 -0600 Subject: [PATCH 1/6] Tweaked CICD, removed husky, removed deployment --- .dockerignore | 4 ---- .github/workflows/ci.yml | 5 ----- .github/workflows/deploy.yml | 21 ------------------ .husky/pre-commit | 4 ---- DB_SETUP.md | 18 ---------------- Dockerfile | 36 ------------------------------- fly.toml | 42 ------------------------------------ package.json | 2 -- 8 files changed, 132 deletions(-) delete mode 100644 .dockerignore delete mode 100644 .github/workflows/deploy.yml delete mode 100644 .husky/pre-commit delete mode 100644 DB_SETUP.md delete mode 100644 Dockerfile delete mode 100644 fly.toml 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/DB_SETUP.md b/DB_SETUP.md deleted file mode 100644 index 5a966b81..00000000 --- a/DB_SETUP.md +++ /dev/null @@ -1,18 +0,0 @@ -## Creating your databases with ElephantSQL - -1. [Sign in to ElephantSQL](https://customer.elephantsql.com/login) using your GitHub account - - If it asks you to create a team, create one with any name - -2. When logged in, click the green *Create New Instance* button in the top right -![](./assets/db-setup/1.PNG) - -3. Enter a name for your new database instance and choose the *Tiny Turtle (Free)* plan. **This will be your PRIMARY database** -![](./assets/db-setup/2.PNG) - -4. Repeat the same steps to create a second database, this time give it the same name but add `-shadow` to the end. **This will be your SHADOW database** - -5. In your shadow instance: - - click the *Browser* menu item on the left side of the screen - - in the SQL Browser text input, enter `CREATE SCHEMA shadow;` - - click the *Execute* button -![](./assets/db-setup/3.PNG) \ 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/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 41e19e5e..4efc3e40 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" }, "prisma": { @@ -33,7 +32,6 @@ "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" From de9a8c5e9700591c0ea65dea18210636b73f27a3 Mon Sep 17 00:00:00 2001 From: eyvmal Date: Tue, 29 Oct 2024 09:51:58 +0100 Subject: [PATCH 2/6] add patch endpoint for post --- src/controllers/post.js | 11 +++++++++++ src/routes/post.js | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/controllers/post.js b/src/controllers/post.js index 7b168039..0fcc8896 100644 --- a/src/controllers/post.js +++ b/src/controllers/post.js @@ -26,3 +26,14 @@ export const getAll = async (req, res) => { ] }) } + +export const updateById = async (req, res) => { + const { content } = req.body + const { id } = req.params + + if (!content) { + return sendDataResponse(res, 400, { content: 'Must provide content' }) + } + + return sendDataResponse(res, 200, { post: { id, content } }) +} diff --git a/src/routes/post.js b/src/routes/post.js index a7fbbfb3..9fa2cfee 100644 --- a/src/routes/post.js +++ b/src/routes/post.js @@ -1,10 +1,11 @@ import { Router } from 'express' -import { create, getAll } from '../controllers/post.js' +import { create, getAll, updateById } from '../controllers/post.js' import { validateAuthentication } from '../middleware/auth.js' const router = Router() router.post('/', validateAuthentication, create) router.get('/', validateAuthentication, getAll) +router.patch('/:id', validateAuthentication, updateById) export default router From 1ec0f3cd7ce8df95b99bc828a201380bcc333a3b Mon Sep 17 00:00:00 2001 From: eyvmal Date: Tue, 29 Oct 2024 09:52:50 +0100 Subject: [PATCH 3/6] add swagger doc for patch endpoint for post --- docs/openapi.yml | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/docs/openapi.yml b/docs/openapi.yml index 5f2a05f2..2d4e5b4a 100644 --- a/docs/openapi.yml +++ b/docs/openapi.yml @@ -216,6 +216,41 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + /posts/{id}: + patch: + tags: + - post + summary: Patch a post by id + description: patch a post + operationId: updatePostById + security: + - bearerAuth: [] + parameters: + - name: id + in: path + description: 'The post id that needs to be updated' + required: true + schema: + type: integer + requestBody: + description: The post description + content: + application/json: + schema: + $ref: '#/components/schemas/UpdatePost' + responses: + '200': + description: Successful operation + content: + application/json: + schema: + $ref: '#/components/schemas/Posts' + '401': + description: Unauthorised + content: + application/json: + schema: + $ref: '#/components/schemas/Error' /logs: post: tags: @@ -430,7 +465,13 @@ components: type: string profileImageUrl: type: string - + + UpdatePost: + type: object + properties: + content: + type: string + CreatedUser: type: object properties: From a63fb24963205691643031e9c15a13de197cbe6c Mon Sep 17 00:00:00 2001 From: eyvmal Date: Tue, 29 Oct 2024 10:32:47 +0100 Subject: [PATCH 4/6] update updateById method to interact with database --- src/controllers/post.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/controllers/post.js b/src/controllers/post.js index 0fcc8896..959ea83a 100644 --- a/src/controllers/post.js +++ b/src/controllers/post.js @@ -1,4 +1,5 @@ import { sendDataResponse } from '../utils/responses.js' +import { getPostById, updateContentById } from '../domain/post.js' export const create = async (req, res) => { const { content } = req.body @@ -28,12 +29,22 @@ export const getAll = async (req, res) => { } export const updateById = async (req, res) => { - const { content } = req.body const { id } = req.params + const { content } = req.body - if (!content) { - return sendDataResponse(res, 400, { content: 'Must provide content' }) + try { + const post = await getPostById(Number(id)) + if (post) { + const updatedPost = await updateContentById(Number(id), content) + return sendDataResponse(res, 200, { post: updatedPost }) + } else { + return sendDataResponse(res, 404, { + content: `Post with id ${id} not found` + }) + } + } catch (error) { + return sendDataResponse(res, 500, { + content: 'Internal server error' + }) } - - return sendDataResponse(res, 200, { post: { id, content } }) } From b70d04e9afc639e00260e2f2120bf3fc6b007142 Mon Sep 17 00:00:00 2001 From: eyvmal Date: Tue, 29 Oct 2024 10:33:15 +0100 Subject: [PATCH 5/6] add getPostById and updateContentById db methods --- src/domain/post.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/domain/post.js diff --git a/src/domain/post.js b/src/domain/post.js new file mode 100644 index 00000000..940c6d7e --- /dev/null +++ b/src/domain/post.js @@ -0,0 +1,40 @@ +import dbClient from '../utils/dbClient.js' + +export class Post { + constructor(id = null) { + this.id = id + } + + toJSON() { + return { + post: { + id: this.id + } + } + } +} + +export async function getPostById(id) { + try { + const post = await dbClient.post.findUnique({ + where: { id: id } + }) + return post + } catch (error) { + console.error('Error fetching post by ID:', error) + return null + } +} + +export async function updateContentById(id, content) { + try { + const post = await dbClient.post.update({ + where: { id: id }, + data: { content: content } + }) + return post + } catch (error) { + console.error('Error updating post content by ID:', error) + return null + } +} From dff0614743f4e38bd635fb5094eb2a03fe148ff5 Mon Sep 17 00:00:00 2001 From: eyvmal Date: Tue, 29 Oct 2024 11:30:47 +0100 Subject: [PATCH 6/6] add all error messages --- docs/openapi.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/openapi.yml b/docs/openapi.yml index 2d4e5b4a..45d7c88d 100644 --- a/docs/openapi.yml +++ b/docs/openapi.yml @@ -251,6 +251,18 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: Not Found + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + '500': + description: Server error + content: + application/json: + schema: + $ref: '#/components/schemas/Error' /logs: post: tags: