From 4e278e67e15ac0da75bb70d4b65675840f40d1f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Escalera=20Garc=C3=ADa?= Date: Sat, 9 Mar 2024 19:09:03 +0100 Subject: [PATCH] arreglos paginacion api victor --- Victor/api-VEG.js | 65 ++++++++++++++++++++-------------------- tests/apiVEG-gcloud.json | 4 +-- 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/Victor/api-VEG.js b/Victor/api-VEG.js index af6fa69..c74b241 100644 --- a/Victor/api-VEG.js +++ b/Victor/api-VEG.js @@ -33,40 +33,39 @@ module.exports = (app, db) => { }); // GET para obtener datos con paginación -app.get(API_BASE + "/youtube-trends", (req, res) => { - const page = parseInt(req.query.page) || 1; // Página solicitada (predeterminada: 1) - const limit = parseInt(req.query.limit) || 10; // Número de resultados por página (predeterminado: 10) - const skip = (page - 1) * limit; // Salto para la paginación - - // Filtros opcionales - const filters = {}; - if (req.query.country) filters.country = req.query.country; - if (req.query.title) filters.title = req.query.title; - if (req.query.publishedAt) { - const publishedAt = req.query.publishedAt; - const [year, month] = publishedAt.split("-"); // Dividir el valor de la URL en año y mes - const startOfMonth = new Date(year, month - 1, 1); - const endOfMonth = new Date(year, month, 0, 23, 59, 59); - filters.published_at = { $gte: startOfMonth.toISOString(), $lte: endOfMonth.toISOString() }; - } - if (req.query.channelTitle) filters.channel_title = req.query.channelTitle; - if (req.query.categoryId) filters.category_id = parseInt(req.query.categoryId); - if (req.query.trendingDate) filters.trending_date = req.query.trendingDate; - if (req.query.viewCount) filters.view_count = parseInt(req.query.viewCount); - if (req.query.commentCount) filters.comment_count = parseInt(req.query.commentCount); - - // Realizar la consulta con los filtros y la paginación - db.find(filters).sort({ id: 1 }).skip(skip).limit(limit).exec((err, docs) => { - if (err) { - console.error(err); + app.get(API_BASE + "/youtube-trends", (req, res) => { + + const queryParams = req.query; + const limit = parseInt(queryParams.limit) || 10; + const offset = parseInt(queryParams.offset) || 0; + + let query = {}; + + Object.keys(queryParams).forEach(key => { + if (key !== 'limit' && key !== 'offset') { + const value = !isNaN(queryParams[key]) ? parseFloat(queryParams[key]) : queryParams[key]; + if (typeof value === 'string') { + query[key] = new RegExp(value, 'i'); + } else { + query[key] = value; + } + } + }); + + db.find(query).skip(offset).limit(limit).exec((err, data_VEG) => { + if (err) { res.sendStatus(500, "Internal Error"); - } else if (docs && docs.length > 0) { - res.status(200).json(docs); - } else { - res.sendStatus(404, "Not Found"); - } - }); -}); + } else { + + const datosFinal = data_VEG.map(d => { + const { _id, ...datosSin_id } = d; + return datosSin_id; + }); + res.status(200).json(datosFinal); + } + }); + }); + //POST para crear un nuevo dato app.post(API_BASE + "/youtube-trends", (req, res) => { diff --git a/tests/apiVEG-gcloud.json b/tests/apiVEG-gcloud.json index 4559e9e..06e3869 100644 --- a/tests/apiVEG-gcloud.json +++ b/tests/apiVEG-gcloud.json @@ -198,7 +198,7 @@ "method": "GET", "header": [], "url": { - "raw": "{{gcloud}}/api/v1/youtube-trends?published-at=2022-06", + "raw": "{{gcloud}}/api/v1/youtube-trends?published-at=2010-07-011T22:00:12Z", "host": [ "{{gcloud}}" ], @@ -210,7 +210,7 @@ "query": [ { "key": "published-at", - "value": "2022-06" + "value": "2010-07-011T22:00:12Z" } ] }