Merge remote-tracking branch 'refs/remotes/origin/master'

This commit is contained in:
Renge 2022-09-24 21:52:06 -04:00
commit 3a5b6c60c1

View File

@ -12,7 +12,7 @@ db.on('error', console.error.bind(console, "MongoDB Atlas connection error"))
post_thread = async(req) => {
try {
console.log(req);
const { title, author, category, tags, images, content, comments, favorited_by } = req.body;
const { title, author, category, tags, images, content, comments, favorited_by } = req;
if (!title || !author || !category || !tags || !images || !content || !comments || !favorited_by ) {
let res = {
@ -22,6 +22,11 @@ post_thread = async(req) => {
return JSON.stringify(res);
}
for (let i = 0; i < images.length; i++) {
const image = new Image({ data: images[i] });
}
const newForum = new Forum({
title: title,
author: author,
@ -33,12 +38,22 @@ post_thread = async(req) => {
favorited_by: favorited_by,
})
await newForum.save();
newForum.save().then(() => {
console.log("New forum created");
let res = {
status: 200,
body: "Missing parameters",
}
return JSON.stringify(res);
}).catch(err => {
console.log("New forum error -- not created");
console.error(err);
let res = {
status: 500,
body: "server error"
}
return JSON.stringify(res)
});
}
catch (err) {
console.error(err);
@ -52,7 +67,7 @@ post_thread = async(req) => {
update_thread = async(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 ) {
let res = {
@ -62,9 +77,219 @@ update_thread = async(req) => {
return JSON.stringify(res);
}
const forum = await Forum.findById(id);
if (!forum) {
let res = {
status: 400,
body: "Thread does not exist",
}
return JSON.stringify(res);
}
let res = {
status: 200,
body: forum
}
return JSON.stringify(res);
}
delete_thread = async(req) => {
console.log(req);
const { id } = req;
if (!id) {
let res = {
status: 400,
body: "Missing parameters",
}
return JSON.stringify(res);
}
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);
let res = {
status: 500,
body: "Server error",
}
return JSON.stringify(res);
});
}
favorite = async(req) => {
const { id, forum_id } = req;
if ( !id || !forum_id ) {
let res = {
status: 400,
body: "Missing parameters",
}
return JSON.stringify(res);
}
let user = User.findById(id);
if (!user) {
let res = {
status: 400,
body: "User does not exist",
}
return JSON.stringify(res);
}
let forum = Forum.findById(forum_id);
if (!forum) {
let res = {
status: 400,
body: "Thread does not exist",
}
return JSON.stringify(res);
}
let found = user.favorites.indexOf(forum_id)
if (found >= 0) {
let res = {
status: 400,
body: "Thread already favorited",
}
return JSON.stringify(res);
}
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);
let res = {
status: 500,
body: "Server error",
}
return JSON.stringify(res);
})
}).catch(err => {
console.log("User favorite error");
console.log(err);
let res = {
status: 500,
body: "Server error",
}
return JSON.stringify(res);
});
}
unfavorite = async(req) => {
const { id, forum_id } = req;
if ( !id || !forum_id ) {
let res = {
status: 400,
body: "Missing parameters",
}
return JSON.stringify(res);
}
let user = User.findById(id);
if (!user) {
let res = {
status: 400,
body: "User does not exist",
}
return JSON.stringify(res);
}
let forum = Forum.findById(forum_id);
if (!forum) {
let res = {
status: 400,
body: "Thread does not exist",
}
return JSON.stringify(res);
}
let found = user.favorites.indexOf(forum_id)
if (found >= 0) {
let res = {
status: 400,
body: "Thread already favorited",
}
return JSON.stringify(res);
}
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);
let res = {
status: 500,
body: "Server error",
}
return JSON.stringify(res);
})
}).catch(err => {
console.log("User unfavorite error");
console.log(err);
let res = {
status: 500,
body: "Server error",
}
return JSON.stringify(res);
});
}
get_random_thread_list = async(req) => {
}
get_cook_thread_list = async(req) => {
}
get_eat_thread_list = async(req) => {
}
get_cook_thread = async(req) => {
}
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) {