Fixed some forum functions

This commit is contained in:
David Huang 2022-09-24 22:14:58 -04:00
parent d195660092
commit 6506c0aece

View File

@ -9,22 +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; 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++) { for (let i = 0; i < images.length; i++) {
const image = new Image({ data: images[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({
@ -32,7 +51,7 @@ 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,
@ -42,26 +61,17 @@ post_thread = async(req) => {
console.log("New forum created"); 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 => { }).catch(err => {
console.log("New forum error -- not created"); console.log("New forum error -- not created");
console.error(err); return server_error();
let res = {
status: 500,
body: "server error"
}
return JSON.stringify(res)
}); });
} }
catch (err) { catch (err) {
console.error(err); console.error(err);
let res = { return server_error();
status: 500,
body: "server error"
}
return JSON.stringify(res)
} }
} }
@ -69,21 +79,13 @@ update_thread = async(req) => {
console.log(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;
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 ) {
let res = { return error_400("Missing Parameters");
status: 400,
body: "Missing parameters",
}
return JSON.stringify(res);
} }
const forum = await Forum.findById(id); const forum = await Forum.findById(id);
if (!forum) { if (!forum) {
let res = { return error_400("Thread does not exist");
status: 400,
body: "Thread does not exist",
}
return JSON.stringify(res);
} }
let res = { let res = {
@ -98,11 +100,7 @@ delete_thread = async(req) => {
console.log(req); console.log(req);
const { id } = req; const { id } = req;
if (!id) { if (!id) {
let res = { return error_400("Missing Parameters");
status: 400,
body: "Missing parameters",
}
return JSON.stringify(res);
} }
Forum.findByIdAndDelete(id).then(() => { Forum.findByIdAndDelete(id).then(() => {
@ -115,11 +113,7 @@ delete_thread = async(req) => {
}).catch(err => { }).catch(err => {
console.log("Forum deletion error"); console.log("Forum deletion error");
console.log(err); console.log(err);
let res = { return server_error();
status: 500,
body: "Server error",
}
return JSON.stringify(res);
}); });
} }
@ -128,38 +122,22 @@ delete_thread = async(req) => {
favorite = async(req) => { favorite = async(req) => {
const { id, forum_id } = req; const { id, forum_id } = req;
if ( !id || !forum_id ) { if ( !id || !forum_id ) {
let res = { return error_400("Missing Parameters");
status: 400,
body: "Missing parameters",
}
return JSON.stringify(res);
} }
let user = User.findById(id); let user = User.findById(id);
if (!user) { if (!user) {
let res = { return error_400("User does not exist");
status: 400,
body: "User does not exist",
}
return JSON.stringify(res);
} }
let forum = Forum.findById(forum_id); let forum = Forum.findById(forum_id);
if (!forum) { if (!forum) {
let res = { return error_400("Thread does not exist")
status: 400,
body: "Thread does not exist",
}
return JSON.stringify(res);
} }
let found = user.favorites.indexOf(forum_id) let found = user.favorites.indexOf(forum_id)
if (found >= 0) { if (found >= 0) {
let res = { return error_400("Thread already favorited");
status: 400,
body: "Thread already favorited",
}
return JSON.stringify(res);
} }
user.favorites.push(forum_id); user.favorites.push(forum_id);
forum.favorited_by++; forum.favorited_by++;
@ -176,58 +154,34 @@ favorite = async(req) => {
}).catch(err => { }).catch(err => {
console.log("Thread favorite error"); console.log("Thread favorite error");
console.log(err); console.log(err);
let res = { return server_error();
status: 500,
body: "Server error",
}
return JSON.stringify(res);
}) })
}).catch(err => { }).catch(err => {
console.log("User favorite error"); console.log("User favorite error");
console.log(err); console.log(err);
let res = { return server_error();
status: 500,
body: "Server error",
}
return JSON.stringify(res);
}); });
} }
unfavorite = async(req) => { unfavorite = async(req) => {
const { id, forum_id } = req; const { id, forum_id } = req;
if ( !id || !forum_id ) { if ( !id || !forum_id ) {
let res = { return error_400("Missing parameters");
status: 400,
body: "Missing parameters",
}
return JSON.stringify(res);
} }
let user = User.findById(id); let user = User.findById(id);
if (!user) { if (!user) {
let res = { return error_400("User does not exist")
status: 400,
body: "User does not exist",
}
return JSON.stringify(res);
} }
let forum = Forum.findById(forum_id); let forum = Forum.findById(forum_id);
if (!forum) { if (!forum) {
let res = { return error_400("Thread does not exist")
status: 400,
body: "Thread does not exist",
}
return JSON.stringify(res);
} }
let found = user.favorites.indexOf(forum_id) let found = user.favorites.indexOf(forum_id)
if (found >= 0) { if (found >= 0) {
let res = { return error_400("Thread already favorited")
status: 400,
body: "Thread already favorited",
}
return JSON.stringify(res);
} }
user.favorites.splice(found, 1); user.favorites.splice(found, 1);
forum.favorited_by--; forum.favorited_by--;
@ -244,20 +198,12 @@ unfavorite = async(req) => {
}).catch(err => { }).catch(err => {
console.log("Thread unfavorite error"); console.log("Thread unfavorite error");
console.log(err); console.log(err);
let res = { return server_error();
status: 500,
body: "Server error",
}
return JSON.stringify(res);
}) })
}).catch(err => { }).catch(err => {
console.log("User unfavorite error"); console.log("User unfavorite error");
console.log(err); console.log(err);
let res = { return server_error();
status: 500,
body: "Server error",
}
return JSON.stringify(res);
}); });
} }
@ -273,8 +219,15 @@ get_eat_thread_list = async(req) => {
} }
get_cook_thread = async(req) => { get_thread = async(req) => {
const { id } = req;
if (!id) {
let res = {
status: 400,
body: "Missing parameters",
}
return JSON.stringify(res);
}
} }
get_eat_thread = async(req) => { get_eat_thread = async(req) => {