const vegetables = require('../models').vegetables const models = require('../models') const VegetableTypes = require('./VegetableTypes') const uuid = require('uuid/v4') const multer = require('multer') const path = require('path') const fs = require('fs') const Resize = require('../libs/resize') const Aws = require('../libs/aws') const Op = models.Sequelize.Op class Vegetables { constructor () { this._upload = multer({ storage: multer.diskStorage({}), fileFilter: function (req, file, cb) { const filetypes = /jpg|jpeg|png|JPG|JPEG|PNG/ const mimetype = filetypes.test(file.mimetype) const extname = filetypes.test(path.extname(file.originalname).toLowerCase()) if (mimetype && extname) { return cb(null, true) } cb(new Error('Error: File upload only supports the following filetypes - ' + filetypes)) } }).single('mainPicture') } _createItem (req, callback) { let newItem = req.body newItem.vegetableTypeId = req.params.vegetableTypesId vegetables.create(newItem) .then(item => { models.properties .findAndCountAll() .then(properties => { if (properties && properties.rows.length > 0) { let saved = 0 const _callback = () => { if (properties.count === saved) { callback(null, item) } } properties.rows.forEach(propperty => { models.vegetableProperties .create({ vegetableId: item.id, propertyId: propperty.id }) .then(() => { saved += 1 _callback() }) .catch((e) => { console.log(e) saved += 1 _callback() }) }) } else { callback(null, item) } }) .catch(e => { item.destroy() callback(e, 500) }) }) .catch(e => { callback(e, null) }) } _patchOne (item, values, callback) { item.update(values) .then(item => { callback(null, item) }) .catch(e => { callback(e, null) }) } _deleteOne (item, req, callback) { vegetables.destroy({ where: { id: req.params.vegetablesId } }) .then(deleted => { if (deleted === 0) { callback(new Error('Error when trying to delete item')) return false } callback(null, null) }) .catch(e => { callback(e, null) }) } _deleteMedias (path, callback) { const aws = new Aws() if (!callback) { callback = (e) => { if (e) { console.error(e) } } } aws.deleteObjects([path, path.replace('_large.', '_thumb.')], callback) } static getAll (req, callback) { vegetables.findAndCountAll({ where: { vegetableTypeId: req.params.vegetableTypesId }, include: ['Type'], order: [ ['name', 'ASC'] ] }) .then(items => { if (!items) { callback(new Error('No vegetable found'), 204) return false } callback(null, items) }) .catch((e) => { callback(e, null) }) } static searchAll (req, callback) { vegetables.findAndCountAll({ where: { name: { [Op.like]: `%${req.query.q}%` } }, include: ['Type'], order: [ ['name', 'ASC'] ] }) .then(items => { if (!items) { callback(new Error('No vegetable found'), 204) return false } callback(null, items) }) .catch((e) => { callback(e, null) }) } createOne (req, callback) { VegetableTypes.getOne(req, (err, universe) => { if (err) { callback(err, universe) return false } this._upload(req, req.body, (err) => { if (err) { callback(err, null) return false } if (req.file) { let uploaded = 0 let aws = new Aws() let thumb = new Resize() let large = new Resize() const key = uuid() let _create = () => { if (uploaded === 2) { this._createItem(req, callback) fs.unlink(req.file.path, () => { }) } } large.createLargeImage(req.file.path, (err, file) => { if (err) { callback(err, null) return false } aws.upload({ path: file.output, filename: `main_${key}_large.${req.file.originalname.split('.')[req.file.originalname.split('.').length - 1]}` }, (err, res) => { if (!err) { req.body.mainPicture = res.file uploaded += 1 _create() fs.unlink(file.output, () => { }) } }) }) thumb.createThumbnail(req.file.path, (err, file) => { if (err) { callback(err, null) return false } aws.upload({ path: file.output, filename: `main_${key}_thumb.${req.file.originalname.split('.')[req.file.originalname.split('.').length - 1]}` }, (err, res) => { if (!err) { uploaded += 1 _create() fs.unlink(file.output, () => { }) } }) }) } else { this._createItem(req, callback) } }) }) } static getOne (req, callback) { vegetables.find({ where: { vegetableTypeId: req.params.vegetableTypesId, id: req.params.vegetablesId }, include: [ 'Type', 'Pictures', { model: models.vegetableProperties, as: 'Properties', include: ['Property'] } ] }) .then(item => { if (!item) { callback(new Error('Vegetable not found'), 404) return false } callback(null, item) }) .catch((e) => { callback(e, null) }) } patchOne (req, callback) { Vegetables.getOne(req, (err, item) => { if (err) { callback(err, item) return false } this._upload(req, req.body, (err) => { if (err) { callback(err, null) return false } let values = req.body if (req.file) { let uploaded = 0 let aws = new Aws() let thumb = new Resize() let large = new Resize() const key = uuid() if (item.mainPicture !== null && item.mainPicture !== '') { this._deleteMedias(item.mainPicture) } let _patch = () => { if (uploaded === 2) { this._patchOne(item, values, callback) fs.unlink(req.file.path, () => { }) } } large.createLargeImage(req.file.path, (err, file) => { if (err) { callback(err, null) return false } aws.upload({ path: file.output, filename: `main_${key}_large.${req.file.originalname.split('.')[req.file.originalname.split('.').length - 1]}` }, (err, res) => { if (!err) { values.mainPicture = res.file uploaded += 1 _patch() fs.unlink(file.output, () => { }) } }) }) thumb.createThumbnail(req.file.path, (err, file) => { if (err) { callback(err, null) return false } aws.upload({ path: file.output, filename: `main_${key}_thumb.${req.file.originalname.split('.')[req.file.originalname.split('.').length - 1]}` }, (err, res) => { if (!err) { uploaded += 1 _patch() fs.unlink(file.output, () => { }) } }) }) } else { this._patchOne(item, values, callback) } }) }) } deleteOne (req, callback) { Vegetables.getOne(req, (err, item) => { if (err) { callback(err, item) return false } if (item.mainPicture !== null && item.mainPicture !== '') { if (!callback) { callback = (e) => { if (e) { console.error(e) } } } this._deleteMedias(item.mainPicture, (e, res) => { if (e) { callback(e, null) return false } this._deleteOne(item, req, callback) }) } else { this._deleteOne(item, req, callback) } }) } } module.exports = Vegetables