Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Pool = require('pg').Pool
- const pool = new Pool({
- user: 'postgres',
- host: 'localhost',
- database: 'CarDB_1',
- password: 'phucduongoanh',
- port: 5432,
- })
- //////////////////////////////////////////////////
- const getCars = (request, response) => {
- pool.query('SELECT * FROM public.car ORDER BY id ASC', (error, results) => {
- if (error) {
- throw error
- }
- response.status(200).json(results.rows)
- })
- }
- const getCarById = (request, response) => {
- const id = parseInt(request.params.id)
- pool.query('SELECT * FROM public.car WHERE id = $1', [id], (error, results) => {
- if (error) {
- throw error
- }
- response.status(200).json(results.rows)
- })
- }
- const createCar = (request, response) => {
- const { brand, color, price, enginesize, quantity } = request.body
- pool.query('INSERT INTO public.car (brand, color, price, enginesize, quantity) VALUES ($1, $2, $3, $4, $5)', [brand, color, price, enginesize, quantity], (error, results) => {
- if (error) {
- throw error
- }
- //response.status(201).send(`Car added with ID: ${result.insertId}`)
- response.status(201).send(`Car added`)
- })
- }
- const updateCar = (request, response) => {
- const id = parseInt(request.params.id)
- const { brand, color, price, enginesize, quantity } = request.body
- pool.query(
- 'UPDATE public.car SET brand = $1, color = $2, price = $3, enginesize = $4, quantity = $5 WHERE id = $6',
- [ brand, color, price, enginesize, quantity, id],
- (error, results) => {
- if (error) {
- throw error
- }
- response.status(200).send(`Car modified with ID: ${id}`)
- }
- )
- }
- const deleteCar = (request, response) => {
- const id = parseInt(request.params.id)
- pool.query('DELETE FROM public.car WHERE id = $1', [id], (error, results) => {
- if (error) {
- throw error
- }
- response.status(200).send(`Car deleted with ID: ${id}`)
- })
- }
- console.log(getCars)
- module.exports = {
- getCars,
- getCarById,
- createCar,
- updateCar,
- deleteCar
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement