All files / app route.js

66.66% Statements 24/36
68.75% Branches 11/16
80% Functions 4/5
66.66% Lines 24/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 781x 1x 1x 1x 1x     1x 4x 4x   4x                     4x 1x       3x 1x 1x         2x 2x                 2x     1x 1x 1x     1x 1x         1x                                   1x  
const formValidator = require('./form_validator');
const photoModel = require('./photo_model');
const producer = require('./queue/producer');
const jobStore = require('./queue/job_store');
const storage = require('./storage/storage');
 
function route(app) {
  app.get('/', async (req, res) => {
    const tags = req.query.tags;
    const tagmode = req.query.tagmode;
 
    const ejsLocalVariables = {
      tagsParameter: tags || '',
      tagmodeParameter: tagmode || '',
      photos: [],
      searchResults: false,
      invalidParameters: false,
      // Part V - download link for an already-zipped result (if any).
      zipDownloadUrl: ''
    };
 
    // if no input params are passed in then render the view with out querying the api
    if (!tags && !tagmode) {
      return res.render('index', ejsLocalVariables);
    }
 
    // validate query parameters
    if (!formValidator.hasValidFlickrAPIParams(tags, tagmode)) {
      ejsLocalVariables.invalidParameters = true;
      return res.render('index', ejsLocalVariables);
    }
 
    // Part V - if the zip for these tags is already available, generate a
    // temporary download link and expose it to the view.
    const completedObjectName = jobStore.getObjectName(tags);
    Iif (completedObjectName) {
      try {
        ejsLocalVariables.zipDownloadUrl = await storage.getDownloadUrl(completedObjectName);
      } catch (error) {
        console.error('failed to generate signed url', error);
      }
    }
 
    // get photos from flickr public feed api
    return photoModel
      .getFlickrPhotos(tags, tagmode)
      .then(photos => {
        ejsLocalVariables.photos = photos;
        ejsLocalVariables.searchResults = true;
        return res.render('index', ejsLocalVariables);
      })
      .catch(error => {
        console.log('aspdfonaposd', error);
        return res.status(500).send({ error });
      });
  });
 
  // Part I & II - endpoint producer: queue a "zip these tags" job.
  app.post('/zip', async (req, res) => {
    const tags = req.query.tags;
    const tagmode = req.query.tagmode || 'all';
 
    if (!tags) {
      return res.status(400).send({ error: 'Missing "tags" query parameter' });
    }
 
    try {
      const messageId = await producer.publishZipJob(tags, tagmode);
      return res.status(202).send({ status: 'queued', messageId, tags });
    } catch (error) {
      console.error('failed to publish zip job', error);
      return res.status(500).send({ error: 'Failed to queue zip job' });
    }
  });
}
 
module.exports = route;