export cards

This commit is contained in:
2026-03-14 23:54:17 +01:00
parent 87897295f0
commit 4d20b218f7
16 changed files with 985 additions and 4 deletions
+58
View File
@@ -0,0 +1,58 @@
const db = require('../config/db');
const axios = require('axios');
// Use your existing insertCardImage for initial URL insert
const { insertCardImage } = require('../models/cardImageModel');
/**
* GET /cardImage/:cardId
* Returns the image blob of a card. If not stored, downloads from image_url, saves to DB.
*/
async function getCardImage(req, res) {
const { cardId } = req.params;
try {
// Check if blob exists
const [rows] = await db.execute(
'SELECT image_data, image_url FROM card_images WHERE card_id = ? LIMIT 1',
[cardId]
);
if (rows.length === 0) {
return res.status(404).json({ error: 'Card image not found' });
}
const cardImage = rows[0];
if (cardImage.image_data) {
// Blob exists → return as base64
const base64Image = cardImage.image_data.toString('base64');
return res.json({ image: `data:image/png;base64,${base64Image}` });
}
// Blob does not exist → download from URL
if (!cardImage.image_url) {
return res.status(404).json({ error: 'No image URL available for this card' });
}
const response = await axios.get(cardImage.image_url, {
responseType: 'arraybuffer'
});
const imageBuffer = Buffer.from(response.data, 'binary');
// Save blob to DB
await db.execute(
'UPDATE card_images SET image_data = ? WHERE card_id = ?',
[imageBuffer, cardId]
);
const base64Image = imageBuffer.toString('base64');
res.json({ image: `data:image/png;base64,${base64Image}` });
} catch (err) {
console.error('Error fetching card image:', err);
res.status(500).json({ error: 'Failed to fetch card image' });
}
}
module.exports = { getCardImage };
+52
View File
@@ -0,0 +1,52 @@
const db = require('../config/db');
/**
* GET /exportCards
* Returns all cards with their printings and amount_owned
*/
async function exportCards(req, res) {
try {
// Fetch all cards
const [cards] = await db.execute(`
SELECT id, name, card_type AS type, frame_type, level, race, attribute, link_val, tcg_date, ocg_date
FROM cards
`);
// Fetch all printings with sets and rarities
const [printings] = await db.execute(`
SELECT csr.card_id, csr.set_id, csr.card_set_code AS set_code, csr.amount_owned,
s.set_name,
r.id AS rarity_id, r.rarity_name, r.rarity_code
FROM card_sets_rarity csr
JOIN sets s ON csr.set_id = s.id
JOIN rarities r ON csr.rarity_id = r.id
`);
// Map printings to cards
const cardMap = {};
cards.forEach(card => {
cardMap[card.id] = { ...card, printings: [] };
});
printings.forEach(p => {
if (cardMap[p.card_id]) {
cardMap[p.card_id].printings.push({
set_id: p.set_id,
set_name: p.set_name,
set_code: p.set_code,
rarity_id: p.rarity_id,
rarity_name: p.rarity_name,
rarity_code: p.rarity_code,
amount_owned: p.amount_owned || 0
});
}
});
res.json(Object.values(cardMap));
} catch (err) {
console.error('Error exporting cards:', err);
res.status(500).json({ error: 'Failed to export cards' });
}
}
module.exports = { exportCards };
+12 -4
View File
@@ -1,16 +1,24 @@
const express = require('express');
require('dotenv').config();
const cors = require('cors');
// Routes
const importRoutes = require('./routes/importRoutes');
const collectionRoutes = require('./routes/collectionRoutes');
const exportCardsRoutes = require('./routes/exportCardsRoutes');
const cardImageRoutes = require('./routes/cardImageRoutes'); // <-- new
const app = express();
app.use(cors()); // enable CORS for frontend
app.use(express.json());
// Mount routes
app.use('/import', importRoutes);
app.use('/collection', collectionRoutes);
app.use('/exportCards', exportCardsRoutes);
app.use('/cardImage', cardImageRoutes); // <-- mount the new card image route
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
const collectionRoutes = require('./routes/collectionRoutes');
app.use('/collection', collectionRoutes);
});
+8
View File
@@ -0,0 +1,8 @@
const express = require('express');
const router = express.Router();
const { getCardImage } = require('../controllers/cardImageController');
// GET /cardImage/:cardId
router.get('/:cardId', getCardImage);
module.exports = router;
+8
View File
@@ -0,0 +1,8 @@
const express = require('express');
const router = express.Router();
const { exportCards } = require('../controllers/exportCardsController');
// GET /exportCards
router.get('/', exportCards);
module.exports = router;