2018-09-08 00:00:47 +02:00
|
|
|
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')
|
2018-09-19 22:08:11 +02:00
|
|
|
const Resize = require('../libs/resize')
|
2018-09-08 00:00:47 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 22:08:11 +02:00
|
|
|
aws.deleteObjects([path, path.replace('_large.', '_thumb.')], callback)
|
2018-09-08 00:00:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static getAll (req, callback) {
|
|
|
|
vegetables.findAndCountAll({
|
|
|
|
where: {
|
|
|
|
vegetableTypeId: req.params.vegetableTypesId
|
|
|
|
},
|
2018-09-09 18:15:19 +02:00
|
|
|
include: ['Type'],
|
2018-09-08 00:00:47 +02:00
|
|
|
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) {
|
2018-09-19 22:08:11 +02:00
|
|
|
let uploaded = 0
|
2018-09-08 00:00:47 +02:00
|
|
|
let aws = new Aws()
|
2018-09-19 22:08:11 +02:00
|
|
|
let thumb = new Resize()
|
|
|
|
let large = new Resize()
|
|
|
|
const key = uuid()
|
2018-09-19 22:12:42 +02:00
|
|
|
let _create = () => {
|
2018-09-19 22:08:11 +02:00
|
|
|
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.createLargeImage(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, () => {})
|
|
|
|
}
|
|
|
|
})
|
2018-09-08 00:00:47 +02:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
this._createItem(req, callback)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
static getOne (req, callback) {
|
|
|
|
vegetables.find({
|
|
|
|
where: {
|
|
|
|
vegetableTypeId: req.params.vegetableTypesId,
|
|
|
|
id: req.params.vegetablesId
|
|
|
|
},
|
2018-09-09 18:15:19 +02:00
|
|
|
include: ['Type', 'Pictures']
|
2018-09-08 00:00:47 +02:00
|
|
|
})
|
|
|
|
.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) {
|
2018-09-19 22:08:11 +02:00
|
|
|
let uploaded = 0
|
2018-09-08 00:00:47 +02:00
|
|
|
let aws = new Aws()
|
2018-09-19 22:08:11 +02:00
|
|
|
let thumb = new Resize()
|
|
|
|
let large = new Resize()
|
|
|
|
const key = uuid()
|
2018-09-08 00:00:47 +02:00
|
|
|
|
|
|
|
if (item.mainPicture !== null && item.mainPicture !== '') {
|
|
|
|
this._deleteMedias(item.mainPicture)
|
|
|
|
}
|
|
|
|
|
2018-09-19 22:12:42 +02:00
|
|
|
let _patch = () => {
|
2018-09-19 22:08:11 +02:00
|
|
|
if (uploaded === 2) {
|
|
|
|
this._patchOne(item, values, callback)
|
|
|
|
fs.unlink(req.file.path, () => {})
|
2018-09-08 00:00:47 +02:00
|
|
|
}
|
2018-09-19 22:08:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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.createLargeImage(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, () => {})
|
|
|
|
}
|
|
|
|
})
|
2018-09-08 00:00:47 +02:00
|
|
|
})
|
|
|
|
} 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
|