const { join } = require("path");
const sanitize = require("mongo-sanitize");
const { Types } = require("mongoose");
const BlogPost = require("../models/blogpost");

module.exports = function oauth(app) {

  app.get("/api/blog/posts/:limit?", (req, res) => {
    console.log(req.params.limit);
    if(req.params.limit && !+req.params.limit > 0) {
      res.sendStatus(400);
      return;
    }
    const limit = req.params.limit ? +req.params.limit : false
    BlogPost.find({}, {title: 1, description: 1, image: 1, author: 1, prettyURL: 1})
    .sort({"createdTS": -1})
    .limit(limit)
    .exec()
    .then(posts => {
      res.json(posts);
    })
    .catch(e => {
      res.sendStatus(500);
    });
  });

  app.get("/api/blog/post/:postid", (req, res) => {
    BlogPost.findOne({
      prettyURL: req.params.postid
    })
    .then(post => {
      if(!post) {
        res.sendStatus(404);
      } else {
        res.json(post._doc);
      }
    })
    .catch(e => {
      res.sendStatus(500);
    });
  });

};

@@ -530,16 +530,30 @@
 ettyURL:
+ 1, createdTS:
  1%7D)%0A   
@@ -1022,11 +1022,401 @@
 %0A  %7D);%0A%0A
+  // app.put(%22/api/blog/post/:postid%22, (req, res) =%3E %7B%0A  //   BlogPost.update(%7B%0A  //     _id: new Types.ObjectId(req.params.postid)%0A  //   %7D, %7B%0A  //     content%0A  //   %7D)%0A  //   .then(post =%3E %7B%0A  //     if(!post) %7B%0A  //       res.sendStatus(404);%0A  //     %7D else %7B%0A  //       res.json(post._doc);%0A  //     %7D%0A  //   %7D)%0A  //   .catch(e =%3E %7B%0A  //     res.sendStatus(500);%0A  //   %7D);%0A  // %7D);%0A%0A
 %7D;%0A

return timestamp in blogpost list