142 lines
4.5 KiB
JavaScript
142 lines
4.5 KiB
JavaScript
var express = require('express');
|
|
var router = express.Router();
|
|
var mq = require('../mq/rmq');
|
|
const auth = require('../auth/oauth');
|
|
const axios = require('axios');
|
|
|
|
router.get('/', auth.jwtCheck, function(req, res, next) {
|
|
res.status(200).send({
|
|
msg: "This is the / endpoint of the backend!"
|
|
});
|
|
});
|
|
|
|
/* Routes for posts (news and questions) */
|
|
router.get('/get_news', auth.jwtCheck, function(req, res, next) {
|
|
});
|
|
|
|
router.get('/get_questions', auth.jwtCheck, function(req, res, next) {
|
|
});
|
|
|
|
router.post('/submit_post', auth.jwtCheck, function(req, res, next) {
|
|
if (!req.body) {
|
|
res.status(400).send({
|
|
message: "Content can not be empty!"
|
|
});
|
|
return;
|
|
}
|
|
|
|
const post = {
|
|
title: req.body.title,
|
|
url: req.body.url || '',
|
|
author: req.body.author,
|
|
author_email: req.body.email,
|
|
description: req.body.description || ''
|
|
};
|
|
|
|
mq.publish("", "jobs",
|
|
Buffer.from(JSON.stringify({...post, method: "submit_post"})),
|
|
() => {
|
|
axios
|
|
.post('http://db_server:3001/stat_submit_post', post)
|
|
.then(_res => {
|
|
if (_res.status === 200) {
|
|
res.status(201).send({
|
|
msg: "Created new post with title: " + post.title +
|
|
", and author: " + post.author
|
|
});
|
|
} else {
|
|
res.status(400).send({
|
|
msg: "Failed to create new post with title: " + post.title +
|
|
", and author: " + post.author
|
|
});
|
|
}
|
|
})
|
|
.catch(_err => {
|
|
console.log(_err);
|
|
});
|
|
});
|
|
});
|
|
|
|
/* Routes for comments */
|
|
router.post('/add_comment', auth.jwtCheck, function(req, res, next) {
|
|
if (!req.body) {
|
|
res.status(400).send({
|
|
message: "Content can not be empty!"
|
|
});
|
|
return;
|
|
}
|
|
|
|
const comm = {
|
|
ts: Date.now(),
|
|
content: req.body.content,
|
|
author: req.body.author,
|
|
news_id: req.body.news_id
|
|
};
|
|
|
|
mq.publish("", "jobs",
|
|
Buffer.from(JSON.stringify({...comm, method: "add_comment"})),
|
|
() => {
|
|
axios
|
|
.post('http://db_server:3001/stat_add_comment', comm)
|
|
.then(_res => {
|
|
if (_res.status === 200) {
|
|
res.status(201).send({
|
|
msg: "Created new comment from author: " + comm.author +
|
|
", with news_id: " + comm.news_id
|
|
});
|
|
} else {
|
|
res.status(400).send({
|
|
msg: "Failed to create new comment from author: " + comm.author +
|
|
", with news_id: " + comm.news_id
|
|
});
|
|
}
|
|
})
|
|
.catch(_err => {
|
|
console.log(_err);
|
|
});
|
|
});
|
|
|
|
mq.publish("", "mailq",
|
|
Buffer.from(JSON.stringify({...comm, method: "add_comment"})), () => {});
|
|
});
|
|
|
|
router.get('/get_comments', auth.jwtCheck, function(req, res, next) {
|
|
});
|
|
|
|
router.delete('/delete_comment/:news_id/:comment_id', auth.jwtCheck, auth.adminScope, (req, res, next) => {
|
|
if (!req.params) {
|
|
res.status(400).send({
|
|
message: "Query params can not be empty!"
|
|
});
|
|
return;
|
|
}
|
|
|
|
const settings = {
|
|
news_id: req.params.news_id,
|
|
comment_id: req.params.comment_id
|
|
};
|
|
|
|
mq.publish("", "jobs",
|
|
Buffer.from(JSON.stringify({...settings, method: "delete_comment"})),
|
|
() => {
|
|
axios
|
|
.post(`http://db_server:3001/stat_delete_comment/${settings.news_id}/${settings.comment_id}`, settings)
|
|
.then(_res => {
|
|
if (_res.status === 200) {
|
|
res.status(201).send({
|
|
msg: `Deleted comment with news_id=${settings.news_id} and comment_id=${settings.comment_id}`
|
|
});
|
|
} else {
|
|
res.status(400).send({
|
|
msg: `Failed to delete comment with news_id=${settings.news_id} and comment_id=${settings.comment_id}`
|
|
});
|
|
}
|
|
})
|
|
.catch(_err => {
|
|
console.log(_err);
|
|
});
|
|
});
|
|
});
|
|
|
|
module.exports = router;
|