const vegetables = require('../models').vegetables const VegetableTypes = require('./VegetableTypes') const uuid = require('uuid/v4') const multer = require('multer') const path = require('path') const fs = require('fs') const Aws = require('../libs/aws') 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 => { callback(null, item) }) .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], callback) } static getAll (req, callback) { vegetables.findAndCountAll({ where: { vegetableTypeId: req.params.vegetableTypesId }, 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 aws = new Aws() aws.upload({ path: req.file.path, filename: `main_${uuid()}.${req.file.originalname.split('.')[req.file.originalname.split('.').length - 1]}` }, (err, res) => { if (!err) { req.body.mainPicture = res.file } this._createItem(req, callback) fs.unlink(req.file.path, () => {}) }) } else { this._createItem(req, callback) } }) }) } static getOne (req, callback) { vegetables.find({ where: { vegetableTypeId: req.params.vegetableTypesId, id: req.params.vegetablesId }, include: ['Type'] }) .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 aws = new Aws() if (item.mainPicture !== null && item.mainPicture !== '') { this._deleteMedias(item.mainPicture) } aws.upload({ path: req.file.path, filename: `main_${req.params.vegetablesId}${path.extname(req.file.originalname).toLowerCase()}` }, (err, res) => { if (!err) { values.mainPicture = res.file } this._patchOne(item, values, callback) fs.unlink(req.file.path, () => {}) }) } 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 !== '') { const aws = new Aws() 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