Skip to content

Instantly share code, notes, and snippets.

@in43sh
Created November 30, 2018 22:25
Show Gist options
  • Save in43sh/1072b08578567b0aa34feb937af906de to your computer and use it in GitHub Desktop.
Save in43sh/1072b08578567b0aa34feb937af906de to your computer and use it in GitHub Desktop.
const Properties = require('../models').Properties;
import { map } from 'lodash';
// const _ = require('lodash'); // causing error
const models = require ('../models/properties');
module.exports = {
createProperty: (req, res, next) => {
const { name, address, category, open_time, close_time, description, photo, geoloc } = req.body;
return Properties
.create({ name, address, category, open_time, close_time, description, photo, geoloc })
.then(property => res.status(200).json({ status: 'Created one property', property }))
.catch(error => console.log(error));
},
getAllProperties: (req, res, next) => {
return Properties
.findAll({paranoid: false})
.then(properties => res.status(200).json({ status: 'Retrieved all properties', properties }))
.catch(error => console.log(error));
},
getSingleProperty: (req, res, next) => {
const { id } = req.params;
return Properties
.findById(id)
.then(property => res.status(200).json({ status: 'Retrieved one property', property }))
.catch(error => console.log(error));
},
updateProperty: (req, res, next) => {
const { id } = req.params;
const { name, address, category, open_time, close_time, description, photo, geoloc } = req.body;
return Properties
.findById(id)
.then(property => {
if (!property) {
return res.status(404).send({ message: 'Property not found' })
}
return property
.update({
name: name,
address: address,
category: category,
open_time: open_time,
close_time: close_time,
description: description,
photo: photo,
geoloc: geoloc
})
.then(property => res.status(200).json({ status: 'Updated one property', property }))
.catch(error => console.log(error));
})
.catch(error => console.log(error));
},
destroyProperty: (req, res, next) => {
const { id } = req.params;
return Properties
.findById(id)
.then(property => {
if (!property) {
return res.status(404).send({ message: 'Property not found' })
}
return property
.destroy()
.then(() => res.status(200).json({ status: 'Deleted one property', property }))
.catch(error => console.log(error));
})
},
search: (req, res, next) => {
console.log("REQUEST BODEY",req.body);
const results = models.sequelize.query(`
SELECT *
FROM ${models.Properties.tableName}
WHERE _search @@ plainto_tsquery('english', :query);
`, {
model: models.Properties,
replacements: { query: req.query.query },
});
const properties = models.Properties.find({
where: {
created_at: {
$gte: '2018-01-01',
},
id: {
$in: map(results, 'id'),
},
},
});
return properties
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment