This commit is contained in:
+89
-73
@@ -1,113 +1,129 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { check, validationResult } from 'express-validator/check'
|
|
||||||
import { Pool } from 'pg'
|
import { Pool } from 'pg'
|
||||||
import { database } from '../../config'
|
import { database } from '../../config'
|
||||||
|
|
||||||
|
const { check, validationResult } = require('express-validator/check')
|
||||||
|
|
||||||
const router = express.Router()
|
const router = express.Router()
|
||||||
|
|
||||||
// Create a connection pool to the database
|
// Create a connection pool to the database
|
||||||
const pool = new Pool(database)
|
const pool = new Pool(database)
|
||||||
|
|
||||||
// CREATE AN IMAGE
|
// CREATE AN IMAGE
|
||||||
router.post('/', [
|
router.post(
|
||||||
check('file').isString(),
|
'/',
|
||||||
check('image').isBase64()
|
[check('file').isString(), check('image').isBase64()],
|
||||||
], async (req, res) => {
|
async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const errors = validationResult(req)
|
const errors = validationResult(req)
|
||||||
if (!errors.isEmpty()) return res.status(400).json({ errors: errors.array() })
|
if (!errors.isEmpty())
|
||||||
|
return res.status(400).json({ errors: errors.array() })
|
||||||
|
|
||||||
const { file, image } = req.body
|
const { file, image } = req.body
|
||||||
|
|
||||||
const client = await pool.connect()
|
const client = await pool.connect()
|
||||||
const result = await client.query('INSERT INTO images (file, image, created_at, updated_at) VALUES ($1, $2, NOW(), NOW()) RETURNING *', [file, image])
|
const result = await client.query(
|
||||||
client.release()
|
'INSERT INTO images (file, image, created_at, updated_at) VALUES ($1, $2, NOW(), NOW()) RETURNING *',
|
||||||
|
[file, image]
|
||||||
|
)
|
||||||
|
client.release()
|
||||||
|
|
||||||
return res.status(201).json(result.rows[0])
|
return res.status(201).json(result.rows[0])
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err.stack)
|
console.error(err.stack)
|
||||||
return res.status(500).json({ error: 'Server error' })
|
return res.status(500).json({ error: 'Server error' })
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
)
|
||||||
|
|
||||||
// READ AN IMAGE
|
// READ AN IMAGE
|
||||||
router.get('/:id', async (req, res) => {
|
router.get('/:id', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params
|
const { id } = req.params
|
||||||
|
|
||||||
const client = await pool.connect()
|
const client = await pool.connect()
|
||||||
const result = await client.query('SELECT * FROM images WHERE id = $1', [id])
|
const result = await client.query('SELECT * FROM images WHERE id = $1', [
|
||||||
client.release()
|
id,
|
||||||
|
])
|
||||||
|
client.release()
|
||||||
|
|
||||||
if (result.rows.length === 0) return res.status(404).json({ error: 'Image not found' })
|
if (result.rows.length === 0)
|
||||||
|
return res.status(404).json({ error: 'Image not found' })
|
||||||
|
|
||||||
return res.status(200).json(result.rows[0])
|
return res.status(200).json(result.rows[0])
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err.stack)
|
console.error(err.stack)
|
||||||
return res.status(500).json({ error: 'Server error' })
|
return res.status(500).json({ error: 'Server error' })
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// UPDATE AN IMAGE
|
// UPDATE AN IMAGE
|
||||||
router.put('/:id', [
|
router.put(
|
||||||
check('file').optional().isString(),
|
'/:id',
|
||||||
check('image').optional().isBase64()
|
[check('file').optional().isString(), check('image').optional().isBase64()],
|
||||||
], async (req, res) => {
|
async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const errors = validationResult(req)
|
const errors = validationResult(req)
|
||||||
if (!errors.isEmpty()) return res.status(400).json({ errors: errors.array() })
|
if (!errors.isEmpty())
|
||||||
|
return res.status(400).json({ errors: errors.array() })
|
||||||
|
|
||||||
const { id } = req.params
|
const { id } = req.params
|
||||||
const { file, image } = req.body
|
const { file, image } = req.body
|
||||||
|
|
||||||
const client = await pool.connect()
|
const client = await pool.connect()
|
||||||
let query = 'UPDATE images SET'
|
let query = 'UPDATE images SET'
|
||||||
const values = []
|
const values = []
|
||||||
|
|
||||||
if (file) {
|
if (file) {
|
||||||
query += ' file = $1,'
|
query += ' file = $1,'
|
||||||
values.push(file)
|
values.push(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (image) {
|
if (image) {
|
||||||
query += ' image = $2,'
|
query += ' image = $2,'
|
||||||
values.push(image)
|
values.push(image)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove trailing comma and space from query string
|
// Remove trailing comma and space from query string
|
||||||
query = query.slice(0, -1)
|
query = query.slice(0, -1)
|
||||||
|
|
||||||
query += ', updated_at = NOW() WHERE id = $3 RETURNING *'
|
query += ', updated_at = NOW() WHERE id = $3 RETURNING *'
|
||||||
values.push(id)
|
values.push(id)
|
||||||
|
|
||||||
const result = await client.query(query, values)
|
const result = await client.query(query, values)
|
||||||
client.release()
|
client.release()
|
||||||
|
|
||||||
if (result.rows.length === 0) return res.status(404).json({ error: 'Image not found' })
|
if (result.rows.length === 0)
|
||||||
|
return res.status(404).json({ error: 'Image not found' })
|
||||||
|
|
||||||
return res.status(200).json(result.rows[0])
|
return res.status(200).json(result.rows[0])
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err.stack)
|
console.error(err.stack)
|
||||||
return res.status(500).json({ error: 'Server error' })
|
return res.status(500).json({ error: 'Server error' })
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
)
|
||||||
|
|
||||||
// DELETE AN IMAGE
|
// DELETE AN IMAGE
|
||||||
router.delete('/:id', async (req, res) => {
|
router.delete('/:id', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params
|
const { id } = req.params
|
||||||
|
|
||||||
const client = await pool.connect()
|
const client = await pool.connect()
|
||||||
const result = await client.query('DELETE FROM images WHERE id = $1 RETURNING *', [id])
|
const result = await client.query(
|
||||||
client.release()
|
'DELETE FROM images WHERE id = $1 RETURNING *',
|
||||||
|
[id]
|
||||||
|
)
|
||||||
|
client.release()
|
||||||
|
|
||||||
if (result.rows.length === 0) return res.status(404).json({ error: 'Image not found' })
|
if (result.rows.length === 0)
|
||||||
|
return res.status(404).json({ error: 'Image not found' })
|
||||||
|
|
||||||
return res.status(200).json(result.rows[0])
|
return res.status(200).json(result.rows[0])
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err.stack)
|
console.error(err.stack)
|
||||||
return res.status(500).json({ error: 'Server error' })
|
return res.status(500).json({ error: 'Server error' })
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
export default router
|
export default router
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { check, validationResult } from 'express-validator/check'
|
|
||||||
import { Pool } from 'pg'
|
import { Pool } from 'pg'
|
||||||
import { database } from '../../config'
|
import { database } from '../../config'
|
||||||
|
|
||||||
|
const { check, validationResult } = require('express-validator/check')
|
||||||
|
|
||||||
const router = express.Router()
|
const router = express.Router()
|
||||||
|
|
||||||
// Create a connection pool to the database
|
// Create a connection pool to the database
|
||||||
@@ -45,8 +46,11 @@ router.get('/', async (req, res) => {
|
|||||||
// Get a single location by ID
|
// Get a single location by ID
|
||||||
router.get('/:id', async (req, res) => {
|
router.get('/:id', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const result = await pool.query('SELECT * FROM locations WHERE id = $1', [req.params.id])
|
const result = await pool.query('SELECT * FROM locations WHERE id = $1', [
|
||||||
if (result.rows.length === 0) return res.status(404).send('Location not found')
|
req.params.id,
|
||||||
|
])
|
||||||
|
if (result.rows.length === 0)
|
||||||
|
return res.status(404).send('Location not found')
|
||||||
res.send(result.rows[0])
|
res.send(result.rows[0])
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err.message)
|
console.error(err.message)
|
||||||
|
|||||||
Reference in New Issue
Block a user