chartManD commited on
Commit
7ae259a
·
1 Parent(s): b820eb3

get po iduser y post de aliemento

Browse files
main.js CHANGED
@@ -4,11 +4,15 @@ let app = express();
4
  const connectDB = require("./src/database/conection.js");
5
 
6
  const {
7
- getAllGlucosa,
8
  getGlucosaByIdUser,
9
  postOneRegisterGlucosa,
10
  } = require("./src/controllers/glucosa-controller.js");
11
 
 
 
 
 
 
12
  connectDB();
13
  const PORT = process.env.PORT || 7860;
14
 
@@ -18,6 +22,7 @@ app.use(express.json());
18
  app.use(bodyParser.urlencoded({ extended: false }));
19
 
20
  // Endpoints
 
21
  // app.get("/", (req, res) => { -- Usar con app frond corregida --
22
  // res.sendFile(`${__dirname}/public/index.html`);
23
  // });
@@ -30,9 +35,11 @@ app.get("/api/", (req, res) => {
30
  res.json({ message: "Esta es la raiz del API" });
31
  });
32
 
33
- app.get("/api/glucosa", getAllGlucosa);
34
- app.post("/api/glucosa", postOneRegisterGlucosa);
35
  app.get("/api/glucosa/:iduser", getGlucosaByIdUser);
 
 
 
 
36
 
37
  app.listen(PORT, () => {
38
  console.log("La aplicacion esta corriendo");
 
4
  const connectDB = require("./src/database/conection.js");
5
 
6
  const {
 
7
  getGlucosaByIdUser,
8
  postOneRegisterGlucosa,
9
  } = require("./src/controllers/glucosa-controller.js");
10
 
11
+ const {
12
+ postOneFood,
13
+ getFoodByUserID,
14
+ } = require("./src/controllers/alimento-controller.js");
15
+
16
  connectDB();
17
  const PORT = process.env.PORT || 7860;
18
 
 
22
  app.use(bodyParser.urlencoded({ extended: false }));
23
 
24
  // Endpoints
25
+
26
  // app.get("/", (req, res) => { -- Usar con app frond corregida --
27
  // res.sendFile(`${__dirname}/public/index.html`);
28
  // });
 
35
  res.json({ message: "Esta es la raiz del API" });
36
  });
37
 
 
 
38
  app.get("/api/glucosa/:iduser", getGlucosaByIdUser);
39
+ app.post("/api/glucosa", postOneRegisterGlucosa);
40
+
41
+ app.get("/api/alimento/:iduser", getFoodByUserID);
42
+ app.post("/api/alimento", postOneFood);
43
 
44
  app.listen(PORT, () => {
45
  console.log("La aplicacion esta corriendo");
src/controllers/alimento-controller.js ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const Alimento = require("../models/alimento.js");
2
+
3
+ /**
4
+ *
5
+ * @param {import("express").Request} req
6
+ * @param {import("express").Response} res
7
+ */
8
+ async function getFoodByUserID(req, res) {
9
+ const { iduser } = req.params;
10
+ try {
11
+ const alimentos = await Alimento.find({ iduser: iduser });
12
+ if (alimentos.length == 0) {
13
+ res.json({});
14
+ }
15
+ res.json(alimentos);
16
+ } catch (error) {
17
+ res.status(500).json({ error: "Error al optner los alimentos" });
18
+ }
19
+ }
20
+
21
+ /**
22
+ *
23
+ * @param {import("express").Request} req
24
+ * @param {import("express").Response} res
25
+ */
26
+ async function postOneFood(req, res) {
27
+ try {
28
+ await Alimento.create(req.body);
29
+ res.json({ message: "Ok, datos ingresados" });
30
+ } catch (error) {
31
+ res.status(500).json({ error: "Error al ingresar alimento" });
32
+ }
33
+ }
34
+
35
+ module.exports = {
36
+ postOneFood,
37
+ getFoodByUserID,
38
+ };
src/controllers/glucosa-controller.js CHANGED
@@ -1,26 +1,5 @@
1
  const Glucosa = require("../models/glucosa.js");
2
 
3
- /**
4
- * @description Obtener todos los registros de glucosa
5
- *
6
- * @param {import("express").Request} req
7
- * @param {import("express").Response} res
8
- */
9
- async function getAllGlucosa(req, res) {
10
- try {
11
- const registros = await Glucosa.find();
12
- if (registros.length == 0) {
13
- res.json({ message: "No hay registros existentes de glucosa" });
14
- }
15
- res.json(registros);
16
- } catch (error) {
17
- console.error("Error al conectar a MongoDB:", error.message);
18
- res
19
- .status(500)
20
- .json({ error: "Error al obtener los registros de glucosa" });
21
- }
22
- }
23
-
24
  /**
25
  * @description Obtener todos los registros de glucosa de un usuario
26
  *
@@ -32,11 +11,10 @@ async function getGlucosaByIdUser(req, res) {
32
  try {
33
  const registros = await Glucosa.find({ iduser: userId });
34
  if (registros.length == 0) {
35
- res.json({ message: "El usuario no tiene registros de glucosa" });
36
  }
37
  res.json(registros);
38
  } catch (error) {
39
- console.error("Error al conectar a MongoDB:", error.message);
40
  res
41
  .status(500)
42
  .json({ error: "Error al obtener los registros de glucosa" });
@@ -54,10 +32,7 @@ async function postOneRegisterGlucosa(req, res) {
54
  await Glucosa.create(req.body);
55
  res.json({ message: "Ok, datos ingresados" });
56
  } catch (error) {
57
- console.error("Error al crear registro glucosa:", error);
58
- res
59
- .status(500)
60
- .json({ error: "Error al obtener los registros de glucosa" });
61
  }
62
  }
63
 
 
1
  const Glucosa = require("../models/glucosa.js");
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  /**
4
  * @description Obtener todos los registros de glucosa de un usuario
5
  *
 
11
  try {
12
  const registros = await Glucosa.find({ iduser: userId });
13
  if (registros.length == 0) {
14
+ res.json({});
15
  }
16
  res.json(registros);
17
  } catch (error) {
 
18
  res
19
  .status(500)
20
  .json({ error: "Error al obtener los registros de glucosa" });
 
32
  await Glucosa.create(req.body);
33
  res.json({ message: "Ok, datos ingresados" });
34
  } catch (error) {
35
+ res.status(500).json({ error: "Error al ingresar el registro de glucosa" });
 
 
 
36
  }
37
  }
38
 
src/database/mocks/mocks-alimento.js ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const datos = [
2
+ {
3
+ nombre: "Papa",
4
+ proteinas: 3,
5
+ calorias: 95,
6
+ carbohidratos: 35,
7
+ grasas: 1,
8
+ iduser: "adadd123",
9
+ },
10
+ {
11
+ nombre: "Papa",
12
+ proteinas: 3,
13
+ calorias: 95,
14
+ carbohidratos: 35,
15
+ grasas: 1,
16
+ iduser: "adadd123",
17
+ },
18
+ ];
19
+
20
+ // db.alimentos.insertMany([
21
+ // {
22
+ // nombre: "Papa",
23
+ // proteinas: 3,
24
+ // calorias: 95,
25
+ // carbohidratos: 35,
26
+ // grasas: 1,
27
+ // iduser: "adadd123",
28
+ // },
29
+ // {
30
+ // nombre: "Tomate",
31
+ // proteinas: 0,
32
+ // calorias: 13,
33
+ // carbohidratos: 5,
34
+ // grasas: 0,
35
+ // iduser: "adadd123",
36
+ // },
37
+ // ]);
src/database/mocks/mocks-glucosa.js CHANGED
@@ -21,3 +21,51 @@ const datos = [
21
  iduser: "adadd123",
22
  },
23
  ];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  iduser: "adadd123",
22
  },
23
  ];
24
+
25
+ // db.glucosa.insertMany([
26
+ // {
27
+ // glucosa: 123,
28
+ // ayuno: false,
29
+ // comentario: "no hay lugar como el hogar",
30
+ // createdAt: Date.now(),
31
+ // iduser: "adadd123",
32
+ // },
33
+ // {
34
+ // glucosa: 108,
35
+ // ayuno: false,
36
+ // comentario: "no hay lugar como el machete",
37
+ // createdAt: Date.now(),
38
+ // iduser: "adadd123",
39
+ // },
40
+ // {
41
+ // glucosa: 185,
42
+ // ayuno: false,
43
+ // comentario: "no hay lugar como el barrio",
44
+ // createdAt: Date.now(),
45
+ // iduser: "adadd123",
46
+ // },
47
+ // ]);
48
+
49
+ // db.glucosa.insertMany([
50
+ // {
51
+ // glucosa: 123,
52
+ // ayuno: false,
53
+ // comentario: "no hay lugar como el hogar",
54
+ // createdAt: Date.now(),
55
+ // iduser: "wwwe1",
56
+ // },
57
+ // {
58
+ // glucosa: 108,
59
+ // ayuno: false,
60
+ // comentario: "no hay lugar como el machete",
61
+ // createdAt: Date.now(),
62
+ // iduser: "wwwe1",
63
+ // },
64
+ // {
65
+ // glucosa: 185,
66
+ // ayuno: false,
67
+ // comentario: "no hay lugar como el barrio",
68
+ // createdAt: Date.now(),
69
+ // iduser: "pppok",
70
+ // },
71
+ // ]);
src/models/alimento.js CHANGED
@@ -1,11 +1,18 @@
1
  const mongoose = require("mongoose");
2
 
3
- const alimento = new mongoose.Schema({
4
- nombre: String,
5
- proteinas: Number,
6
- calorias: Number,
7
- carbohidratos: Number,
8
- grasas: Number,
9
- createdAt: { type: Date, default: Date.now },
10
- iduser: String,
11
- });
 
 
 
 
 
 
 
 
1
  const mongoose = require("mongoose");
2
 
3
+ const alimento = new mongoose.Schema(
4
+ {
5
+ nombre: { type: String, required: true },
6
+ proteinas: { type: Number, required: true },
7
+ calorias: { type: Number, required: true },
8
+ carbohidratos: { type: Number, required: true },
9
+ grasas: { type: Number, required: true },
10
+ createdAt: { type: Date, default: Date.now },
11
+ iduser: { type: String, required: true },
12
+ },
13
+ { collection: "alimentos" }
14
+ );
15
+
16
+ const Alimento = mongoose.model("Alimentos", alimento);
17
+
18
+ module.exports = Alimento;
src/models/glucosa.js CHANGED
@@ -3,10 +3,10 @@ const mongoose = require("mongoose");
3
  const glucosaSchema = new mongoose.Schema(
4
  {
5
  glucosa: { type: Number, required: true },
6
- ayuno: Boolean,
7
  comentario: { type: String, default: null },
8
  createdAt: { type: Date, default: Date.now },
9
- iduser: String,
10
  },
11
  { collection: "glucosa" }
12
  );
 
3
  const glucosaSchema = new mongoose.Schema(
4
  {
5
  glucosa: { type: Number, required: true },
6
+ ayuno: { type: Boolean, required: true },
7
  comentario: { type: String, default: null },
8
  createdAt: { type: Date, default: Date.now },
9
+ iduser: { type: String, required: true },
10
  },
11
  { collection: "glucosa" }
12
  );
src/models/usuario.js CHANGED
@@ -1,14 +1,14 @@
1
  const mongoose = require("mongoose");
2
 
3
  const usuario = new mongoose.Schema({
4
- nombre: String,
5
- apellido: String,
6
- username: String,
7
- password: String,
8
- edad: Number,
9
- altura: Number,
10
- peso: Number,
11
- genero: String,
12
- tipo_diabetes: String,
13
- actividad_fisica: String,
14
  });
 
1
  const mongoose = require("mongoose");
2
 
3
  const usuario = new mongoose.Schema({
4
+ nombre: { type: String, required: true },
5
+ apellido: { type: String, required: true },
6
+ username: { type: String, required: true },
7
+ password: { type: String, required: true },
8
+ edad: { type: Number, required: true },
9
+ altura: { type: Number, required: true },
10
+ peso: { type: Number, required: true },
11
+ genero: { type: String, required: true },
12
+ tipo_diabetes: { type: String, required: true },
13
+ actividad_fisica: { type: String, required: true },
14
  });