From 9e54f54fedc180db69917805a5f2f92ed5270833 Mon Sep 17 00:00:00 2001 From: Mathias02 Date: Mon, 6 Mar 2023 14:35:18 +0200 Subject: [PATCH 1/3] initial commit --- cyf-ecommerce-api/server.js | 252 ++++++++++++++++++++++++++++++++++++ 1 file changed, 252 insertions(+) create mode 100644 cyf-ecommerce-api/server.js diff --git a/cyf-ecommerce-api/server.js b/cyf-ecommerce-api/server.js new file mode 100644 index 00000000..a9ce78d9 --- /dev/null +++ b/cyf-ecommerce-api/server.js @@ -0,0 +1,252 @@ +const express = require("express"); +const app = express(); +const {Pool} = require("pg"); + +const port = process.env.PORT || 4001; + +const pool = new Pool({ + user: 'ptgs', + host: 'dpg-cg0qgb7dvk4ovd26hei0-a.oregon-postgres.render.com', + database: 'cyfdbcourse', + password: '2CY6mAb7GKWpWb1Rq49MIweALl1Zsb5m', + port: 5432 + , ssl: { + rejectUnauthorized: false + } +}); + +app.use(express.json()); + +app.get('/', (req, res) =>{ + console.log('well done') + res.status(200).send('Welcome to our product site') +}) + +//GET +app.get("/products", (req, res) => { + pool.query('SELECT * FROM products') + .then((result) =>{res.status(200).json(result.rows)}) + .catch((error) =>{ + console.error(error) + res.status(500).json(error) + }) + +}); + +//GET PARAMS QUERY + +app.get("/customers/:customerId", function (req, res) { + const custmrId = req.params.customerId; + + pool + .query("SELECT * FROM customers WHERE id=$1", [custmrId]) + .then((result) => res.json(result.rows)) + .catch((error) => { + console.error(error); + res.status(500).json(error); + }); +}); + +// // post + +app.post("/customers", function (req, res) { + const newCustomerName = req.body.customer_name; + const newCustomerAddress = req.body.address; + const newCustomerCity = req.body.city; + const newCustomerCountry = req.body.country; + + pool + .query("SELECT * FROM customers WHERE customer_name=$1", [newCustomerName]) + .then((result) => { + if (result.rows.length > 0) { + return res + .status(400) + .send("A customer with the same name already exists!"); + } else { + const query = + "INSERT INTO customers (customer_name, address, city, country) VALUES ($1, $2, $3, $4)"; + pool + .query(query, [newCustomerName, newCustomerAddress, newCustomerCity, newCustomerCountry]) + .then(() => res.send("customer created!")) + .catch((error) => { + console.error(error); + res.status(500).json(error); + }); + } + }); +}); + + +app.post("/products", function (req, res) { + const newProductName = req.body.product_name; + + pool + .query("SELECT * FROM products WHERE product_name=$1", [newProductName]) + .then((result) => { + if (result.rows.length > 0) { + return res + .status(400) + .send("A product with the same name already exists!"); + } else { + const query = + "INSERT INTO products (product_name) VALUES ($1)"; + pool + .query(query, [newProductName]) + .then(() => res.send("product created!")) + .catch((error) => { + console.error(error); + res.status(500).json(error); + }); + } + }); +}); + + +app.post("/availability", function (req, res) { + const avProdName = req.body.prod_id; + const avProdSupp = req.body.supp_id; + const avProdPrice = req.body.unit_price; + + if (!Number.isInteger(avProdName) || newHotelRooms <= 0) { + return res + .status(400) + .send("The number of the product should be a positive integer."); + } + + pool + .query("SELECT * FROM customers WHERE prod_id=$1", [avProdName]) + .then((result) => { + if (result.rows.length > 0) { + return res + .status(400) + .send("A product with the same name already exists!"); + } else { + const query = + "INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES ($1, $2, $3)"; + pool + .query(query, [avProdName, avProdSupp, avProdPrice]) + .then(() => res.send("product is available!")) + .catch((error) => { + console.error(error); + res.status(500).json(error); + }); + } + }); +}); + +app.post("/orders", function (req, res) { + const orderDate = req.body.order_date; + const orderReferrence = req.body.order_reference; + const orderCustomerId = req.body.customer_id; + + if (!Number.isInteger(orderCustomerId) || orderCustomerId <= 0) { + return res + .status(400) + .send("The customer id should be a positive integer."); + } + + pool + .query("SELECT * FROM orders WHERE order_date =$1", [orderReferrence]) + .then((result) => { + if (result.rows.length > 0) { + return res + .status(400) + .send("An order with the same name already exists!"); + } else { + const query = + "INSERT INTO orders (order_date, order_reference, customer_id) VALUES ($1, $2, $3)"; + pool + .query(query, [orderDate, orderReferrence, orderCustomerId]) + .then(() => res.send("order created!")) + .catch((error) => { + console.error(error); + res.status(500).json(error); + }); + } + }); +}); + +app.post("/customers/:customerId/orders", function (req, res) { + const customerId = req.params.customerId; + + + if (!Number.isInteger(customerId) || customerId <= 0) { + return res + .status(400) + .send("The number of the order should be a positive integer."); + } + + pool + .query("SELECT * FROM customers WHERE id=$1", [customerId]) + .then((result) => { + if (result.rows.length > 0) { + return res + .status(400) + .send("An order with the same name already exists!"); + } else { + const query = + "INSERT INTO orders (prod_id, supp_id, unit_price) VALUES ($1, $2, $3)"; + pool + .query(query, [customerId]) + .then(() => res.send("product is available!")) + .catch((error) => { + console.error(error); + res.status(500).json(error); + }); + } + }); +}); +// //UPDATE + +app.put("/customers/:customerId", function (req, res) { + const customerId = req.params.customerId; + const newName = req.body.name; + const newAddress = req.body.address; + const newCity = req.body.city; + const newCountry = req.body.country; + + pool + .query("UPDATE customers SET customer_name=$1 address=$2 city=$3 country=$4 WHERE id=$5", [newName, newAddress , newCity, newCountry, customerId]) + .then(() => res.send(`Customer ${customerId} updated!`)) + .catch((error) => { + console.error(error); + res.status(500).json(error); + }); +}); + +//Delete + +app.delete("/orders/:orderId", function (req, res) { + const orderId = req.params.orderId; + + pool + .query("DELETE FROM orders WHERE id=$1", [orderId]) + .then(() => pool.query("DELETE FROM customers WHERE id=$2", [orderId])) + .then(() => res.send(`Customer ${orderId} deleted!`)) + .catch((error) => { + console.error(error); + res.status(500).json(error); + }); +}); + +app.delete("/customers/:customerId", function (req, res) { + const customerId = req.params.customerId; + + + pool + .query("DELETE FROM customers WHERE id=$1", [customerId]) + .then(() => res.send(`Customer ${customerId} deleted!`)) + .catch((error) => { + console.error(error); + res.status(500).json(error); + }); +}); + +const listener = app.listen(port, () =>{ + console.log(`port is listening on port ${port}`) +}) + + + + + From 615c0c17f293c8962748cdbc3e95e7e122dad47b Mon Sep 17 00:00:00 2001 From: Mathias02 Date: Mon, 6 Mar 2023 15:33:39 +0200 Subject: [PATCH 2/3] exercise fie committed --- cyf-ecommerce-api/exercise.js | 216 ++++++++++++++++++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100644 cyf-ecommerce-api/exercise.js diff --git a/cyf-ecommerce-api/exercise.js b/cyf-ecommerce-api/exercise.js new file mode 100644 index 00000000..c93ebd49 --- /dev/null +++ b/cyf-ecommerce-api/exercise.js @@ -0,0 +1,216 @@ +//routes, +// app.get("/hotels", function(req, res) { +// pool.query('SELECT * FROM hotels') +// .then((result) => res.json(result.rows)) +// .catch((error) => { +// console.error(error); +// res.status(500).json(error); +// }); +// }); +// app.post("/hotels", function (req, res) { +// const newHotelName = req.body.name; +// const newHotelRooms = req.body.rooms; +// const newHotelPostcode = req.body.postcode; +// if (!Number.isInteger(newHotelRooms) || newHotelRooms <= 0) { +// return res +// .status(400) +// .send("The number of rooms should be a positive integer."); +// } +// pool +// .query("SELECT * FROM hotels WHERE name=$1", [newHotelName]) +// .then((result) => { +// if (result.rows.length > 0) { +// return res +// .status(400) +// .send("An hotel with the same name already exists!"); +// } else { +// const query = +// "INSERT INTO hotels (name, rooms, postcode) VALUES ($1, $2, $3)"; +// pool +// .query(query, [newHotelName, newHotelRooms, newHotelPostcode]) +// .then(() => res.send("Hotel created!")) +// .catch((error) => { +// console.error(error); +// res.status(500).json(error); +// }); +// } +// }); +// }); +// //name, email, address, city, postcode, country +// app.post("/customers", function (req, res) { +// const newCustomerName = req.body.name; +// const newEmail = req.body.email; +// const newAddress = req.body.address; +// const newCity = req.body.city; +// const newPostcode = req.body.postcode; +// const newCountry = req.body.country; +// pool +// .query("SELECT * FROM customers WHERE name=$1", [newCustomerName]) +// .then((result) => { +// if (result.rows.length > 0) { +// return res +// .status(400) +// .send("An Customer with the same name already exists!"); +// } else { +// const query = +// "INSERT INTO customers (name, email, address, city, postcode, country) VALUES ($1, $2, $3)"; +// pool +// .query(query, [newCustomerName, newAddress, newCity, newPostcode, newCountry]) +// .then(() => res.send("Customer created!")) +// .catch((error) => { +// console.error(error); +// res.status(500).json(error); +// }); +// } +// }); +// }); +// // search hotel by id +// app.get("/hotels/:hotelId", function (req, res) { +// const hotelId = req.params.hotelId; +// pool +// .query("SELECT * FROM hotels WHERE id=$1", [hotelId]) +// .then((result) => res.json(result.rows)) +// .catch((error) => { +// console.error(error); +// res.status(500).json(error); +// }); +// }); +// app.get("/customers", function (req, res) { +// const customerNameQuery = req.query.name; +// let query = `SELECT * FROM customers ORDER BY name`; +// let params = []; +// if (customerNameQuery) { +// query = `SELECT * FROM customers WHERE name LIKE $1 ORDER BY name`; +// params.push(`%${customerNameQuery}%`); +// } +// pool +// .query(query, params) +// .then((result) => res.json(result.rows)) +// .catch((error) => { +// console.error(error); +// res.status(500).json(error); +// }); +// }); +// app.get("/customers/:customerId", function (req, res) { +// const customerId = req.params.customerId; +// pool +// .query("SELECT * FROM customers WHERE id=$1", [customerId]) +// .then((result) => res.json(result.rows)) +// .catch((error) => { +// console.error(error); +// res.status(500).json(error); +// }); +// }); +// app.get("/customers/:customerId/bookings", function (req, res) { +// const customerId = req.params.customerId; +// pool +// .query("SELECT * FROM customers c inner join bookings b on c.id = b.customer_id WHERE c.id=$1", [customerId]) +// .then((result) => res.json(result.rows)) +// .catch((error) => { +// console.error(error); +// res.status(500).json(error); +// }); +// }); +// // updating customers email +// app.put("/customers/:customerId", function (req, res) { +// const customerId = req.params.customerId; +// const newEmail = req.body.email; +// if(newEmail === "" || !newEmail.includes("@")){ +// return res +// .status(400) +// .send("Please insert a valid email address"); +// } +// pool +// .query("UPDATE customers SET email=$1 WHERE id=$2", [newEmail, customerId]) +// .then(() => res.send(`Customer ${customerId} updated!`)) +// .catch((error) => { +// console.error(error); +// res.status(500).json(error); +// }); +// }); +// app.listen(port, function() { +// console.log("Server is listening on port 3001. Ready to accept requests!"); +// }); + +//DELETE +// app.delete("/customers/:customerId", function (req, res) { +// const customerId = req.params.customerId; + +// pool +// .query("DELETE FROM bookings WHERE customer_id=$1", [customerId]) +// .then(() => pool.query("DELETE FROM customers WHERE id=$1", [customerId])) +// .then(() => res.send(`Customer ${customerId} deleted!`)) +// .catch((error) => { +// console.error(error); +// res.status(500).json(error); +// }); +// }); + +// //DELETE +// app.delete("/customers/:customerId", function (req, res) { +// const customerId = req.params.customerId; + +// pool +// .query("DELETE FROM customers WHERE id=$1", [customerId]) +// .then(() => res.send(`Customer ${customerId} deleted!`)) +// .catch((error) => { +// console.error(error); +// res.status(500).json(error); +// }); +// }); + + +// GET QUERY NAME + +// app.get("/products/", function(req, res) { +// const nameSearch = req.query.name; +// let query = `SELECT * FROM products ORDER BY name`; +// let params = []; +// if(nameSearch){ +// query = 'SELECT * FROM products WHERE name LIKE $1 ORDER BY name'; +// params.push(`%{nameSearch}%`) +// } + +// pool.query(query, params) +// .then((result) => { +// res.status(200).send(result.rows) +// }) +// .catch((error) =>{ +// console.error(error) +// res.status(500).send(error) +// }) + +// }); + + //GET WITH QUERY NAME + +// app.get("/hotels", function (req, res) { +// const hotelNameQuery = req.query.name; +// let query = `SELECT * FROM hotels ORDER BY name`; +// let params = []; +// if (hotelNameQuery) { +// query = `SELECT * FROM hotels WHERE name LIKE $1 ORDER BY name`; +// params.push(`%${hotelNameQuery}%`); +// } + +// pool +// .query(query, params) +// .then((result) => res.json(result.rows)) +// .catch((error) => { +// console.error(error); +// res.status(500).json(error); +// }); +// }); + +//GET WITH QUERY +// app.get("/products", function (req, res) { +// const prodId = req.query.name; + +// pool +// .query("SELECT * FROM hotels WHERE id=$1", [prodId]) +// .then((result) => res.json(result.rows)) +// .catch((error) => { +// console.error(error); +// res.status(500).json(error); +// }); +// }); \ No newline at end of file From b7f28bdb5d59ebda62a25e852fcdaf6848ba10f2 Mon Sep 17 00:00:00 2001 From: Mathias02 Date: Mon, 13 Mar 2023 12:34:25 +0200 Subject: [PATCH 3/3] server changes --- cyf-ecommerce-api/server.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/cyf-ecommerce-api/server.js b/cyf-ecommerce-api/server.js index a9ce78d9..5ac1ab3e 100644 --- a/cyf-ecommerce-api/server.js +++ b/cyf-ecommerce-api/server.js @@ -107,7 +107,7 @@ app.post("/availability", function (req, res) { const avProdSupp = req.body.supp_id; const avProdPrice = req.body.unit_price; - if (!Number.isInteger(avProdName) || newHotelRooms <= 0) { + if (!Number.isInteger(avProdName) || avProdName <= 0) { return res .status(400) .send("The number of the product should be a positive integer."); @@ -136,7 +136,7 @@ app.post("/availability", function (req, res) { app.post("/orders", function (req, res) { const orderDate = req.body.order_date; - const orderReferrence = req.body.order_reference; + const orderReference = req.body.order_reference; const orderCustomerId = req.body.customer_id; if (!Number.isInteger(orderCustomerId) || orderCustomerId <= 0) { @@ -146,7 +146,7 @@ app.post("/orders", function (req, res) { } pool - .query("SELECT * FROM orders WHERE order_date =$1", [orderReferrence]) + .query("SELECT * FROM orders WHERE order_date =$1", [orderReference]) .then((result) => { if (result.rows.length > 0) { return res @@ -156,7 +156,7 @@ app.post("/orders", function (req, res) { const query = "INSERT INTO orders (order_date, order_reference, customer_id) VALUES ($1, $2, $3)"; pool - .query(query, [orderDate, orderReferrence, orderCustomerId]) + .query(query, [orderDate, orderReference, orderCustomerId]) .then(() => res.send("order created!")) .catch((error) => { console.error(error); @@ -232,7 +232,6 @@ app.delete("/orders/:orderId", function (req, res) { app.delete("/customers/:customerId", function (req, res) { const customerId = req.params.customerId; - pool .query("DELETE FROM customers WHERE id=$1", [customerId]) .then(() => res.send(`Customer ${customerId} deleted!`)) @@ -243,7 +242,7 @@ app.delete("/customers/:customerId", function (req, res) { }); const listener = app.listen(port, () =>{ - console.log(`port is listening on port ${port}`) + console.log(`listening on port ${port}`) })