eletrotupi / tcc/ api/src/routes/mood.js06314a5b665164bc343bc1c0a38ca4a562c53b7b
832 B | 33 lines Raw
1
import express from "express";
2

3
const router = express.Router();
4

5
router.post("/", (req, res) => {
6
  const { user_id, stress_level, anxiety_level, title, description } = req.body;
7

8
  if (!user_id || !stress_level || !anxiety_level)
9
    return res.status(400).json({ error: "Campos faltantes" });
10

11
  const stmt = req.db.prepare(
12
    `
13
      INSERT INTO mood (user_id, stress_level, anxiety_level, title, description)
14
      VALUES (?, ?, ?, ?, ?)
15
    `
16
  );
17

18
  const result = stmt.run(
19
    user_id, stress_level, anxiety_level, title || null, description || null
20
  );
21

22
  res.status(201).json({ id: result.lastInsertRowid });
23
});
24

25
router.get("/:user_id", (req, res) => {
26
  const stmt = req.db.prepare("SELECT * FROM mood WHERE user_id = ?");
27
  const rows = stmt.all(req.params.user_id);
28

29
  res.json(rows);
30
});
31

32
export default router;
33