added image and location APIs (#4)

Reviewed-on: #4
Co-authored-by: Joseph Nelson <joseph.nelson4456@gmail.com>
Co-committed-by: Joseph Nelson <joseph.nelson4456@gmail.com>
This commit was merged in pull request #4.
This commit is contained in:
2026-05-16 22:43:51 -07:00
committed by joseph.nelson4456
parent 3b55f82130
commit 0db3d677b2
4 changed files with 229 additions and 0 deletions
+95
View File
@@ -0,0 +1,95 @@
import express from 'express'
import { Pool } from 'pg'
import { database } from '../../config'
const { check, validationResult } = require('express-validator')
const router = express.Router()
// Create a connection pool to the database
const pool = new Pool(database)
// Create a new location
router.post(
'/',
[check('name', 'Name is required').not().isEmpty()],
async (req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() })
}
try {
const { name } = req.body
await pool.query(
'INSERT INTO locations (name, created_at, updated_at) VALUES ($1, NOW(), NOW())',
[name]
)
res.send({ message: 'Location created' })
} catch (err) {
console.error(err.message)
res.status(500).send('Server error')
}
}
)
// Get all locations
router.get('/', async (req, res) => {
try {
const result = await pool.query('SELECT * FROM locations')
res.send(result.rows)
} catch (err) {
console.error(err.message)
res.status(500).send('Server error')
}
})
// Get a single location by ID
router.get('/:id', async (req, res) => {
try {
const result = await pool.query('SELECT * FROM locations WHERE id = $1', [
req.params.id,
])
if (result.rows.length === 0)
return res.status(404).send('Location not found')
res.send(result.rows[0])
} catch (err) {
console.error(err.message)
res.status(500).send('Server error')
}
})
// Update a location
router.put(
'/:id',
[check('name', 'Name is required').not().isEmpty()],
async (req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() })
}
try {
const { name } = req.body
await pool.query(
'UPDATE locations SET name = $1, updated_at = NOW() WHERE id = $2',
[name, req.params.id]
)
res.send({ message: 'Location updated' })
} catch (err) {
console.error(err.message)
res.status(500).send('Server error')
}
}
)
// Delete a location
router.delete('/:id', async (req, res) => {
try {
await pool.query('DELETE FROM locations WHERE id = $1', [req.params.id])
res.send({ message: 'Location deleted' })
} catch (err) {
console.error(err.message)
res.status(500).send('Server error')
}
})
export default router