Compare commits

...

10 Commits

Author SHA1 Message Date
fb4a5806b3 add: forum router 2022-09-24 22:42:02 -04:00
David Huang
eba9846e60 Merge branch 'master' of github.com:qndydyhm/sbuhacks 2022-09-24 22:15:06 -04:00
David Huang
6506c0aece Fixed some forum functions 2022-09-24 22:14:58 -04:00
3a5b6c60c1 Merge remote-tracking branch 'refs/remotes/origin/master' 2022-09-24 21:52:06 -04:00
ee6c5fd5f1 feat: implement logOutUser 2022-09-24 21:51:39 -04:00
David Huang
d195660092 Fixed some promise resolves 2022-09-24 21:50:49 -04:00
David Huang
13f2f4b986 Added more function stubs 2022-09-24 21:24:12 -04:00
David Huang
1bd27b7caa Merge branch 'master' of github.com:qndydyhm/sbuhacks 2022-09-24 21:18:02 -04:00
David Huang
a59edc7136 Working on forum 2022-09-24 21:17:51 -04:00
76849cc747 feat: implement getUserById and getUserByToken 2022-09-24 21:12:14 -04:00
6 changed files with 424 additions and 24 deletions

124
api/apis/forum-apis.js Normal file
View File

@ -0,0 +1,124 @@
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,6 +15,15 @@ loginUser = async (req, res) => {
return await assert("login", 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) => { const assert = async (queueName, req, res) => {
amqp.connect(rabbitMQ, function (error0, connection) { amqp.connect(rabbitMQ, function (error0, connection) {
@ -75,4 +84,5 @@ const assert = async (queueName, req, res) => {
module.exports = { module.exports = {
registerUser, registerUser,
loginUser, loginUser,
logoutUser,
} }

View File

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

View File

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

View File

@ -147,6 +147,41 @@ 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'); const amqp = require('amqplib/callback_api');
amqp.connect(rabbitMQ, function (error0, connection) { amqp.connect(rabbitMQ, function (error0, connection) {
@ -198,4 +233,50 @@ 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,17 +9,41 @@ const rabbitMQ = config.rabbitMQ
db.on('error', console.error.bind(console, "MongoDB Atlas connection error")) 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) => { post_thread = async(req) => {
try { try {
console.log(req); console.log(req);
const { title, author, category, tags, images, content, comments, favorited_by } = req.body; const { title, author, category, tags, images, content } = req;
if (!title || !author || !category || !tags || !images || !content || !comments || !favorited_by ) { if (!title || !author || !category || !tags || images === undefined || content === undefined ) {
let res = { return error_400("Missing Paratemers")
status: 400,
body: "ok",
} }
return JSON.stringify(res);
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);
})
} }
const newForum = new Forum({ const newForum = new Forum({
@ -27,44 +51,198 @@ post_thread = async(req) => {
author: author, author: author,
category: category, category: category,
tags: tags, tags: tags,
images: images, images: image_ids,
content: content, content: content,
comments: comments, comments: comments,
favorited_by: favorited_by, favorited_by: favorited_by,
}) })
await newForum.save(); newForum.save().then(() => {
console.log("New forum created");
let res = { let res = {
status: 200, status: 200,
body: "Missing parameters", body: "Ok",
} }
return JSON.stringify(res); return JSON.stringify(res);
}).catch(err => {
console.log("New forum error -- not created");
return server_error();
});
} }
catch (err) { catch (err) {
console.error(err); console.error(err);
let res = { return server_error();
status: 500,
body: "server error"
}
return JSON.stringify(res)
} }
} }
update_thread = async(req) => { update_thread = async(req) => {
console.log(req); console.log(req);
const { id, title, author, category, tags, images, content, comments, favorited_by } = req.body; const { id, title, author, category, tags, images, content, comments, favorited_by } = req;
if ( !id || !title || !author || !category || !tags || !images || !content || !comments || !favorited_by ) { 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) {
let res = { let res = {
status: 400, status: 400,
body: "Missing parameters", body: "Missing parameters",
} }
return JSON.stringify(res); 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'); const amqp = require('amqplib/callback_api');
amqp.connect(rabbitMQ, function (error0, connection) { amqp.connect(rabbitMQ, function (error0, connection) {
if (error0) { if (error0) {