Compare commits

..

No commits in common. "fb4a5806b324e41c50cd1d0543dd966471b5b81b" and "65b1841a7aaa7cc2f8d6498d92a1a434d8996fb8" have entirely different histories.

6 changed files with 24 additions and 424 deletions

View File

@ -1,124 +0,0 @@
const uuid = require('uuid');
const config = require('../config')
const rabbitMQ = config.rabbitMQ
const amqp = require('amqplib/callback_api');
postThread = async (req, res) => {
req = req.body;
return await assert("postThread", req, res);
}
getCookThreadList = async (req, res) => {
req = req.body;
return await assert("getCookThreadList", req, res);
}
getEatThreadList = async (req, res) => {
req = req.body;
return await assert("getEatThreadList", req, res);
}
getCookThread = async (req, res) => {
req = req.body;
return await assert("getCookThread", req, res);
}
searchCookThread = async (req, res) => {
req = req.body;
return await assert("searchCookThread", req, res);
}
searchEatThread = async (req, res) => {
req = req.body;
return await assert("searchEatThread", req, res);
}
// updateThread = async (req, res) => {
// req = req.body;
// return await assert("updateThread", req, res);
// }
// logoutUser = async (req, res) => {
// return await res.cookie("token", '', {
// httpOnly: true,
// secure: true,
// sameSite: "lax"
// }).status(200).json({
// success: true
// })
// }
// /deletethread
// /favorite
// /unfavorite
// /getrandomthreadlist:num
const assert = async (queueName, req, res) => {
amqp.connect(rabbitMQ, function (error0, connection) {
if (error0) {
throw error0;
}
connection.createChannel(function (error1, channel) {
if (error1) {
throw error1;
}
channel.assertQueue('', {
exclusive: true
}, function (error2, q) {
if (error2) {
throw error2;
}
let correlationId = uuid.v4();
console.log(' [x] Requesting: ', req);
channel.consume(q.queue, function (msg) {
console.log(msg)
if (msg.properties.correlationId == correlationId) {
console.log(' [.] Got %s', msg.content.toString());
msg = JSON.parse(msg.content)
console.log(msg.cookie)
if (msg.cookie) {
res.cookie("token", msg.cookie, {
httpOnly: true,
secure: true,
sameSite: "lax"
}).status(msg.status).send(msg.body)
}
else {
res.status(msg.status).send(msg.body)
}
setTimeout(function () {
connection.close();
}, 500);
}
else {
res.status(500).send("Internal server error")
}
}, {
noAck: true
});
channel.sendToQueue(queueName,
Buffer.from(JSON.stringify(req)), {
correlationId: correlationId,
replyTo: q.queue
});
});
});
});
}
module.exports = {
postThread,
getCookThreadList,
getEatThreadList,
getCookThread,
searchCookThread,
searchEatThread,
}

View File

@ -15,15 +15,6 @@ loginUser = async (req, res) => {
return await assert("login", req, res);
}
logoutUser = async (req, res) => {
return await res.cookie("token", '', {
httpOnly: true,
secure: true,
sameSite: "lax"
}).status(200).json({
success: true
})
}
const assert = async (queueName, req, res) => {
amqp.connect(rabbitMQ, function (error0, connection) {
@ -84,5 +75,4 @@ const assert = async (queueName, req, res) => {
module.exports = {
registerUser,
loginUser,
logoutUser,
}

View File

@ -1,15 +1,8 @@
const express = require('express')
const router = express.Router()
const UserAPI = require('../apis/user-apis')
const ForumAPI = require('../apis/forum-apis')
router.post('/login', UserAPI.loginUser)
router.post('/register', UserAPI.registerUser)
router.get('/logout', UserAPI.logoutUser)
router.post('/postthread', ForumAPI.postThread)
router.get
module.exports = router

View File

@ -2,7 +2,7 @@ const jwt = require("jsonwebtoken")
const config = require('../config')
function authManager() {
getUserId = function (token) {
getUserID = function (token) {
try {
if (!token) {
return 'Guest';

View File

@ -147,41 +147,6 @@ const login = async (req) => {
}
}
const getUserById = async (req) => {
const user = await User.findById({_id: req});
if (!user) {
let res = {
found: false,
user: {}
}
return JSON.stringify(res)
}
else {
let res = {
found: true,
user: {
name: user.name,
email: user.email,
Id: user._id
}
}
return JSON.stringify(res)
}
}
const getUserByToken = async (req) => {
let id = getUserId(req)
if (id !== 'Guest') {
return getUserById(id)
}
else {
let res = {
found: false,
user: {}
}
return JSON.stringify(res)
}
}
const amqp = require('amqplib/callback_api');
amqp.connect(rabbitMQ, function (error0, connection) {
@ -233,50 +198,4 @@ amqp.connect(rabbitMQ, function (error0, connection) {
})
});
});
connection.createChannel(function (error1, channel) {
if (error1) {
throw error1;
}
var queue = 'getUserById';
channel.assertQueue(queue, {
durable: false
});
channel.prefetch(1);
console.log(' [x] Awaiting RPC requests');
channel.consume(queue, function reply(msg) {
getUserById(msg.content.toString()).then((res) => {
channel.sendToQueue(msg.properties.replyTo,
Buffer.from(res.toString()), {
correlationId: msg.properties.correlationId
});
channel.ack(msg);
})
});
});
connection.createChannel(function (error1, channel) {
if (error1) {
throw error1;
}
var queue = 'getUserByToken';
channel.assertQueue(queue, {
durable: false
});
channel.prefetch(1);
console.log(' [x] Awaiting RPC requests');
channel.consume(queue, function reply(msg) {
getUserByToken(msg.content.toString()).then((res) => {
channel.sendToQueue(msg.properties.replyTo,
Buffer.from(res.toString()), {
correlationId: msg.properties.correlationId
});
channel.ack(msg);
})
});
});
});

View File

@ -9,41 +9,17 @@ const rabbitMQ = config.rabbitMQ
db.on('error', console.error.bind(console, "MongoDB Atlas connection error"))
error_400 = (msg) => {
let res = {
status: 400,
body: msg
}
return JSON.stringify(res);
}
server_error = () => {
let res = {
status: 500,
body: "Server error",
}
return JSON.stringify(res);
}
post_thread = async(req) => {
try {
console.log(req);
const { title, author, category, tags, images, content } = req;
const { title, author, category, tags, images, content, comments, favorited_by } = req.body;
if (!title || !author || !category || !tags || images === undefined || content === undefined ) {
return error_400("Missing Paratemers")
if (!title || !author || !category || !tags || !images || !content || !comments || !favorited_by ) {
let res = {
status: 400,
body: "ok",
}
let image_ids = [];
for (let i = 0; i < images.length; i++) {
const image = new Image({ data: images[i] });
image.save().then(() => {
console.log("Image created and saved");
image_ids.push(image._id);
}).catch(err => {
console.log("Image save error");
console.log(err);
})
return JSON.stringify(res);
}
const newForum = new Forum({
@ -51,198 +27,44 @@ post_thread = async(req) => {
author: author,
category: category,
tags: tags,
images: image_ids,
images: images,
content: content,
comments: comments,
favorited_by: favorited_by,
})
newForum.save().then(() => {
console.log("New forum created");
await newForum.save();
let res = {
status: 200,
body: "Ok",
body: "Missing parameters",
}
return JSON.stringify(res);
}).catch(err => {
console.log("New forum error -- not created");
return server_error();
});
}
catch (err) {
console.error(err);
return server_error();
let res = {
status: 500,
body: "server error"
}
return JSON.stringify(res)
}
}
update_thread = async(req) => {
console.log(req);
const { id, title, author, category, tags, images, content, comments, favorited_by } = req;
const { id, title, author, category, tags, images, content, comments, favorited_by } = req.body;
if ( !id || !title || !author || !category || !tags || images === undefined || content === undefined || comments === undefined || !favorited_by === undefined ) {
return error_400("Missing Parameters");
}
const forum = await Forum.findById(id);
if (!forum) {
return error_400("Thread does not exist");
}
let res = {
status: 200,
body: forum
}
return JSON.stringify(res);
}
delete_thread = async(req) => {
console.log(req);
const { id } = req;
if (!id) {
return error_400("Missing Parameters");
}
Forum.findByIdAndDelete(id).then(() => {
console.log("Thread deleted");
let res = {
status: 200,
body: "Ok"
}
return JSON.stringify(res);
}).catch(err => {
console.log("Forum deletion error");
console.log(err);
return server_error();
});
}
favorite = async(req) => {
const { id, forum_id } = req;
if ( !id || !forum_id ) {
return error_400("Missing Parameters");
}
let user = User.findById(id);
if (!user) {
return error_400("User does not exist");
}
let forum = Forum.findById(forum_id);
if (!forum) {
return error_400("Thread does not exist")
}
let found = user.favorites.indexOf(forum_id)
if (found >= 0) {
return error_400("Thread already favorited");
}
user.favorites.push(forum_id);
forum.favorited_by++;
user.save().then(() => {
console.log("User favorites updated");
forum.save().then(() => {
console.log("Thread favorite updated")
let res = {
status: 200,
body: "Ok"
}
return JSON.stringify(res);
}).catch(err => {
console.log("Thread favorite error");
console.log(err);
return server_error();
})
}).catch(err => {
console.log("User favorite error");
console.log(err);
return server_error();
});
}
unfavorite = async(req) => {
const { id, forum_id } = req;
if ( !id || !forum_id ) {
return error_400("Missing parameters");
}
let user = User.findById(id);
if (!user) {
return error_400("User does not exist")
}
let forum = Forum.findById(forum_id);
if (!forum) {
return error_400("Thread does not exist")
}
let found = user.favorites.indexOf(forum_id)
if (found >= 0) {
return error_400("Thread already favorited")
}
user.favorites.splice(found, 1);
forum.favorited_by--;
user.save().then(() => {
console.log("User unfavorites updated");
forum.save().then(() => {
console.log("Thread unfavorite updated");
let res = {
status: 200,
body: "Ok"
}
return JSON.stringify(res);
}).catch(err => {
console.log("Thread unfavorite error");
console.log(err);
return server_error();
})
}).catch(err => {
console.log("User unfavorite error");
console.log(err);
return server_error();
});
}
get_random_thread_list = async(req) => {
}
get_cook_thread_list = async(req) => {
}
get_eat_thread_list = async(req) => {
}
get_thread = async(req) => {
const { id } = req;
if (!id) {
if ( !id || !title || !author || !category || !tags || !images || !content || !comments || !favorited_by ) {
let res = {
status: 400,
body: "Missing parameters",
}
return JSON.stringify(res);
}
}
get_eat_thread = async(req) => {
}
search_cook_thread = async(req) => {
}
search_eat_thread = async(req) => {
}
const amqp = require('amqplib/callback_api');
amqp.connect(rabbitMQ, function (error0, connection) {
if (error0) {