const bufferConverter = require('../utils/buffer-converter');

const listCount = 5;

module.exports = function (data) {
  return {
    getPhotoDetails(req, res) {
      let foundPhoto = {};

      data.getPhotoById(req.params.id)
        .then((photo) => {
          foundPhoto = photo;

          return bufferConverter.convertBufferTo64Array(photo.data);
        })
        .then((convertedString) => {
          let canUpvote = false;
          if (req.user) {
            canUpvote = !foundPhoto.upvotes.find(v => v.user === req.user.email);
          }

          const photoModel = {
            contentType: foundPhoto.contentType,
            data: convertedString,
            canUpvote,
            votes: foundPhoto.upvotes.length,
            comments: foundPhoto.comments,
            date: foundPhoto.date,
            author: foundPhoto.author,
            id: foundPhoto._id,
            hasUser: !!req.user
          };

          res.render('photo-details', {
            photoModel
          });
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getHotPhotos(req, res) {
      data.getHotPhotos(listCount)
      .then((photos) => {
        res.render('photo-list', {
          photos
        });
      });
    },
    getTrendingPhotos(req, res) {
      data.getTrendingPhotos(listCount)
      .then((photos) => {
        res.render('photo-list', {
          photos
        });
      });
    },
    postComment(req, res) {
      data.createComment(req.params.id, req.body.content, req.user)
        .then((successPhoto) => {
          res.redirect(`/photo/details/${req.params.id}`);
        });
    },
    putUpvote(req, res) {
      data.upvote(req.params.id, req.user)
      .then((successPhoto) => {
        res.send;
        res.redirect(`/photo/details/${req.params.id}`);
      });
    },
    removeUpvote(req, res) {
      data.unvote(req.params.id, req.user)
       .then((successPhoto) => {
         res.send;
         res.redirect(`/photo/details/${req.params.id}`);
       });
    }
  };
};

@@ -1464,32 +1464,69 @@
 ent(req, res) %7B%0A
+      if (req.isAuthenticated()) %7B%0A  
       data.creat
@@ -1573,24 +1573,24 @@
 , req.user)%0A
-
         .the
@@ -1674,32 +1674,40 @@
 %60);%0A        %7D);%0A
+      %7D%0A
     %7D,%0A    putUp
@@ -1715,32 +1715,69 @@
 ote(req, res) %7B%0A
+      if (req.isAuthenticated()) %7B%0A  
       data.upvot
@@ -1799,24 +1799,24 @@
 , req.user)%0A
-
       .then(
@@ -1912,32 +1912,40 @@
 d%7D%60);%0A      %7D);%0A
+      %7D%0A
     %7D,%0A    remov
@@ -1956,32 +1956,69 @@
 ote(req, res) %7B%0A
+      if (req.isAuthenticated()) %7B%0A  
       data.unvot
@@ -2040,24 +2040,24 @@
 , req.user)%0A
-
        .then
@@ -2150,24 +2150,24 @@
 rams.id%7D%60);%0A
-
        %7D);%0A 
@@ -2161,24 +2161,32 @@
 %0A       %7D);%0A
+      %7D%0A
     %7D%0A  %7D;%0A%7D

Add authentication for commenting and upvote