All files route.js

100% Statements 28/28
100% Branches 12/12
100% Functions 5/5
100% Lines 28/28

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 601x 1x     1x 4x 4x   4x                 4x 1x       3x 1x 1x       2x     1x 1x 1x     1x 1x       1x 3x 3x 1x     2x 2x 2x 1x   1x 1x         1x  
const formValidator = require('./form_validator');
const photoModel = require('./photo_model');
 
function route(app) {
  app.get('/', (req, res) => {
    const tags = req.query.tags;
    const tagmode = req.query.tagmode;
 
    const ejsLocalVariables = {
      tagsParameter: tags || '',
      tagmodeParameter: tagmode || '',
      photos: [],
      searchResults: false,
      invalidParameters: false
    };
 
    // 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);
    }
 
    // 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 });
      });
  });
 
  app.post('/zip', async (req, res) => {
    const tags = req.query.tags;
    if (!tags) {
      return res.status(400).send({ error: 'Les tags sont requis' });
    }
 
    try {
      const producer = require('./producer');
      await producer.publishMessage(tags);
      return res.send(`Tags "${tags}" envoyés dans la queue Pub/Sub !`);
    } catch (error) {
      console.error('Erreur lors de l\'envoi du message :', error);
      return res.status(500).send({ error: error.message });
    }
  });
}
 
module.exports = route;