const db = require('../../../database/schemas.js');

module.exports = {
  getOne: (req, res) => {
    db.Post.findOne({
      where: {
        id: req.params.listId,
      },
      include: [{
        model: db.Picture,
      }],
    })
      .then((listing) => {
        res.send(listing);
      });
  },

  getAll: (err, res) => {
    db.Post.findAll({})
      .then((posts) => {
        res.send(posts);
      });
  },

  getAllForUser: (req, res) => {
    db.User.find({ where: {id: req.params.id} })
      .then((user) => {
        user.getPosts({}).then((posts) => {
          res.json(posts);
        });
      });
  },

  createOne: (req, res) => {
    db.User.find({ where: { id: req.params.id } })
      .then((user) => {
        const newPost = {
          title: req.body.title,
          body: req.body.body,
          type: req.body.type,
          pictures: req.body.images,
          userId: user.id,
        };
        db.Post.create(newPost, { include: [db.Picture] })
          .then((post) => {
            res.json(post);
          });
      })
      .catch((err) => {
        res.status(400).json({ error: err });
      });
  },

  patchOne: (req, res) => {
    db.Post.update(req.body, {
      where: {
        id: req.params.listId,
      },
    })
      .then((status) => {
        res.send(status);
      });
  },

  deleteOne: (req, res) => {
    db.Post.destroy({
      where: {
        id: req.params.listId,
      },
    })
      .then((status) => {
        if (status === 1) {
          res.send('listing deleted!');
        } else {
          res.send('listing wasn\'t found');
        }
      });
  },

  getAllPhotos: (req, res) => {
    db.Picture.findAll({
      where: {
        post_id: req.params.listId,
      },
    })
      .then((photos) => {
        res.send(photos);
      });
  },

  createOnePhoto: (req, res) => {
    db.Picture.create({
      img_name: req.body.img_name,
      img_path: req.body.img_path,
      post_id: req.params.listId,
    })
      .then((photoData) => {
        res.send(photoData);
      });
  },

  patchOnePhoto: (req, res) => {
    db.Picture.update(
      {
        img_name: req.body.img_name,
        img_path: req.body.img_path,
      },
      {
        where: {
          id: req.params.id,
        },
      })
      .then((status) => {
        res.send(status);
      });
  },

  deleteOnePhoto: (req, res) => {
    db.Picture.destroy({
      where: {
        id: req.params.id,
      },
    })
      .then((status) => {
        if (status === 1) {
          res.send('photo deleted!');
        } else {
          res.send('photo wasn\'t found');
        }
      });
  },
};

@@ -347,16 +347,78 @@
 indAll(%7B
+%0A      where: %7B%7D,%0A      include: %5B%7B model: db.Picture %7D%5D,%0A    
 %7D)%0A     
@@ -521,32 +521,38 @@
   db.User.find(%7B
+%0A     
  where: %7Bid: req
@@ -544,16 +544,17 @@
 where: %7B
+ 
 id: req.
@@ -562,17 +562,23 @@
 arams.id
-%7D
+ %7D,%0A   
  %7D)%0A    
@@ -620,18 +620,82 @@
 tPosts(%7B
-%7D)
+%0A          include: %5B%7B model: db.Picture %7D%5D,%0A        %7D)%0A          
 .then((p
@@ -707,32 +707,34 @@
  =%3E %7B%0A          
+  
 res.json(posts);
@@ -730,24 +730,26 @@
 son(posts);%0A
+  
         %7D);%0A

Send associated pictures with listings