manga-server/routes/mangaRoutes.js
2024-10-05 10:07:31 +02:00

61 lines
1.6 KiB
JavaScript

const express = require('express');
const router = express.Router();
const Manga = require('../models/manga');
const Chapter = require('../models/chapter');
const mangaController = require('../controllers/mangaController');
const connectors = require('../connectors.json');
router.get('/fetch', async (req, res) => {
const connectorId = req.query.connectorId;
const localisation = req.query.localisation;
await mangaController.fetchAndStoreMangas(connectorId, localisation);
res.send('Données récupérées avec succès');
});
router.get('/mangas', async (req, res) => {
const connectorId = req.query.connectorId;
const localisation = req.query.localisation;
let whereClause = {};
if (connectorId) {
whereClause.connectorId = connectorId;
}
if (localisation) {
const connectorsForLoc = connectors.filter((config) => config.localisation === localisation);
const connectorNames = connectorsForLoc.map((c) => c.name);
if (connectorNames.length > 0) {
whereClause.connectorName = connectorNames;
} else {
return res.json([]);
}
}
const mangas = await Manga.findAll({
where: whereClause,
include: [{ model: Chapter, as: 'chapters' }],
});
res.json(mangas);
});
router.get('/mangas/:title', async (req, res) => {
const title = req.params.title;
const manga = await Manga.findOne({
where: { title },
include: [{ model: Chapter, as: 'chapters' }],
});
if (!manga) {
return res.status(404).send('Manga non trouvé');
}
res.json(manga);
});
module.exports = router;