Added posts and categories
This commit is contained in:
16
category.model.js
Normal file
16
category.model.js
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
const mongoose = require('mongoose');
|
||||||
|
const Schema = mongoose.Schema;
|
||||||
|
|
||||||
|
// Database schema for an email message
|
||||||
|
let Category = new Schema({
|
||||||
|
name: {
|
||||||
|
type: String
|
||||||
|
},
|
||||||
|
color: {
|
||||||
|
type: String
|
||||||
|
},
|
||||||
|
}, {
|
||||||
|
collection: 'categories'
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = mongoose.model('Category', Category);
|
||||||
77
category.route.js
Normal file
77
category.route.js
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
const utils = require('./utils');
|
||||||
|
const express = require('express');
|
||||||
|
const categoryRoutes = express.Router();
|
||||||
|
|
||||||
|
let Post = require('./post.model');
|
||||||
|
let Category = require('./category.model');
|
||||||
|
let Session = require('./session.model');
|
||||||
|
let User = require('./user.model');
|
||||||
|
|
||||||
|
/*
|
||||||
|
POST - /category/create
|
||||||
|
Create a category
|
||||||
|
Response: 201 - Created
|
||||||
|
401 - Unauthorized
|
||||||
|
*/
|
||||||
|
categoryRoutes.route('/create').post((req, res) => {
|
||||||
|
if (!req.body) {
|
||||||
|
res.status(401).send("Missing body");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
utils.checkSession(req.body.userId, req.body.sessionId, (isValidId) => {
|
||||||
|
utils.isAdmin(req.body.userId, (isAdmin) => {
|
||||||
|
if (isValidId && isAdmin) {
|
||||||
|
let c = new Category(req.body);
|
||||||
|
c.save()
|
||||||
|
.then(() => {
|
||||||
|
res.json(c);
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
console.log(e);
|
||||||
|
res.status(500).send("Error creating category");
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
res.status(401).send("Invalid permissions to create category.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
GET - /category/all
|
||||||
|
Get all categories
|
||||||
|
Response: 200 - OK
|
||||||
|
*/
|
||||||
|
categoryRoutes.route('/all').get((req, res) => {
|
||||||
|
Category.find({}, (err, cArr) => {
|
||||||
|
if (err) {
|
||||||
|
console.log(err);
|
||||||
|
res.status(500).send("Error getting categories");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res.status(200).send(cArr);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
GET - /category/posts
|
||||||
|
Get all posts with specified category
|
||||||
|
Response: 200 - OK
|
||||||
|
*/
|
||||||
|
categoryRoutes.route('/posts').get((req, res) => {
|
||||||
|
Post.find({}, (err, postArr) => {
|
||||||
|
if (err) {
|
||||||
|
console.log(err);
|
||||||
|
res.status(500).send("Error getting posts");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
postArr = postArr.filter(post => post.category.includes(req.body._id));
|
||||||
|
res.status(200).send(postArr);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = categoryRoutes;
|
||||||
6
index.js
6
index.js
@@ -8,6 +8,8 @@ const utils = require ('./utils');
|
|||||||
const CronJob = require('cron').CronJob;
|
const CronJob = require('cron').CronJob;
|
||||||
const config = require('./DB.js');
|
const config = require('./DB.js');
|
||||||
const userRoutes = require('./user.route');
|
const userRoutes = require('./user.route');
|
||||||
|
const postRoutes = require('./post.route');
|
||||||
|
const categoryRoutes = require('./category.route');
|
||||||
|
|
||||||
console.log("Starting Kno-Logic Backend Server");
|
console.log("Starting Kno-Logic Backend Server");
|
||||||
|
|
||||||
@@ -31,7 +33,9 @@ app.use(express.urlencoded({ extended: true }))
|
|||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
// Express routes
|
// Express routes
|
||||||
app.use('/users', userRoutes);
|
app.use('/user', userRoutes);
|
||||||
|
app.use('/post', postRoutes);
|
||||||
|
app.use('/category', categoryRoutes);
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log('Express server running on port:', PORT);
|
console.log('Express server running on port:', PORT);
|
||||||
|
|||||||
31
post.model.js
Normal file
31
post.model.js
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
const mongoose = require('mongoose');
|
||||||
|
const Schema = mongoose.Schema;
|
||||||
|
|
||||||
|
// Database schema for an email message
|
||||||
|
let Post = new Schema({
|
||||||
|
title: {
|
||||||
|
type: String
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
type: String
|
||||||
|
},
|
||||||
|
author: {
|
||||||
|
type: String
|
||||||
|
},
|
||||||
|
category: {
|
||||||
|
type: Array
|
||||||
|
},
|
||||||
|
link: {
|
||||||
|
type: String
|
||||||
|
},
|
||||||
|
date: {
|
||||||
|
type: Date
|
||||||
|
},
|
||||||
|
photo: {
|
||||||
|
type: String
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
collection: 'posts'
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = mongoose.model('Post', Post);
|
||||||
181
post.route.js
Normal file
181
post.route.js
Normal file
@@ -0,0 +1,181 @@
|
|||||||
|
const utils = require('./utils');
|
||||||
|
const express = require('express');
|
||||||
|
const postRoutes = express.Router();
|
||||||
|
|
||||||
|
let Post = require('./post.model');
|
||||||
|
let Category = require('./category.model');
|
||||||
|
let Session = require('./session.model');
|
||||||
|
let User = require('./user.model');
|
||||||
|
|
||||||
|
/*
|
||||||
|
POST - /post/create
|
||||||
|
Create a post
|
||||||
|
Response: 201 - Created
|
||||||
|
401 - Unauthorized
|
||||||
|
*/
|
||||||
|
postRoutes.route('/create').post((req, res) => {
|
||||||
|
if (!req.body) {
|
||||||
|
res.status(401).send("Missing body");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
utils.checkSession(req.body.userId, req.body.sessionId, (isValidId) => {
|
||||||
|
utils.isAdmin(req.body.userId, (isAdmin) => {
|
||||||
|
if (isValidId && isAdmin) {
|
||||||
|
let p = new Post(req.body);
|
||||||
|
p.date = utils.dateToEpoch(p.date);
|
||||||
|
p.save()
|
||||||
|
.then(() => {
|
||||||
|
res.json(p);
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
console.log(e);
|
||||||
|
res.status(500).send("Error creating post");
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
res.status(401).send("Invalid permissions to create post.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
POST - /post/delete
|
||||||
|
Create a post
|
||||||
|
Response: 200 - Deleted
|
||||||
|
401 - Unauthorized
|
||||||
|
*/
|
||||||
|
postRoutes.route('/delete').post((req, res) => {
|
||||||
|
if (!req.body) {
|
||||||
|
res.status(401).send("Missing body");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
utils.checkSession(req.body.userId, req.body.sessionId, (isValidId) => {
|
||||||
|
utils.isAdmin(req.body.userId, (isAdmin) => {
|
||||||
|
if (isValidId && isAdmin) {
|
||||||
|
Post.findByIdAndDelete(req.body._id, (err, r) => {
|
||||||
|
if (err) {
|
||||||
|
res.status(500).send("Error deleting post");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res.status(200).send("Deleted post");
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
res.status(401).send("Invalid permissions to delete post.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
POST - /post/edit
|
||||||
|
Create a post
|
||||||
|
Response: 200 - Edited
|
||||||
|
401 - Unauthorized
|
||||||
|
*/
|
||||||
|
postRoutes.route('/edit').post((req, res) => {
|
||||||
|
if (!req.body) {
|
||||||
|
res.status(401).send("Missing body");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
utils.checkSession(req.body.userId, req.body.sessionId, (isValidId) => {
|
||||||
|
utils.isAdmin(req.body.userId, (isAdmin) => {
|
||||||
|
if (isValidId && isAdmin) {
|
||||||
|
Post.findById(req.body._id, (err, r) => {
|
||||||
|
if (err) {
|
||||||
|
res.status(500).send("Error editing post");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
r.save()
|
||||||
|
.then(() => {
|
||||||
|
res.json(r);
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
console.log(e);
|
||||||
|
res.status(500).send("Error creating post");
|
||||||
|
});
|
||||||
|
res.status(200).send("Edited post");
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
res.status(401).send("Invalid permissions to delete post.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
POST - /post/id
|
||||||
|
Get post by id
|
||||||
|
Response: 200 - OK
|
||||||
|
*/
|
||||||
|
postRoutes.route('/id').post((req, res) => {
|
||||||
|
Post.findById(req.body._id, (err, post) => {
|
||||||
|
if (err) {
|
||||||
|
console.log(err);
|
||||||
|
res.status(500).send("Error getting posts");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res.status(200).send(post);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
POST - /post/date
|
||||||
|
Get post by date
|
||||||
|
Response: 200 - OK
|
||||||
|
*/
|
||||||
|
postRoutes.route('/date').post((req, res) => {
|
||||||
|
let d = new Date(req.body.date);
|
||||||
|
d = utils.dateToEpoch(d);
|
||||||
|
Post.find({ date: d}, (err, post) => {
|
||||||
|
if (err) {
|
||||||
|
console.log(err);
|
||||||
|
res.status(500).send("Error getting posts");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res.status(200).send(post);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
GET - /post/all
|
||||||
|
Get all posts
|
||||||
|
Response: 200 - OK
|
||||||
|
*/
|
||||||
|
postRoutes.route('/all').get((req, res) => {
|
||||||
|
Post.find({}, (err, postArr) => {
|
||||||
|
if (err) {
|
||||||
|
console.log(err);
|
||||||
|
res.status(500).send("Error getting posts");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res.status(200).send(postArr);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
GET - /posts/today
|
||||||
|
Get the posts for today
|
||||||
|
Response: 200 - Removed session
|
||||||
|
400 - No session exists
|
||||||
|
*/
|
||||||
|
postRoutes.route('/today').get((req, res) => {
|
||||||
|
let date = new Date();
|
||||||
|
date = utils.dateToEpoch(date);
|
||||||
|
Post.find({ date: date }, (err, postArr) => {
|
||||||
|
if (err) {
|
||||||
|
console.log(err);
|
||||||
|
res.status(500).send("Error getting posts");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res.status(200).send(postArr);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = postRoutes;
|
||||||
@@ -11,6 +11,9 @@ let User = new Schema({
|
|||||||
},
|
},
|
||||||
password: {
|
password: {
|
||||||
type: String
|
type: String
|
||||||
|
},
|
||||||
|
permission: {
|
||||||
|
type: Number
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
collection: 'users'
|
collection: 'users'
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ userRoutes.route('/create').post((req, res) => {
|
|||||||
res.status(409).send("Account already exists");
|
res.status(409).send("Account already exists");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
u.permission = 0;
|
||||||
u.save()
|
u.save()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
res.status(201).send("Success creating user");
|
res.status(201).send("Success creating user");
|
||||||
@@ -66,6 +67,7 @@ userRoutes.route('/create').post((req, res) => {
|
|||||||
401 - Incorrect
|
401 - Incorrect
|
||||||
*/
|
*/
|
||||||
userRoutes.route('/login').post((req, res) => {
|
userRoutes.route('/login').post((req, res) => {
|
||||||
|
console.log(req.body);
|
||||||
if (!req.body) {
|
if (!req.body) {
|
||||||
res.status(401).send("Missing body");
|
res.status(401).send("Missing body");
|
||||||
return;
|
return;
|
||||||
|
|||||||
38
utils.js
38
utils.js
@@ -95,6 +95,44 @@ const sendMail = async (user, message, replacements) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// checkSession(userId, sessionId) checks if the sessionId is valid for the user
|
||||||
|
const checkSession = (userId, sessionId, f) => {
|
||||||
|
Session.find({userId: userId, sessionId: sessionId }, (err, res) => {
|
||||||
|
if (res) {
|
||||||
|
f(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
f(false);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// isAdmin(userId) checks if the user with userId is an administrator
|
||||||
|
const isAdmin = (userId, f) => {
|
||||||
|
User.findById(userId, (err, res) => {
|
||||||
|
if(res.permission == 1) {
|
||||||
|
f(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
f(false);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// dateToEpoch(date) change the time of the date object to epoch
|
||||||
|
function dateToEpoch(d) {
|
||||||
|
console.log(d);
|
||||||
|
if (d) {
|
||||||
|
// When comparing js dates, the timezone does not matter
|
||||||
|
// ex. May 17 EDT == May 17 GMT, May 17 EDT != May 18 GMT
|
||||||
|
return d.setHours(0,0,0,0);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
module.exports.purgeSessions = purgeSessions;
|
module.exports.purgeSessions = purgeSessions;
|
||||||
module.exports.loadDefaultTemplates = loadDefaultTemplates;
|
module.exports.loadDefaultTemplates = loadDefaultTemplates;
|
||||||
module.exports.sendMail = sendMail;
|
module.exports.sendMail = sendMail;
|
||||||
|
module.exports.checkSession = checkSession;
|
||||||
|
module.exports.isAdmin = isAdmin;
|
||||||
|
module.exports.dateToEpoch = dateToEpoch;
|
||||||
|
|||||||
Reference in New Issue
Block a user