This repository was archived by the owner on Aug 17, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathserver.js
More file actions
102 lines (80 loc) · 2.51 KB
/
server.js
File metadata and controls
102 lines (80 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const express = require("express");
const app = express();
const { Pool } = require("pg");
const bodyParser = require("body-parser"); // for parsing application/json
app.use(bodyParser.json()); // for parsing application/json
app.get("/", (req, res) => {
res.send("Database Project");
});
const db = new Pool({
user: "CYF", // replace with you username
host: "localhost",
database: "cyf_ecommerce",
password: process.env.USER_PASS,
port: 5432,
});
// Write a SQL query to return all customers
app.get("/customers", function (req, res) {
db.query("SELECT * FROM customers", (error, result) => {
res.json(result.rows);
});
});
// Write a SQL query to return all customers specified by id
app.get("/customers/:id", function (req, res) {
const custId = parseInt(req.params.id);
db.query(
"SELECT * FROM customers WHERE id = $1",
[custId],
function (err, result) {
if (err) {
console.error(err);
res.status(500).send("Error retrieving customer from database");
} else {
res.json(result.rows[0]);
console.log(result.rows[0]);
}
}
);
});
// Write a SQL query to return all suppliers
app.post("/suppliers", function (req, res) {
const newName = req.body.name;
const nweAddress = req.body.address;
const newCity = req.body.city;
const newCountry = req.body.country;
if (!newName || !nweAddress || !newCity || !newCountry) {
return res.status(400).send(" fields required.");
}
// TODO: Write a SQL query to insert a new customer and return the id
const query =
"INSERT INTO customers (name, address, city, country) " +
"VALUES ($1, $2, $3, $4) returning id";
db.query(query, [newName, nweAddress, newCity, newCountry], (err, result) => {
if (err) {
console.error(err);
return res.status(500).send("Error creating customer.");
}
const newID = result.rows[0].id;
res.send(`New customer added. New Id = ${newID}`);
});
});
// Write a SQL query to return all
app.get("/products", function (req, res) {
const productName = req.query.name;
console.log(productName);
db.query(
"SELECT * FROM products WHERE product_name LIKE CONCAT('%', $1::text, '%')",
[productName],
(error, result) => {
if (error) {
console.error(error);
res.status(500).send("Error retrieving products from database");
} else {
res.json(result.rows);
}
}
);
});
app.listen(5000, function () {
console.log("Server is listening on port 5000. Ready to accept requests!");
});