/** * POST /books/add * * Create a book. */ // [START add] router.post( '/add', images.multer.single('image'), images.sendUploadToGCS, (req, res, next) => { let data = req.body; // Was an image uploaded? If so, we'll use its public URL // in cloud storage. if (req.file && req.file.cloudStoragePublicUrl) { data.imageUrl = req.file.cloudStoragePublicUrl; } // Save the data to the database. getModel().create(data, (err, savedData) => { if (err) { next(err); return; } res.redirect(`${req.baseUrl}/${savedData.id}`); }); } );
/** * POST /books/:id/edit * * Update a book. */ router.post( '/:book/edit', images.multer.single('image'), images.sendUploadToGCS, (req, res, next) => { let data = req.body; // Was an image uploaded? If so, we'll use its public URL // in cloud storage. if (req.file && req.file.cloudStoragePublicUrl) { req.body.imageUrl = req.file.cloudStoragePublicUrl; } getModel().update(req.params.book, data, (err, savedData) => { if (err) { next(err); return; } res.redirect(`${req.baseUrl}/${savedData.id}`); }); } );
if (req.file && req.file.cloudStoragePublicUrl) { data.imageUrl = req.file.cloudStoragePublicUrl;