basic set import works
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
const mysql = require('mysql2/promise');
|
||||
require('dotenv').config();
|
||||
|
||||
const pool = mysql.createPool({
|
||||
host: process.env.DB_HOST,
|
||||
port: process.env.DB_PORT || 3306,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: process.env.DB_NAME,
|
||||
waitForConnections: true,
|
||||
connectionLimit: 10,
|
||||
queueLimit: 0
|
||||
});
|
||||
|
||||
module.exports = pool;
|
||||
@@ -0,0 +1,24 @@
|
||||
const { fetchAllSets } = require('../services/ygoproService');
|
||||
const { upsertSet } = require('../models/setModel');
|
||||
|
||||
async function importSets(req, res) {
|
||||
try {
|
||||
const sets = await fetchAllSets();
|
||||
let added = 0;
|
||||
|
||||
for (const set of sets) {
|
||||
await upsertSet(set);
|
||||
added++;
|
||||
}
|
||||
|
||||
res.json({
|
||||
added,
|
||||
total: sets.length
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Failed to import sets' });
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { importSets };
|
||||
@@ -0,0 +1,13 @@
|
||||
const express = require('express');
|
||||
require('dotenv').config();
|
||||
const importRoutes = require('./routes/importRoutes');
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
|
||||
app.use('/import', importRoutes);
|
||||
|
||||
const PORT = process.env.PORT || 3000;
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server running on http://localhost:${PORT}`);
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
const db = require('../config/db');
|
||||
|
||||
async function upsertSet(set) {
|
||||
const { set_name, set_code, num_of_cards, tcg_date } = set;
|
||||
|
||||
const sql = `
|
||||
INSERT INTO sets (set_name, set_code, num_of_cards, tcg_date)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
set_name = VALUES(set_name),
|
||||
num_of_cards = VALUES(num_of_cards),
|
||||
tcg_date = VALUES(tcg_date)
|
||||
`;
|
||||
await db.execute(sql, [set_name, set_code, num_of_cards, tcg_date]);
|
||||
}
|
||||
|
||||
module.exports = { upsertSet };
|
||||
@@ -0,0 +1,17 @@
|
||||
const db = require('../config/db');
|
||||
|
||||
async function upsertSet(set) {
|
||||
const { set_name, set_code, num_of_cards, tcg_date } = set;
|
||||
|
||||
const sql = `
|
||||
INSERT INTO sets (set_name, set_code, num_of_cards, tcg_date)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
set_name = VALUES(set_name),
|
||||
num_of_cards = VALUES(num_of_cards),
|
||||
tcg_date = VALUES(tcg_date)
|
||||
`;
|
||||
await db.execute(sql, [set_name, set_code, num_of_cards, tcg_date]);
|
||||
}
|
||||
|
||||
module.exports = { upsertSet };
|
||||
@@ -0,0 +1,7 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { importSets } = require('../controllers/importController');
|
||||
|
||||
router.post('/sets', importSets);
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,22 @@
|
||||
const axios = require('axios');
|
||||
require('dotenv').config();
|
||||
|
||||
const API_BASE = process.env.YGOPRO_API_BASE;
|
||||
|
||||
async function fetchAllSets() {
|
||||
try {
|
||||
const response = await axios.get(`${API_BASE}/cardsets.php`);
|
||||
const sets = response.data.map(s => ({
|
||||
set_name: s.set_name,
|
||||
set_code: s.set_code,
|
||||
num_of_cards: s.num_of_cards || null,
|
||||
tcg_date: s.tcg_date || null
|
||||
}));
|
||||
return sets;
|
||||
} catch (err) {
|
||||
console.error('Error fetching sets from YGOPRODeck:', err.message);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { fetchAllSets };
|
||||
Reference in New Issue
Block a user