bookshelf/books/crud.js (68 lines of code) (raw):
// Copyright 2017, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const images = require('../lib/images');
const db = require('./firestore');
const router = express.Router();
// Automatically parse request body as form data
router.use(bodyParser.urlencoded({extended: false}));
// Set Content-Type for all responses for these routes
router.use((req, res, next) => {
res.set('Content-Type', 'text/html');
next();
});
/**
* GET /books
*
* Display a page of books (up to ten at a time).
*/
router.get('/', async (req, res) => {
let {books, nextPageToken} = await db.list(10, req.query.pageToken);
res.render('books/list.pug', {
books,
nextPageToken,
});
});
/**
* GET /books/add
*
* Display a form for creating a book.
*/
router.get('/add', (req, res) => {
res.render('books/form.pug', {
book: {},
action: 'Add',
});
});
/**
* POST /books/add
*
* Create a book.
*/
router.post(
'/add',
images.multer.single('image'),
images.sendUploadToGCS,
async (req, res) => {
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.
const savedData = await db.create(data);
res.redirect(`${req.baseUrl}/${savedData.id}`);
}
);
/**
* GET /books/:id/edit
*
* Display a book for editing.
*/
router.get('/:book/edit', async (req, res) => {
const book = await db.read(req.params.book);
res.render('books/form.pug', {
book,
action: 'Edit',
});
});
/**
* POST /books/:id/edit
*
* Update a book.
*/
router.post(
'/:book/edit',
images.multer.single('image'),
images.sendUploadToGCS,
async (req, res) => {
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;
}
const savedData = await db.update(req.params.book, data);
res.redirect(`${req.baseUrl}/${savedData.id}`);
}
);
/**
* GET /books/:id
*
* Display a book.
*/
router.get('/:book', async (req, res) => {
const book = await db.read(req.params.book);
res.render('books/view.pug', {
book,
});
});
/**
* GET /books/:id/delete
*
* Delete a book.
*/
router.get('/:book/delete', async (req, res) => {
await db.delete(req.params.book);
res.redirect(req.baseUrl);
});
module.exports = router;