Fixed issues with category routes and worked on post routes

This commit is contained in:
2023-02-02 15:26:47 -05:00
parent 555fc37047
commit 24e2326dc4
11 changed files with 513 additions and 53 deletions

View File

@@ -1 +1,24 @@
const backendURL = "http://127.0.0.1:8080";
function setCookie(cname, cvalue, exdays) {
const d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
let expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}

View File

@@ -1,10 +1,12 @@
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
@@ -62,11 +64,17 @@
},
body: JSON.stringify({ email: email, password: password })
});
const content = await rawResponse.text();
const content = await rawResponse.json();
console.log(content);
if (content.success) {
setCookie("userId", content.response.userId, 1);
setCookie("sessionId", content.response.sessionId, 1);
window.location.replace("/manage/manageHome.html");
} else {
alert("Incorrect email/password.");
}
})();
}
</script>

View File

@@ -0,0 +1,156 @@
<html>
<head>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Kno-Logic Management Portal</h1>
<table border="black">
<th colspan="4">
Manage Categories
</th>
<tr>
<td>
<a href="/manage/manageHome.html">Home</a>
</td>
<td>
<a href="/manage/managePosts.html">Manage Posts</a>
</td>
<td>
<a href="/manage/manageCategories.html">Manage Categories</a>
</td>
<td>
<a href="/manage/manageHome.html">Analytics</a>
</td>
</tr>
</table>
<br>
<table border="black">
<th colspan="2">
Add Category
</th>
<tr>
<td>
<label for="name">Category Name: </label>
</td>
<td>
<input type="text" id="name" name="name">
</td>
</tr>
<tr>
<td>
<label for="color">Category Color: </label>
</td>
<td>
<input type="color" id="color" name="color">
</td>
</tr>
<tr>
<td colspan="2">
<center><button type="button" onclick="addCategory()">Add Category</button></center>
</td>
</tr>
</table>
<br>
<table border="black" id="listTable">
<th colspan="2">
List Categories
</th>
<tr>
<td>
<strong>Name</strong>
</td>
<td>
<strong>Color</strong>
</td>
</th>
<tbody>
</tbody>
</table>
</center>
</body>
<script src="./consts.js"></script>
<script>
function verifySession() {
let userId = getCookie("userId");
let sessionId = getCookie("sessionId");
(async () => {
const rawResponse = await fetch(backendURL + '/v1/user/check-token', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({ userId: userId, sessionId: sessionId })
});
const content = await rawResponse.json();
if (!content.success) {
window.location.replace("/manage/index.html");
}
})();
}
function getCategories() {
(async () => {
const rawResponse = await fetch(backendURL + '/v1/category/all', {
method: 'GET',
});
const content = await rawResponse.json();
var tbodyRef = document.getElementById('listTable').getElementsByTagName('tbody')[0];
content.response.forEach(element => {
var newRow = tbodyRef.insertRow();
newRow.innerHTML = `<td>${element.name}</td><td><input type='color' value='${element.color}' disabled></td>`;
});
})();
}
function addCategory() {
let userId = getCookie("userId");
let sessionId = getCookie("sessionId");
let name = document.getElementById("name").value;
let color = document.getElementById("color").value;
(async () => {
const rawResponse = await fetch(backendURL + '/v1/category/create', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({ userId: userId, sessionId: sessionId, category: { name: name, color: color } })
});
const content = await rawResponse.json();
if (content.success) {
window.location.reload();
} else {
alert("Could not add category: " + content.response);
}
console.log(content);
})();
}
verifySession();
getCategories();
</script>
</html>

View File

@@ -1,10 +1,12 @@
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
@@ -35,25 +37,28 @@
<script src="./consts.js"></script>
<script>
function login() {
let email = document.getElementById("email").value;
let password = document.getElementById("password").value;
function verifySession() {
let userId = getCookie("userId");
let sessionId = getCookie("sessionId");
(async () => {
const rawResponse = await fetch(backendURL + '/v1/user/login', {
const rawResponse = await fetch(backendURL + '/v1/user/check-token', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({ email: email, password: password })
body: JSON.stringify({ userId: userId, sessionId: sessionId })
});
const content = await rawResponse.text();
console.log(content);
})();
const content = await rawResponse.json();
if (!content.success) {
window.location.replace("/manage/index.html");
}
})();
}
verifySession();
</script>
</html>

250
public/managePosts.html Normal file
View File

@@ -0,0 +1,250 @@
<html>
<head>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Kno-Logic Management Portal</h1>
<table border="black">
<th colspan="4">
Manage Posts
</th>
<tr>
<td>
<a href="/manage/manageHome.html">Home</a>
</td>
<td>
<a href="/manage/managePosts.html">Manage Posts</a>
</td>
<td>
<a href="/manage/manageCategories.html">Manage Categories</a>
</td>
<td>
<a href="/manage/manageHome.html">Analytics</a>
</td>
</tr>
</table>
<br>
<table border="black">
<th colspan="2">
Add Post
</th>
<tr>
<td>
<label for="title">Title: </label>
</td>
<td>
<input type="text" id="title" name="title">
</td>
</tr>
<tr>
<td>
<label for="description">Description: </label>
</td>
<td>
<textarea id="description" name="description"></textarea>
</td>
</tr>
<tr>
<td>
<label for="author">Author: </label>
</td>
<td>
<input type="text" id="author" name="author">
</td>
</tr>
<tr>
<td>
<label for="category">Category (hold <code>CTRL</code> to select multiple): </label>
</td>
<td>
<select name="category" id="category" style="width: 100%; height: 100px;" multiple>
</select>
</td>
</tr>
<tr>
<td>
<label for="link">Link: </label>
</td>
<td>
<input type="text" id="link" name="link">
</td>
</tr>
<tr>
<td>
<label for="date">Date: </label>
</td>
<td>
<input type="date" id="date" name="date">
</td>
</tr>
<tr>
<td colspan="2">
<center><button type="button" onclick="addPost()">Add Post</button></center>
</td>
</tr>
</table>
<br>
<table border="black" id="listTable">
<th colspan="9">
List Posts
</th>
<tr>
<td>
<strong>Title</strong>
</td>
<td>
<strong>Description</strong>
</td>
<td>
<strong>Author</strong>
</td>
<td>
<strong>Category</strong>
</td>
<td>
<strong>Link</strong>
</td>
<td>
<strong>Date</strong>
</td>
<td>
<strong>Photo</strong>
</td>
<td>
<strong>Edit</strong>
</td>
<td>
<strong>Delete</strong>
</td>
</th>
<tbody>
</tbody>
</table>
</center>
</body>
<script src="./consts.js"></script>
<script>
function verifySession() {
let userId = getCookie("userId");
let sessionId = getCookie("sessionId");
(async () => {
const rawResponse = await fetch(backendURL + '/v1/user/check-token', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({ userId: userId, sessionId: sessionId })
});
const content = await rawResponse.json();
if (!content.success) {
window.location.replace("/manage/index.html");
}
})();
}
function getCategories() {
(async () => {
const rawResponse = await fetch(backendURL + '/v1/category/all', {
method: 'GET',
});
const content = await rawResponse.json();
var categoryRef = document.getElementById('category');
content.response.forEach(element => {
categoryRef.appendChild(new Option(element.name, element._id))
});
})();
}
function getPosts() {
(async () => {
const rawResponse = await fetch(backendURL + '/v1/post/all', {
method: 'GET',
});
const content = await rawResponse.json();
console.log(content);
var tbodyRef = document.getElementById('listTable').getElementsByTagName('tbody')[0];
content.response.forEach(element => {
var newRow = tbodyRef.insertRow();
newRow.innerHTML = `<td>${element.title}</td><td>${element.description}</td><td>${element.author}</td><td>${element.category.toString()}</td><td>${element.link}</td><td>${element.date}</td><td>${element.photo} <br> Edit Photo</td><td>Edit</td><td>Delete</td>`;
});
})();
}
function addPost() {
let userId = getCookie("userId");
let sessionId = getCookie("sessionId");
let title = document.getElementById("title").value;
let description = document.getElementById("description").value;
let author = document.getElementById("author").value;
let category = getSelectValues(document.getElementById("category"));
let link = document.getElementById("link").value;
let date = document.getElementById("date").value;
console.log(category);
(async () => {
const rawResponse = await fetch(backendURL + '/v1/post/create', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({ userId: userId, sessionId: sessionId,
post: { title: title, description: description, author: author, category: category, link: link, date: date } })
});
const content = await rawResponse.json();
if (content.success) {
window.location.reload();
} else {
alert("Could not add post: " + content.response);
}
})();
}
function getSelectValues(select) {
let result = [];
let options = select && select.options;
let opt;
for (let i = 0, iLen = options.length; i < iLen; i++) {
opt = options[i];
if (opt.selected) {
result.push(opt.value || opt.text);
}
}
return result;
}
verifySession();
getCategories();
getPosts();
</script>
</html>

View File

@@ -1,10 +1,12 @@
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>

View File

@@ -21,7 +21,7 @@ categoryRoutes.route('/create').post((req, res) => {
utils.account.checkSession(req.body.userId, req.body.sessionId, (isValidId) => {
utils.account.isAdmin(req.body.userId, (isAdmin) => {
if (isValidId && isAdmin) {
let c = new Category(req.body);
let c = new Category(req.body.category);
c.save()
.then(() => {
res.status(200).json({ success: true, response: c });

View File

@@ -21,7 +21,7 @@ postRoutes.route('/create').post((req, res) => {
utils.account.checkSession(req.body.userId, req.body.sessionId, (isValidId) => {
utils.account.isAdmin(req.body.userId, (isAdmin) => {
if (isValidId && isAdmin) {
let p = new Post(req.body);
let p = new Post(req.body.post);
p.date = utils.date.dateToEpoch(p.date);
p.save()
.then(() => {

View File

@@ -243,6 +243,17 @@ userRoutes.route('/favorite/get').post((req, res) => {
})
})
userRoutes.route('/check-token').post((req, res) => {
utils.account.checkSession(req.body.userId, req.body.sessionId, valid => {
if (valid) {
res.status(200).json({ success: true, response: "Valid SessionId" });
} else {
res.status(401).json({ success: false, response: "Incorrect SessionId" });
}
});
});
userRoutes.route('/refresh').post((req, res) => {
utils.account.checkRefresh(req.body.userId, req.body.refresh, valid => {
if (valid) {

View File

@@ -4,11 +4,16 @@ const bcrypt = require('bcrypt');
// checkSession(userId, sessionId) checks if the sessionId is valid for the user
const checkSession = (userId, sessionId, f) => {
let success = false;
Session.find({ userId: userId, sessionId: sessionId }, (err, res) => {
if (res && res.type == 0) {
res.forEach(element => {
if (element.type == 0 && !success) {
success = true;
f(true);
return;
}
})
if (!success)
f(false);
});
}

0
utils/viewcount.js Normal file
View File