added csv and updated migration to work better hopefully
Test Workflow / test-and-lint (pull_request) Failing after 41s
Test Workflow / test-and-lint (pull_request) Failing after 41s
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
export const up = (pgm) => {
|
export const up = (pgm) => {
|
||||||
pgm.sql(`
|
pgm.sql(`
|
||||||
ALTER TABLE "items"
|
ALTER TABLE "items"
|
||||||
ADD CONSTRAINT "set_id"
|
ADD COLUMN "set_id" UUID,
|
||||||
|
ADD CONSTRAINT "fk_set_id"
|
||||||
FOREIGN KEY ("set_id")
|
FOREIGN KEY ("set_id")
|
||||||
REFERENCES "sets" ("id")
|
REFERENCES "sets"("id")
|
||||||
ON DELETE CASCADE;
|
ON DELETE CASCADE;
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
@@ -11,6 +12,7 @@ export const up = (pgm) => {
|
|||||||
export const down = (pgm) => {
|
export const down = (pgm) => {
|
||||||
pgm.sql(`
|
pgm.sql(`
|
||||||
ALTER TABLE "items"
|
ALTER TABLE "items"
|
||||||
DROP CONSTRAINT "fk_name";
|
DROP COLUMN "set_id",
|
||||||
|
DROP CONSTRAINT "fk_set_id";
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,4 +106,39 @@ router.delete('/:id', async (req, res) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Create a new item from CSV
|
||||||
|
router.post('/csv', [
|
||||||
|
check('collection_id').not().isEmpty(),
|
||||||
|
check('set_id').not().isEmpty() // Add validation for set_id
|
||||||
|
], async (req, res) => {
|
||||||
|
const errors = validationResult(req)
|
||||||
|
if (!errors.isEmpty()) {
|
||||||
|
return res.status(400).json({ errors: errors.array() })
|
||||||
|
}
|
||||||
|
const { collection_id, set_id } = req.body
|
||||||
|
const csvBuffer = req.files.file.buffer.toString('utf8')
|
||||||
|
const csvLines = csvBuffer.split('\n').filter(line => line.trim().length > 0)
|
||||||
|
const client = await pool.connect()
|
||||||
|
try {
|
||||||
|
// Insert each row into the items table
|
||||||
|
for (const line of csvLines) {
|
||||||
|
const [productId, name, cleanName, imageUrl, categoryId, groupId, url, modifiedOn, imageCount, extCardText, extUPC, lowPrice, midPrice, highPrice, marketPrice, directLowPrice, subTypeName, extNumber, extRarity, extCardType, extHP, extStage, extAttack1, extWeakness, extRetreatCost, extAttack2, extResistance] = line.split(',').map(value => value.trim())
|
||||||
|
// Fetch the image from imageUrl
|
||||||
|
const imageResponse = await fetch(imageUrl)
|
||||||
|
const imageBuffer = await imageResponse.buffer()
|
||||||
|
|
||||||
|
// Insert the image into the images table
|
||||||
|
const insertImageResult = await client.query(
|
||||||
|
'INSERT INTO images (file, image) VALUES ($1::TEXT, $2::BYTEA) RETURNING id',
|
||||||
|
[`${productId}.jpg`, imageBuffer]
|
||||||
|
)
|
||||||
|
|
||||||
|
await client.query('INSERT INTO items (collection_id, image_id, productId, name, cleanName, extCardText, marketPrice, extRarity, set_id, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, NOW(), NOW())', [collection_id, insertImageResult.rows[0].id, productId, name, cleanName, extCardText, marketPrice, extRarity, set_id])
|
||||||
|
}
|
||||||
|
res.status(201).json({ message: 'Items created' })
|
||||||
|
} finally {
|
||||||
|
client.release()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
export default router
|
export default router
|
||||||
|
|||||||
Reference in New Issue
Block a user