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:
@@ -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
|
||||
Reference in New Issue
Block a user