api/middleware/Vegetables.js

344 lines
8.5 KiB
JavaScript
Raw Normal View History

2018-09-08 00:00:47 +02:00
const vegetables = require('../models').vegetables
2018-10-02 19:18:05 +02:00
const models = require('../models')
2018-09-08 00:00:47 +02:00
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 => {
2018-10-10 21:10:43 +02:00
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.vegetableProp
.create({
vegetablesId: 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)
})
2018-09-08 00:00:47 +02:00
})
.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, () => {})
}
})
})
2018-09-19 22:15:00 +02:00
thumb.createThumbnail(req.file.path, (err, file) => {
2018-09-19 22:08:11 +02:00
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-10-02 19:18:05 +02:00
include: [
'Type',
'Pictures',
{
2018-10-06 17:24:11 +02:00
model: models.vegetableProperties,
as: 'Properties',
include: ['Property']
2018-10-02 19:18:05 +02:00
}
]
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, () => {})
}
})
})
2018-09-19 22:15:00 +02:00
thumb.createThumbnail(req.file.path, (err, file) => {
2018-09-19 22:08:11 +02:00
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