blog.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const Blog = require("../models/blog.js");
  2. const Uploader = require("../models/uploader.js");
  3. module.exports = {
  4. /*
  5. POST: create a new blog
  6. req.body = {
  7. uploader: String (uploader id)
  8. password: String
  9. title: String
  10. tags: String
  11. blog: String
  12. }
  13. redirect to home
  14. */
  15. create: function(req, res){
  16. Uploader.findOne({_id: req.body.uploader})
  17. .then((uploader)=>{
  18. if(uploader === "none") throw "uploader";
  19. if(req.body.password !== uploader.password) throw "pass";
  20. let blog = new Blog({
  21. writer: uploader._id,
  22. title: req.body.title,
  23. tags: req.body.tags.split(","),
  24. article: req.body.blog
  25. });
  26. return blog.save();
  27. })
  28. .then((blog)=>{
  29. return res.redirect("/");
  30. })
  31. .catch((err)=>{
  32. console.log(err);
  33. });
  34. },
  35. /*
  36. GET: get a list of all blogs
  37. response = [Blog]
  38. */
  39. getBlogs: function(req, res){
  40. Blog.aggregate([{$project: {article: 0}}])
  41. .then((blogs)=>{
  42. return res.json(blogs);
  43. })
  44. .catch((err)=>{
  45. return res.json("ERROR: could not retrieve blog data");
  46. });
  47. }
  48. }