Add files via upload

master
nicko-b 4 years ago committed by GitHub
commit fed83277ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 106
      app.js
  2. 1344
      package-lock.json
  3. 18
      package.json

106
app.js

@ -0,0 +1,106 @@
const express = require('express')
const app = express()
const port = 3001
const bodyParser = require('body-parser')
const { json } = require('body-parser')
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
app.use(bodyParser.json())
app.use(
bodyParser.urlencoded({
extended: true,
})
)
const Pool = require('pg').Pool
const pool = new Pool({
user: 'nick',
password: 'nb084406',
host: 'localhost',
database: 'ecommerce_db',
port: 5432,
})
const getUsers = (request, response) => {
pool.query('SELECT * FROM users ORDER BY id ASC', (error, results) => {
if (error) {
throw error
}
response.status(200).json(results.rows)
})
}
app.get('/', getUsers)
const getUserByName = (request, response) => {
pool.query('SELECT * FROM users WHERE firstname = $1 or lastname = $1', [request.params.name], (error, results) => {
if (error) {
throw error
}
response.status(200).json(results.rows)
})
}
app.get('/users/:name', getUserByName)
const addUser = (request, response) => {
pool.query('INSERT INTO users (firstname, lastname, email) VALUES ($1, $2, $3) RETURNING *',
[request.body.firstname, request.body.lastname, request.body.email], (error, results) => {
if (error) {
throw error
}
response.status(200).json(results.rows)
})
}
app.post('/users/adduser', addUser)
const updateUser = (request, response) => {
const id = parseInt(request.params.id)
const { firstname, lastname, email } = request.body
pool.query(
'UPDATE users SET firstName = $1, lastName = $2, email = $3 WHERE id = $4 RETURNING *',
[firstname, lastname, email, id],
(error, results) => {
if (results.rowCount === 0) {
response.send("Hey that user id doesn't exist")
} else {
response.status(200).send(`Updated user ${id}`)
}
}
)
}
app.put('/users/update/:id', updateUser)
const deleteUser = (request, response) => {
const id = parseInt(request.params.id)
pool.query(
'DELETE FROM users WHERE id = $1', [id],
(error, results) => {
if (results.rowCount === 0) {
response.send("Hey that user id doesn't exist")
} else {
response.status(200).send(`Deleted user ${id}`)
}
}
)
}
app.delete('/users/delete/:id', deleteUser)
// console.log(JSON.stringify(results))

1344
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -0,0 +1,18 @@
{
"name": "ecommerce_db",
"version": "1.0.0",
"description": "",
"main": "app.js",
"dependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.4",
"pg": "^8.2.1"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Loading…
Cancel
Save