From bcac4f80277d23e1f778e62df8b6d0dfadff9da7 Mon Sep 17 00:00:00 2001 From: DeGssoft Date: Mon, 2 Jan 2023 11:41:54 -0300 Subject: [PATCH] db --- PI-Food/api/src/db.js | 10 ++++-- PI-Food/api/src/models/Diet.js | 17 +++++++++ PI-Food/api/src/models/Recipe.js | 18 +++++++++- PI-Food/api/src/routes/diets.js | 16 +++++++++ PI-Food/api/src/routes/index.js | 7 ++-- PI-Food/api/src/routes/recipes.js | 58 +++++++++++++++++++++++++++++++ 6 files changed, 120 insertions(+), 6 deletions(-) create mode 100644 PI-Food/api/src/models/Diet.js create mode 100644 PI-Food/api/src/routes/diets.js create mode 100644 PI-Food/api/src/routes/recipes.js diff --git a/PI-Food/api/src/db.js b/PI-Food/api/src/db.js index afede34..cfa1da4 100644 --- a/PI-Food/api/src/db.js +++ b/PI-Food/api/src/db.js @@ -30,10 +30,16 @@ sequelize.models = Object.fromEntries(capsEntries); // En sequelize.models están todos los modelos importados como propiedades // Para relacionarlos hacemos un destructuring -const { Recipe } = sequelize.models; +const { Recipe, Diet } = sequelize.models; // Aca vendrian las relaciones -// Product.hasMany(Reviews); + +//Recipie +Recipe.hasMany(Diet); +Diet.belongsTo(Recipe); +//Diet +Diet.hasMany(Recipe); +Recipe.belongsTo(Diet); module.exports = { ...sequelize.models, // para poder importar los modelos así: const { Product, User } = require('./db.js'); diff --git a/PI-Food/api/src/models/Diet.js b/PI-Food/api/src/models/Diet.js new file mode 100644 index 0000000..dee05db --- /dev/null +++ b/PI-Food/api/src/models/Diet.js @@ -0,0 +1,17 @@ +const { DataTypes } = require('sequelize'); +// Exportamos una funcion que define el modelo +// Luego le injectamos la conexion a sequelize. +module.exports = (sequelize) => { + // defino el modelo + sequelize.define('diet', { + ID: { + type: DataTypes.UUID, + defaultValue: DataTypes.UUIDV4, + primaryKey: true, + allowNull: false + }, + name: { + type: DataTypes.STRING + } +}); +}; diff --git a/PI-Food/api/src/models/Recipe.js b/PI-Food/api/src/models/Recipe.js index 784e44a..50d50a5 100644 --- a/PI-Food/api/src/models/Recipe.js +++ b/PI-Food/api/src/models/Recipe.js @@ -4,9 +4,25 @@ const { DataTypes } = require('sequelize'); module.exports = (sequelize) => { // defino el modelo sequelize.define('recipe', { + ID: { + type: DataTypes.UUID, + defaultValue: DataTypes.UUIDV4, + primaryKey: true, + allowNull: false + }, name: { type: DataTypes.STRING, - allowNull: false, + allowNull: false }, + resume: { + type: DataTypes.STRING, + allowNull: false + }, + level: { + type: DataTypes.INTEGER + }, + instructions: { + type: DataTypes.TEXT + } }); }; diff --git a/PI-Food/api/src/routes/diets.js b/PI-Food/api/src/routes/diets.js new file mode 100644 index 0000000..df0c134 --- /dev/null +++ b/PI-Food/api/src/routes/diets.js @@ -0,0 +1,16 @@ +var express = require('express'); +var router = express.Router(); +const { Recipe } = require('../db'); + +const endpoint = '/diets/'; + +router.get(endpoint, function (req, res, next) { + const diets = Recipe.findAll(); + if (diets) + res.send(diets); + else + res.status(404).send({ error: 'no existe ninguna dieta' }); +}); + + +module.exports = router; \ No newline at end of file diff --git a/PI-Food/api/src/routes/index.js b/PI-Food/api/src/routes/index.js index 91d3704..5802906 100644 --- a/PI-Food/api/src/routes/index.js +++ b/PI-Food/api/src/routes/index.js @@ -1,12 +1,13 @@ const { Router } = require('express'); // Importar todos los routers; -// Ejemplo: const authRouter = require('./auth.js'); - +const recipeRouter = require('./recipes.js'); +const dietRouter = require('./require.js'); const router = Router(); // Configurar los routers -// Ejemplo: router.use('/auth', authRouter); +router.use('/recipes', recipeRouter); +router.use('/diets', dietRouter); module.exports = router; diff --git a/PI-Food/api/src/routes/recipes.js b/PI-Food/api/src/routes/recipes.js new file mode 100644 index 0000000..1313738 --- /dev/null +++ b/PI-Food/api/src/routes/recipes.js @@ -0,0 +1,58 @@ +var express = require('express'); +var router = express.Router(); +const { Recipe } = require('../db'); + +const endpoint = '/recipes/'; +const errPost = 'Error data provided'; +/* GET users listing. */ +/* +[ ] GET /recipes?name='...': +Obtener un listado de las recetas que contengan la palabra +ingresada como query parameter +Si no existe ninguna receta mostrar un mensaje adecuado +*/ +router.get(endpoint, function (req, res, next) { + const { name } = req.query; + const condition = { where: { name } }; + const recipes = Recipe.findAll( + condition + ); + if (recipes) + res.send(recipes); + else + res.status(404).send({ error: 'no existe ninguna receta con ese id' }); +}); + +/* +[ ] GET /recipes/{idReceta}: +Obtener el detalle de una receta en particular +Debe traer solo los datos pedidos en la ruta de detalle +de receta +Incluir los tipos de dieta asociados +*/ +router.get(endpoint + ':idReceta', function (req, res, next) { + const { idReceta } = rq.params; + const recipe = Recipe.findByPk(idReceta); + res.send(recipe || 'Recipe not found'); +}); + +/* +[ ] POST /recipes: +Recibe los datos recolectados desde el formulario +controlado de la ruta de creación de recetas por body +Crea una receta en la base de datos relacionada con sus +tipos de dietas. +*/ +const newRecipe = ({ title, contents }) => Recipe.create({ title, contents }); + +router.post(endpoint, function (req, res, next) { + const { title, contents } = req.body + if (title && contents) { + res.send(newRecipe({ title, contents })); + } + else + res.status(404).json(errPost); +}); + + +module.exports = router; \ No newline at end of file