Skip to content

5 implement put post endpoint #110

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

55 changes: 54 additions & 1 deletion docs/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,53 @@ 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'
'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:
Expand Down Expand Up @@ -430,7 +477,13 @@ components:
type: string
profileImageUrl:
type: string


UpdatePost:
type: object
properties:
content:
type: string

CreatedUser:
type: object
properties:
Expand Down
42 changes: 0 additions & 42 deletions fly.toml

This file was deleted.

2 changes: 0 additions & 2 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,7 +35,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"
Expand Down
22 changes: 22 additions & 0 deletions src/controllers/post.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -26,3 +27,24 @@ export const getAll = async (req, res) => {
]
})
}

export const updateById = async (req, res) => {
const { id } = req.params
const { content } = req.body

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'
})
}
}
40 changes: 40 additions & 0 deletions src/domain/post.js
Original file line number Diff line number Diff line change
@@ -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
}
}
3 changes: 2 additions & 1 deletion src/routes/post.js
Original file line number Diff line number Diff line change
@@ -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
Loading