Giter Club home page Giter Club logo

code-academy-x-press-publishing's Introduction

Intro

CRUD with Sqlite3 practice code-academy

dúvidas:

dúvida 1:

Existe, por exemplo uma tabela de artistas:

  • id int primary key
  • name text
  • etc...

nas rotas de CRUD com sqlite3, vou receber o id como param. p. ex.: /api/artist/:id.

o exercício está usando o sqlite3 como database e sugere criar um middleware para extrair o param do request:

esse é a assinatura middleware:

artistRouter.param('artistId', (req, res, next, param) => {
  // request handling
})

o db é isso aqui:

const sqlite3 = require('sqlite3');
const db = new sqlite3.Database('../database.sqlite');

escrevi isso aqui:

  artistsRouter.param('artistId', (req, res, next, param) => {
  db.get(`SELECT * FROM Artist WHERE id = ${param}`, (err, row) => {
    if(err) next();
    req.artistId = row.id;
  })
})

O gabarito sugere diferente:

  artistsRouter.param('artistId', (req, res, next, param) => {
  db.get(`SELECT * FROM Artist.id WHERE id = $artistId`,
  {$artistId: param},
  (err, row) => {
    if(err) next();
    req.artistId = row.id;
  })
})

A minha dúvida é: Quais as diferenças de uma forma ou de outra? Tem alguma diferença? Eu aposto que não. Essa lib para usar sqlite3 com node parece um pouco antiga e parece oferecer uma alternativa anterior ao ES6 para facilitar interpolação de string. Essa é a minha aposta.

dúvida 2:

isso gera um erro nos testes:

artistsRouter.param('artistId', (req, res, next, param) => {
  db.get(`SELECT * FROM Artist WHERE id = ${param}`, (err, row) => {
    if (err) {
      next(err);
    }
    if (!row) {
      res.sendStatus(404);
    }
    req.artist = row;
    next();
  });
});

isso, não:

artistsRouter.param('artistId', (req, res, next, param) => {
  db.get(`SELECT * FROM Artist WHERE id = ${param}`, (err, row) => {
    if (err) {
      next(err);
    } else if (!row) {
      res.sendStatus(404);
    } else {
      req.artist = row;
      next();
    }
  });
});

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

Por quê?

https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client

estava alterando a response e chamando next(), porque esqueci do return.

Isso, funciona:

artistsRouter.param('artistId', (req, res, next, param) => {
  db.get(`SELECT * FROM Artist WHERE id = ${param}`, (err, row) => {
    if (err) return next(err);
    if (!row) return res.sendStatus(404);
    req.artist = row;
    next();
  });
});

dúvida 3:

Por que isso não funciona?

artistsRouter.post('/', (req, res, next) => {
  const {artist: {name, dateOfBirth, biography}} = req.body
  
  if (!name || !dateOfBirth || !biography) {
    return res.sendStatus(400);
  }
  const isCurrentlyEmployed = req.body.artist.isCurrentlyEmployed === 0 ? 0 : 1;
  db.run(
    `INSERT INTO Artist (name, date_of_birth, biography, is_currently_employed) VALUES (${name}, ${dateOfBirth}, ${biography}, ${isCurrentlyEmployed})`,
    // {
    //   $name: name,
    //   $dateOfBirth: dateOfBirth,
    //   $biography: biography,
    //   $isCurrentlyEmployed: isCurrentlyEmployed,
    // },
    function(err) {
      db.get(`SELECT * FROM Artist WHERE id = ${this.lastID}`, (err, row) => {
        if (err) return next(err)
        res.status(201).json({ artist: row });
      });
    }
  );
});

code-academy-x-press-publishing's People

Contributors

dependabot[bot] avatar lpolon avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.