github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/build-blockchain-insurance-app-master/web/www/routers/repair-shop.router.js (about)

     1  import express from 'express';
     2  import * as RepairShopPeer from '../blockchain/repairShopPeer';
     3  
     4  const router = express.Router();
     5  
     6  router.post('/api/repair-orders', async (req, res) => {
     7    try {
     8      let repairOrders = await RepairShopPeer.getRepairOrders();
     9      res.json(repairOrders);
    10    } catch (e) {
    11      console.log(e);
    12      res.json({ error: "Error accessing blockchain."});
    13    }
    14  });
    15  
    16  router.post('/api/complete-repair-order', async (req, res) => {
    17    const { uuid } = req.body;
    18    if (typeof uuid !== 'string') {
    19      res.json({ error: "Invalid request." });
    20      return;
    21    }
    22  
    23    try {
    24      await RepairShopPeer.completeRepairOrder(uuid);
    25      res.json({ success: true });
    26    } catch (e) {
    27      console.log(e);
    28      res.json({ error: "Error accessing blockchain." });
    29    }
    30  });
    31  
    32  router.post('/api/blocks', async (req, res) => {
    33    const { noOfLastBlocks } = req.body;
    34    if (typeof noOfLastBlocks !== 'number') {
    35      res.json({ error: 'Invalid request' });
    36    }
    37    try {
    38      const blocks = await RepairShopPeer.getBlocks(noOfLastBlocks);
    39      res.json(blocks);
    40    } catch (e) {
    41      res.json({ error: 'Error accessing blockchain.' });
    42    }
    43  });
    44  
    45  router.get('*', (req, res) => {
    46    res.render('repair-shop', { repairShopActive: true });
    47  });
    48  
    49  function wsConfig(io) {
    50    RepairShopPeer.on('block', block => { io.emit('block', block); });
    51  }
    52  
    53  export default router;
    54  export { wsConfig };