Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
1.3 kB
1
Indexable
Never
// requisito 6
app.put('/talker/:id', tokenValidator, async (request, response) => {
  const { id } = request.params;
  const data = await fs.readFile(path.join(__dirname, ARQUIVO), 'utf-8');
  const talkers = JSON.parse(data);
  const { name, age, talk } = request.body;
  const talker = { name, age, talk, id: Number(id) };
  const validate = validateTalker(talker);
  if (validate.message) return response.status(400).json({ message: validate.message });
  const index = talkers.findIndex((talkId) => talkId.id === Number(id));
  if (index === -1) {
    return response.status(404).json({ message: 'Pessoa palestrante não encontrada' });
  }
  talkers[index] = talker;
  await fs.writeFile(path.join(__dirname, ARQUIVO), JSON.stringify(talkers));
  return response.status(200).json(talker);
});

// requisito 7
app.delete('/talker/:id', tokenValidator, async (request, response) => {
  const { id } = request.params;
  const data = await fs.readFile(path.join(__dirname, ARQUIVO), 'utf-8');
  const talkers = JSON.parse(data);
  const index = talkers.findIndex((talkId) => talkId.id === Number(id));
  talkers.splice(index, 1);
  await fs.writeFile(path.join(__dirname, ARQUIVO), JSON.stringify(talkers));
  return response.status(204).json({ message: 'Pessoa palestrante deletada com sucesso' });
});
Leave a Comment