{"size": 1220, "ext": "js", "max_stars_repo_path": "10/app/routes/musicas.js", "max_stars_repo_name": "bmarchete/primeiros-passos-nodejs", "max_stars_repo_stars_event_min_datetime": "2017-07-31T23:30:05.000Z", "max_stars_repo_stars_event_max_datetime": "2021-08-17T02:11:36.000Z", "max_issues_repo_path": "10/app/routes/musicas.js", "max_issues_repo_name": "bmarchete/primeiros-passos-nodejs", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "10/app/routes/musicas.js", "max_forks_repo_name": "bmarchete/primeiros-passos-nodejs", "max_forks_repo_forks_event_min_datetime": "2017-07-01T17:47:38.000Z", "max_forks_repo_forks_event_max_datetime": "2021-12-14T14:54:29.000Z"}
{"max_stars_count": 11, "max_issues_count": null, "max_forks_count": 10, "avg_line_length": 16.7123287671, "max_line_length": 48, "alphanum_fraction": 0.5213114754}
var express = require('express');
var router = express.Router();
var db = require('../db');

router.get('/', (req, res, next) => {
  db("musicas").then((musicas)=>{
    res.render('index',{
      musicas: musicas
    });
  },next);

});

router.get('/add',(req,res,next)=>{
  res.render('add');

});

router.post('/',(req,res,next)=>{
  db("musicas").insert(req.body).then((ids) => {
    res.redirect('/');
  },next)
});

router.get('/edit/:id',(req,res,next)=>{
  const {id} = req.params;

  db("musicas")
  .where("id", id)
  .first()
  .then((musica) => {
    console.log(musica);
    if (!musica) {
      return res.send(400);
    }

    res.render("edit.njk",{
      musica: musica
    });
  },next);

});

router.put('/edit/:id', (req,res,next)=>{
  const {id} = req.params;

  db("musicas")
  .where('id', id)
  .update(req.body)
  .then((result) => {
    if (result === 0) {
      return res.send(400);
    }
    res.redirect('/');
  },next)
});

router.delete('/delete/:id', (req,res,next)=>{
  const {id} = req.params;

  db("musicas")
  .where('id', id)
  .delete()
  .then((result) => {
    if (result === 0) {
      return res.send(400);
    }
    res.redirect('/');
  },next)
});

module.exports = router;
