Rework post functionality
Conforms to the new post format and validates incoming posts
This commit is contained in:
parent
9608a9eea0
commit
3311a17b01
@ -32,41 +32,33 @@ app.controller("create",
|
||||
"$http",
|
||||
function($scope, $http)
|
||||
{
|
||||
$scope.postTitle = "";
|
||||
$scope.postAuthor = "";
|
||||
$scope.postContent = "";
|
||||
$scope.postTags = "";
|
||||
|
||||
$scope.timestamp = currentTime() + " - " + currentDate();
|
||||
$scope.title = "";
|
||||
$scope.author = "";
|
||||
$scope.content = "";
|
||||
$scope.tags = [];
|
||||
|
||||
$scope.postSubmit = function()
|
||||
{
|
||||
// Push to the post service
|
||||
// Create the post object
|
||||
post = {
|
||||
"postTitle": $scope.postTitle,
|
||||
"postAuthor": $scope.postAuthor,
|
||||
"postContent": $scope.postContent,
|
||||
"postTags": $scope.postTags,
|
||||
"postTime": currentTime(),
|
||||
"postDate": currentDate()
|
||||
"title": $scope.title,
|
||||
"author": $scope.author,
|
||||
"content": $scope.content,
|
||||
"tags": $scope.tags,
|
||||
"date": new Date().getTime()
|
||||
};
|
||||
|
||||
$http.post("/writenews", post)
|
||||
.then(
|
||||
(r) =>
|
||||
{
|
||||
if (r.status != 204) console.log("Failed to push new post (" + r.statusText + ")");
|
||||
}
|
||||
);
|
||||
// POST it to the server
|
||||
$http.post("/post", post);
|
||||
|
||||
// Clear the fields
|
||||
$scope.postTitle = "";
|
||||
$scope.postAuthor = "";
|
||||
$scope.postContent = "";
|
||||
$scope.postTags = "";
|
||||
$scope.title = "";
|
||||
$scope.author = "";
|
||||
$scope.content = "";
|
||||
$scope.tags = [];
|
||||
|
||||
// Tell the user that the deed is done *diabolical laughter*
|
||||
alert("Post has been submitted. Thank you!");
|
||||
// alert("Post has been submitted. Thank you!");
|
||||
}
|
||||
}
|
||||
]
|
||||
@ -143,25 +135,3 @@ app.filter("markdown", () =>
|
||||
return marked(input);
|
||||
}
|
||||
});
|
||||
|
||||
function currentTime()
|
||||
{
|
||||
var date = new Date();
|
||||
var timestr = "";
|
||||
timestr += date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
|
||||
timestr += ":";
|
||||
timestr += date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
|
||||
return timestr;
|
||||
}
|
||||
|
||||
function currentDate()
|
||||
{
|
||||
var date = new Date();
|
||||
var datestr = "";
|
||||
datestr += date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
|
||||
datestr += "/";
|
||||
datestr += (date.getMonth()+1) < 10 ? "0" + (date.getMonth()+1) : (date.getMonth()+1);
|
||||
datestr += "/";
|
||||
datestr += date.getFullYear();
|
||||
return datestr;
|
||||
}
|
||||
|
||||
50
server.js
50
server.js
@ -22,9 +22,31 @@ app.get("/posts", function(req, res) {
|
||||
res.json(JSON.parse(fs.readFileSync('./data/posts.json')));
|
||||
});
|
||||
|
||||
// News route, digest received post JSON
|
||||
app.post("/writenews", function(req, res) {
|
||||
handleIncomingPost(req, res);
|
||||
// Posts route, digest received post
|
||||
app.post("/posts", function(req, res) {
|
||||
var postsFile = fs.readFileSync('./data/posts.json');
|
||||
var posts = JSON.parse(postsFile);
|
||||
|
||||
var incomingData = req.body;
|
||||
if (validatePost(incomingData))
|
||||
{
|
||||
posts.unshift(incomingData);
|
||||
}
|
||||
else
|
||||
{
|
||||
res.status(400).json({"error":"Failed to validate post"});
|
||||
return;
|
||||
}
|
||||
|
||||
fs.writeFile('./data/posts.json', JSON.stringify(posts), (e) => {
|
||||
if (e)
|
||||
{
|
||||
res.sendStatus(500);
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
|
||||
res.sendStatus(204);
|
||||
});
|
||||
|
||||
// Start listening
|
||||
@ -32,16 +54,16 @@ app.listen(8080, function (){
|
||||
console.log("Listening on port 8080");
|
||||
});
|
||||
|
||||
// Process incoming posts
|
||||
function handleIncomingPost(req, res){
|
||||
var newData = req.body;
|
||||
console.log("Request object:");
|
||||
console.log(newData);
|
||||
res.status(204).send();
|
||||
}
|
||||
|
||||
// Read and return current posts
|
||||
function handleGetPosts(req, res)
|
||||
function validatePost(post)
|
||||
{
|
||||
res.json({});
|
||||
try
|
||||
{
|
||||
// Check types of each expected property and assume they're there
|
||||
return (typeof post.title === "string" && typeof post.author === "string" && typeof post.date === "number" && Array.isArray(post.tags) && typeof post.content === "string");
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
// Object clearly isn't what we're expecting
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user