needed to update some APIs

This commit is contained in:
2026-05-16 23:46:33 -07:00
parent 96b06cf92d
commit a4646da6ab
+10 -42
View File
@@ -36,9 +36,7 @@ router.get('/:id', async (req, res) => {
}) })
// Create a new item // Create a new item
router.post( router.post('/', [
'/',
[
check('collection_id').not().isEmpty(), check('collection_id').not().isEmpty(),
check('image_id').not().isEmpty(), check('image_id').not().isEmpty(),
check('productId').not().isEmpty(), check('productId').not().isEmpty(),
@@ -47,46 +45,21 @@ router.post(
check('extCardText').not().isEmpty(), check('extCardText').not().isEmpty(),
check('marketPrice').not().isEmpty(), check('marketPrice').not().isEmpty(),
check('extRarity').not().isEmpty(), check('extRarity').not().isEmpty(),
check('set_id').not().isEmpty(), // Add validation for set_id check('set_id').not().isEmpty() // Add validation for set_id
], ], async (req, res) => {
async (req, res) => {
const errors = validationResult(req) const errors = validationResult(req)
if (!errors.isEmpty()) { if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() }) return res.status(400).json({ errors: errors.array() })
} }
const { const { collection_id, image_id, productId, name, cleanName, extCardText, marketPrice, extRarity, set_id } = req.body
collection_id,
image_id,
productId,
name,
cleanName,
extCardText,
marketPrice,
extRarity,
set_id,
} = req.body
const client = await pool.connect() const client = await pool.connect()
try { try {
const result = await client.query( const result = 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()) RETURNING *', [collection_id, image_id, productId, name, cleanName, extCardText, marketPrice, extRarity, set_id])
'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()) RETURNING *',
[
collection_id,
image_id,
productId,
name,
cleanName,
extCardText,
marketPrice,
extRarity,
set_id,
]
)
res.status(201).json(result.rows[0]) res.status(201).json(result.rows[0])
} finally { } finally {
client.release() client.release()
} }
} })
)
// Update an existing item // Update an existing item
router.put('/:id', async (req, res) => { router.put('/:id', async (req, res) => {
@@ -103,18 +76,13 @@ router.put('/:id', async (req, res) => {
const client = await pool.connect() const client = await pool.connect()
try { try {
if (Object.keys(updates).length > 0) { if (Object.keys(updates).length > 0) {
const updateFields = Object.keys(updates) const updateFields = Object.keys(updates).map(key => `${key} = $${Object.keys(updates).indexOf(key) + 2}`).join(', ')
.map((key) => `${key} = $${Object.keys(updates).indexOf(key) + 2}`)
.join(', ')
updates.updated_at = 'NOW()' updates.updated_at = 'NOW()'
const result = await client.query( const result = await client.query(`UPDATE items SET ${updateFields}, updated_at = $${Object.keys(updates).length + 1} WHERE id = $1 RETURNING *`, [id, ...Object.values(updates)])
`UPDATE items SET ${updateFields}, updated_at = $${Object.keys(updates).length + 1} WHERE id = $1 RETURNING *`, return res.json(result.rows[0])
[id, ...Object.values(updates)] }
)
if (result.rows.length === 0) { if (result.rows.length === 0) {
return res.status(404).json({ message: 'Item not found' }) return res.status(404).json({ message: 'Item not found' })
}
res.json(result.rows[0])
} else { } else {
return res.status(204).json() return res.status(204).json()
} }