{"size": 786, "ext": "js", "max_stars_repo_path": "src/routes.js", "max_stars_repo_name": "renne-is-back/DesafioArvoreMegahack-BackEnd-Time43", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/routes.js", "max_issues_repo_name": "renne-is-back/DesafioArvoreMegahack-BackEnd-Time43", "max_issues_repo_issues_event_min_datetime": "2021-05-11T19:09:36.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-27T07:39:26.000Z", "max_forks_repo_path": "src/routes.js", "max_forks_repo_name": "FPPUI/DesafioArvoreMegahack-BackEnd-Time43", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 6, "max_forks_count": null, "avg_line_length": 31.44, "max_line_length": 118, "alphanum_fraction": 0.6145038168}
const express = require('express');
const connection = require('./database/connection');
const pathAbsolute = require('path');

const router = express.Router();

router.get('/download/:id', async (req, res)=>{
    const id = req.params.id;
    try{
        const { path, type } = (await connection.select('path', 'type').from('library').where('id', '=', id).first());
        const fileURL = pathAbsolute.resolve(__dirname, '../', path + '.' + type);
        return res.sendFile(fileURL);
    }catch(err){
        console.log(err);
        return res.send('Ocorreu algum erro.');
    }
    //return res.json({id});
})

router.get('/library', async (req, res)=>{
    const library = await connection.select('*').from('library');
    return res.json(library);
})

module.exports = router;