var db = require('../../db/db');
var _ = require('underscore');

//Controller method - retrieve joined listing/user/category fields from DB
exports.getAll = function(category, callback) {
  // Listing.findAll({ order: ['createdAt', 'DESC'] })
  db.Listing.findAll({
    include:
    [{
      model: db.Category,
      attributes: ['categoryName'],
      where: {categoryName: category},
    },
    {
      model: db.User,
      attributes: ['firstName', 'lastName', 'phone', 'email', 'profilePic']
    },
    {
      model: db.Image,
      attributes: ['path']
    }],
    order: 'createdAt DESC'
  })
    .then(function(listings) {
      callback(200, listings);
    })
    .catch(function(error) {
      console.error(error);
      callback(404, error);
    });
};

// Controller method - get filtered results
exports.getFiltered = function(filters, callback) {
  // constructing where object body
  const filteredWhere = {};
  if (Object.keys(filters).length <= 1) {
    filteredWhere = {};
  } else {
    // filter price
    // ********** RENT ********** \\
    if (filters.price !== undefined) {
      if (filters.price[0] !== undefined && filters.price[1] !== undefined) {
        price = {$gte: filters.price.split(',')[0], $lte: filters.price.split(',')[1]};
      } else {
        price = {$gte: filters.price.split(',')[0]};
      }
      filteredWhere.price = price;
      console.log(filteredWhere.price);
    }
    // filter location
    if (filters.location !== undefined) { filteredWhere.location = filters.location; }
  }
  db.Listing.findAll({
    include:
    [{
      model: db.Category,
      attributes: ['categoryName'],
      where: {categoryName: filters.category},
    },
    {
      model: db.User,
      attributes: ['username', 'phone', 'email']
    },
    {
      model: db.Image,
      attributes: ['path']
    }],
    order: 'createdAt DESC',
    where: filteredWhere, raw: true
  })
    .then(function(listings) {
      callback(200, listings);
      console.log('$$$$listings', listings);
    })
    .catch(function(error) {
      console.error(error);
      callback(404, error);
    });
};

//Controller method - add a listings to DB
exports.addOne = function(listing, images, callback) {

  db.Listing.create(listing)
    .then(function(listing) {
      var img = {};

      if (images.length > 0) {
        _.each(images, function(image) {
          img.path = 'uploads/' + image.filename;
          img.listingId = listing.listingId;
        });
      } else {
        img.path = 'uploads/default-img.png';
        img.listingId = listing.listingId;
      }
      db.Image.create(img)
        .then(function(image) {
          console.log('Image upload successful');
        })
        .catch(function(error) {
          console.error('image upload error', error);
        });

      callback(201, listing);
    })
    .catch(function(error) {
      console.error('posting new one error', error);
      callback(404, error);
    });
};


@@ -2119,16 +2119,59 @@
 %7D);%0A%7D;%0A%0A
+//Controller method - add a listings to DB%0A
 //Contro
@@ -2260,17 +2260,16 @@
 back) %7B%0A
-%0A
   db.Lis
@@ -2323,29 +2323,8 @@
 ) %7B%0A
-      var img = %7B%7D;%0A%0A
     
@@ -2397,34 +2397,53 @@
 %7B%0A          
-img.
+var img = %7B%0A            
 path
- =
+:
  'uploads/' 
@@ -2458,17 +2458,17 @@
 filename
-;
+,
 %0A       
@@ -2462,36 +2462,34 @@
 name,%0A          
-img.
+  
 listingId = list
@@ -2473,34 +2473,33 @@
        listingId
- =
+:
  listing.listing
@@ -2496,25 +2496,24 @@
 ng.listingId
-;
 %0A        %7D);
@@ -2513,124 +2513,17 @@
     
-%7D);%0A      %7D else %7B%0A        img.path = 'uploads/default-img.png';%0A        img.listingId = listing.listingId;%0A      %7D%0A
+  %7D;%0A    
     
@@ -2549,24 +2549,28 @@
 mg)%0A        
+   
+ 
 .then(functi
@@ -2573,32 +2573,36 @@
 nction(image) %7B%0A
+    
           consol
@@ -2639,27 +2639,35 @@
 ');%0A        
+ 
-%7D)%0A
+   %7D)%0A    
         .cat
@@ -2683,24 +2683,28 @@
 on(error) %7B%0A
+    
           co
@@ -2753,20 +2753,43 @@
         
-%7D);%0A
+    %7D);%0A        %7D);%0A      %7D
 %0A      c

Fix bugs