feat: implement user authentication and login modal, refactor backend
This commit is contained in:
81
web/backend/routes/articles.js
Normal file
81
web/backend/routes/articles.js
Normal file
@@ -0,0 +1,81 @@
|
||||
import express from 'express';
|
||||
import { getNotifiedArticles } from '../services/redis.js';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// Obtener artículos notificados
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const articles = await getNotifiedArticles();
|
||||
const limit = parseInt(req.query.limit) || 100;
|
||||
const offset = parseInt(req.query.offset) || 0;
|
||||
|
||||
const sorted = articles.sort((a, b) => b.notifiedAt - a.notifiedAt);
|
||||
const paginated = sorted.slice(offset, offset + limit);
|
||||
|
||||
res.json({
|
||||
articles: paginated,
|
||||
total: articles.length,
|
||||
limit,
|
||||
offset,
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Buscar artículos en Redis
|
||||
router.get('/search', async (req, res) => {
|
||||
try {
|
||||
const query = req.query.q || '';
|
||||
if (!query.trim()) {
|
||||
return res.json({ articles: [], total: 0 });
|
||||
}
|
||||
|
||||
const searchTerm = query.toLowerCase().trim();
|
||||
const allArticles = await getNotifiedArticles();
|
||||
|
||||
// Filtrar artículos que coincidan con la búsqueda
|
||||
const filtered = allArticles.filter(article => {
|
||||
// Buscar en título
|
||||
const title = (article.title || '').toLowerCase();
|
||||
if (title.includes(searchTerm)) return true;
|
||||
|
||||
// Buscar en descripción
|
||||
const description = (article.description || '').toLowerCase();
|
||||
if (description.includes(searchTerm)) return true;
|
||||
|
||||
// Buscar en localidad
|
||||
const location = (article.location || '').toLowerCase();
|
||||
if (location.includes(searchTerm)) return true;
|
||||
|
||||
// Buscar en precio (como número o texto)
|
||||
const price = String(article.price || '').toLowerCase();
|
||||
if (price.includes(searchTerm)) return true;
|
||||
|
||||
// Buscar en plataforma
|
||||
const platform = (article.platform || '').toLowerCase();
|
||||
if (platform.includes(searchTerm)) return true;
|
||||
|
||||
// Buscar en ID
|
||||
const id = String(article.id || '').toLowerCase();
|
||||
if (id.includes(searchTerm)) return true;
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
// Ordenar por fecha de notificación (más recientes primero)
|
||||
const sorted = filtered.sort((a, b) => b.notifiedAt - a.notifiedAt);
|
||||
|
||||
res.json({
|
||||
articles: sorted,
|
||||
total: sorted.length,
|
||||
query: query,
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user