-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from millionhz/jazlan-img
upload Image post
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// create a query to insert an image | ||
const storeImage_J = (post_id, image_content) => { | ||
const modifiedTime = getCurrentTime(); | ||
const sql = `INSERT INTO images (binary, created_time) VALUES (?, ?)` | ||
|
||
return query(sql, [image_content, modifiedTime]).then(()=> { | ||
const id_query = `SELECT image_id FROM images where image_id = (SELECT LAST_INSERT_ID());` | ||
var id = query(id_query) | ||
id = id["image_id"] | ||
|
||
// update image table now | ||
const posts_table = `UPDATE posts SET (image_id = ?) WHERE post_id = ?;` | ||
return query(posts_table, [id, post_id]) | ||
|
||
}) | ||
}; | ||
|
||
Routes | ||
router.get('/:postId/image_J', (req, res, next) => { | ||
const { image } = req.params; | ||
const { postId } = req.params; | ||
|
||
storeImage_J(postId, image) | ||
.then(() => { | ||
|
||
res.sendStatus(200); | ||
}) | ||
.catch((err) => { | ||
|
||
res.status(500).json({ message: err.message }); | ||
next(err); | ||
}); | ||
}); |