-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditpost.js
41 lines (33 loc) · 1.41 KB
/
editpost.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const express = require('express');
const router = express.Router();
const db = require('./db.js');
router.use(express.urlencoded({ extended: false }));
router.use(express.json());
router.put('/', (req, res) => {
const { post_id, title, category, study_name, content} = req.body;
const selectSql = 'SELECT * FROM posts WHERE post_id = ?';
db.query(selectSql, [post_id], (selectError, selectResults) => {
if (selectError) {
console.error('Error selecting post:', selectError);
res.status(500).send('Internal server error');
return;
}
if (selectResults.length === 0) {
console.error('No post found with the given ID:', post_id);
res.status(404).send('No post found with the given ID');
return;
}
const updateSql = 'UPDATE posts SET title = ?, category = ?, study_name = ?, content = ? WHERE post_id = ?';
const values = [title, category, study_name, content, post_id];
db.query(updateSql, values, (updateError, updateResults) => {
if (updateError) {
console.error('Error updating post: ', updateError);
res.status(500).send('Internal server error');
return;
}
console.log('Post updated successfully');
res.status(200).send('Post updated successfully');
});
});
});
module.exports = router;