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) => {
          return bufferConverter.convertCollectionOfBuffersto64Array(photos);
      })
      .then((photos) => {
        res.render('photo-list', {
          photos
        });
    },
    getTrendingPhotos(req, res) {
      data.getTrendingPhotos(listCount)
        .then((photos) => {
          res.render('photo-list', {
            photos
          });
        });
    },
    postComment(req, res) {
      if (req.isAuthenticated()) {
        data.createComment(req.params.id, req.body.content, req.user)
          .then((successPhoto) => {
            res.send;
            res.redirect(`/photo/details/${req.params.id}`);
          });
      }
    },
    putUpvote(req, res) {
      if (req.isAuthenticated()) {
        data.upvote(req.params.id, req.user)
          .then((successPhoto) => {
            res.send;
            res.redirect(`/photo/details/${req.params.id}`);
          });
      }
    },
    removeUpvote(req, res) {
      if (req.isAuthenticated()) {
        data.unvote(req.params.id, req.user)
          .then((successPhoto) => {
            res.send;
            res.redirect(`/photo/details/${req.params.id}`);
          });
      }
    },
    getEdit(req, res) {
      if (!req.isAuthenticated()) {
        res.redirect('/login');
      }

      let foundPhoto;

      data.getPhotoById(req.params.id)
        .then((photo) => {
          if (photo.author !== req.user.email) {
            res.redirect(`/photo/details/${req.params.id}`);
          }

          foundPhoto = photo;

          return bufferConverter.convertBufferTo64Array(photo.data);
        })
        .then((convertedString) => {
          const photoModel = {
            contentType: foundPhoto.contentType,
            data: convertedString,
            votes: foundPhoto.upvotes.length,
            comments: foundPhoto.comments,
            date: foundPhoto.date,
            author: foundPhoto.author,
            id: foundPhoto._ids
          };

          res.render('edit', {
            photo: photoModel
          });
        })
        .catch(
          (err) => {
            console.log(err);
          }
        );
    }
  };
};

@@ -1154,32 +1154,34 @@
 istCount)%0A      
+  
 .then((photos) =
@@ -1185,27 +1185,8 @@
 ) =%3E
- %7B%0A          return
  buf
@@ -1245,19 +1245,12 @@
 tos)
-;
+)
 %0A  
-    %7D)%0A
     
@@ -1263,32 +1263,34 @@
 n((photos) =%3E %7B%0A
+  
         res.rend
@@ -1310,39 +1310,55 @@
 t', %7B%0A          
+  
 photos%0A
+          %7D);%0A
         %7D);%0A    

Fix broken typo