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",
|
"$http",
|
||||||
function($scope, $http)
|
function($scope, $http)
|
||||||
{
|
{
|
||||||
$scope.postTitle = "";
|
$scope.title = "";
|
||||||
$scope.postAuthor = "";
|
$scope.author = "";
|
||||||
$scope.postContent = "";
|
$scope.content = "";
|
||||||
$scope.postTags = "";
|
$scope.tags = [];
|
||||||
|
|
||||||
$scope.timestamp = currentTime() + " - " + currentDate();
|
|
||||||
|
|
||||||
$scope.postSubmit = function()
|
$scope.postSubmit = function()
|
||||||
{
|
{
|
||||||
// Push to the post service
|
// Create the post object
|
||||||
post = {
|
post = {
|
||||||
"postTitle": $scope.postTitle,
|
"title": $scope.title,
|
||||||
"postAuthor": $scope.postAuthor,
|
"author": $scope.author,
|
||||||
"postContent": $scope.postContent,
|
"content": $scope.content,
|
||||||
"postTags": $scope.postTags,
|
"tags": $scope.tags,
|
||||||
"postTime": currentTime(),
|
"date": new Date().getTime()
|
||||||
"postDate": currentDate()
|
|
||||||
};
|
};
|
||||||
|
|
||||||
$http.post("/writenews", post)
|
// POST it to the server
|
||||||
.then(
|
$http.post("/post", post);
|
||||||
(r) =>
|
|
||||||
{
|
|
||||||
if (r.status != 204) console.log("Failed to push new post (" + r.statusText + ")");
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// Clear the fields
|
// Clear the fields
|
||||||
$scope.postTitle = "";
|
$scope.title = "";
|
||||||
$scope.postAuthor = "";
|
$scope.author = "";
|
||||||
$scope.postContent = "";
|
$scope.content = "";
|
||||||
$scope.postTags = "";
|
$scope.tags = [];
|
||||||
|
|
||||||
// Tell the user that the deed is done *diabolical laughter*
|
// 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);
|
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')));
|
res.json(JSON.parse(fs.readFileSync('./data/posts.json')));
|
||||||
});
|
});
|
||||||
|
|
||||||
// News route, digest received post JSON
|
// Posts route, digest received post
|
||||||
app.post("/writenews", function(req, res) {
|
app.post("/posts", function(req, res) {
|
||||||
handleIncomingPost(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
|
// Start listening
|
||||||
@ -32,16 +54,16 @@ app.listen(8080, function (){
|
|||||||
console.log("Listening on port 8080");
|
console.log("Listening on port 8080");
|
||||||
});
|
});
|
||||||
|
|
||||||
// Process incoming posts
|
function validatePost(post)
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
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