const router = require('express-promise-router')() const parseJSON = require('body-parser').json() const db = require('../db') const {validationResult} = require('express-validator') const validate = require('./middleware/validators') const upload = multer({ storage: multer.memoryStorage(), limits: { files: 1, fileSize: 500000 } }) router.post('/', parseJSON, validate.urlSlugRestrictions('slug'), async (req, res) => { const errors = validationResult(req) if(!errors.isEmpty()) return res.status(422).json({errors: errors.array()}) // TODO: Create session res.json(user) }) router.post('/:uuid/images', upload.single('image'), async (req, res) => { // Handle either image upload body or JSON body try { if(req.file) await db.item.addImage(req.params.uuid, req.file.buffer) else await db.item.addImage(req.params.uuid, await b64.decode(req.body.image.split(',')[1])) } catch (error) { error.status = 500 throw error } res.json(await db.item.findById(req.params.uuid)) }) module.exports = router;