routes on files
This commit is contained in:
96
index.js
96
index.js
@@ -31,6 +31,9 @@ const getUserId = function(req){
|
||||
|
||||
// Definitions
|
||||
const Post = require("./def/post.js")
|
||||
const User = require("./def/user.js");
|
||||
var userRoute = require('./routes/user.js');
|
||||
var postRoute = require('./routes/post.js');
|
||||
|
||||
DB.getDB.then((DB)=>{
|
||||
|
||||
@@ -59,15 +62,23 @@ DB.getDB.then((DB)=>{
|
||||
const username = req.query.username || req.body.username;
|
||||
const password = req.query.password || req.body.password;
|
||||
const email = req.query.email || req.body.email;
|
||||
const profile = req.query.profile || req.body.profile;
|
||||
if(!username || !password || !email) return res.json({status: "fail"})
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
//const hashedPassword = await bcrypt.hash(password, 10);
|
||||
const hashedPassword = await bcrypt.hash('12345', 10);
|
||||
const success = await DB.newUser({
|
||||
username: username,
|
||||
email: email,
|
||||
password: hashedPassword
|
||||
});
|
||||
if(success){
|
||||
return login(req, res);
|
||||
let user = {
|
||||
userid: success.insertedId,
|
||||
profile: profile,
|
||||
}
|
||||
const userObj = new User(user);
|
||||
DB.newProfile(userObj)
|
||||
return await login(req, res);
|
||||
}
|
||||
res.redirect('/signup');
|
||||
}
|
||||
@@ -131,84 +142,9 @@ DB.getDB.then((DB)=>{
|
||||
app.get('/logout', (req, res) => {
|
||||
return logout(req, res);
|
||||
});
|
||||
|
||||
app.get("/post/", sessionChecker, async (req, res) => {
|
||||
let posts = await DB.getPosts();
|
||||
return res.json(posts)
|
||||
});
|
||||
|
||||
app.get("/post/new", sessionChecker, async (req, res) => {
|
||||
let post = {
|
||||
userid: getUserId(req),
|
||||
content: req.query.content
|
||||
}
|
||||
let postObj = new Post(post);
|
||||
DB.newPost(postObj)
|
||||
return res.json({
|
||||
status: "ok"
|
||||
})
|
||||
});
|
||||
|
||||
app.get("/post/newComment", sessionChecker, async (req, res) => {
|
||||
let userid = getUserId(req);
|
||||
let postid = req.query.postid;
|
||||
let content = req.query.content;
|
||||
let comment = {
|
||||
userid: userid,
|
||||
content: content,
|
||||
createdAt: new Date(),
|
||||
lastUpdated: new Date(),
|
||||
reactions: {}
|
||||
}
|
||||
console.log("comment", postid, comment);
|
||||
r = await DB.newComment(postid, comment);
|
||||
console.log(r)
|
||||
return res.json({
|
||||
status: "ok"
|
||||
})
|
||||
});
|
||||
|
||||
app.get("/post/react", sessionChecker, async (req, res) => {
|
||||
let userid = getUserId(req);
|
||||
let postid = req.query.postid;
|
||||
let reaction = {
|
||||
type: "like",
|
||||
createdAt: new Date()
|
||||
};
|
||||
console.log("reaction". postid, reaction);
|
||||
r = await DB.newReaction(postid, userid, reaction);
|
||||
console.log(r);
|
||||
return res.json({
|
||||
status: "ok"
|
||||
});
|
||||
})
|
||||
|
||||
app.get("/post/unreact", sessionChecker, async (req, res) => {
|
||||
let userid = getUserId(req);
|
||||
let postid = req.query.postid;
|
||||
r = await DB.removeReaction(postid, userid);
|
||||
console.log(r)
|
||||
return res.json({
|
||||
status: "ok"
|
||||
})
|
||||
});
|
||||
|
||||
app.get("/post/comment/react", sessionChecker, async (req, res) => {
|
||||
let userid = getUserId(req);
|
||||
let postid = req.query.postid;
|
||||
let commentDate = new Date(req.query.commentDate);
|
||||
let reaction = {
|
||||
type: "like",
|
||||
createdAt: new Date()
|
||||
};
|
||||
console.log(req.query)
|
||||
console.log("comment reaction", postid, commentDate, reaction);
|
||||
r = await DB.newCommentReaction(postid, commentDate, userid, reaction);
|
||||
console.log(r)
|
||||
return res.json({
|
||||
status: "ok"
|
||||
})
|
||||
});
|
||||
|
||||
app.use('/user', sessionChecker, userRoute);
|
||||
app.use('/post', sessionChecker, postRoute);
|
||||
|
||||
// route for handling 404 requests(unavailable routes)
|
||||
app.use(function (req, res, next) {
|
||||
|
||||
Reference in New Issue
Block a user