From 533d7614a29e9fa3d2f15b838137584677c41c47 Mon Sep 17 00:00:00 2001 From: Mauricio Bedoya Date: Tue, 4 Jul 2023 12:03:46 -0400 Subject: [PATCH 1/5] setting up Express server --- express/mau1-5427/.env.example | 5 +++++ express/mau1-5427/README.md | 0 express/mau1-5427/index.js | 24 ++++++++++++++++++++++++ express/mau1-5427/package.json | 19 +++++++++++++++++++ express/mau1-5427/product/model.js | 0 express/mau1-5427/product/router.js | 0 express/mau1-5427/product/schema.js | 0 7 files changed, 48 insertions(+) create mode 100644 express/mau1-5427/.env.example create mode 100644 express/mau1-5427/README.md create mode 100644 express/mau1-5427/index.js create mode 100644 express/mau1-5427/package.json create mode 100644 express/mau1-5427/product/model.js create mode 100644 express/mau1-5427/product/router.js create mode 100644 express/mau1-5427/product/schema.js diff --git a/express/mau1-5427/.env.example b/express/mau1-5427/.env.example new file mode 100644 index 0000000..e7f8640 --- /dev/null +++ b/express/mau1-5427/.env.example @@ -0,0 +1,5 @@ +PORT=3001 +MONGO_USER= +MONGO_PASSWORD= +MONGO_HOSTNAME= +DB_NAME= \ No newline at end of file diff --git a/express/mau1-5427/README.md b/express/mau1-5427/README.md new file mode 100644 index 0000000..e69de29 diff --git a/express/mau1-5427/index.js b/express/mau1-5427/index.js new file mode 100644 index 0000000..b1aaafa --- /dev/null +++ b/express/mau1-5427/index.js @@ -0,0 +1,24 @@ +import dotenv from "dotenv"; +dotenv.config(); +import express from "express"; +import mongoose from "mongoose"; + +// environment variables +const { PORT, MONGO_USER, MONGO_PASSWORD, MONGO_HOSTNAME } = process.env; + +const MONGO_URI = `mongodb+srv://${MONGO_USER}:${MONGO_PASSWORD}@${MONGO_HOSTNAME}/?retryWrites=true&w=majority`; + +const app = express(); +app.use(express.json()); + +app.get("/", (req, res) => { + res.send("API Running"); +}); + + +app.listen(PORT, + () => { + console.log(`Server running at port ${PORT} 💻`); + console.log(`http://localhost:${PORT}`); + } +) \ No newline at end of file diff --git a/express/mau1-5427/package.json b/express/mau1-5427/package.json new file mode 100644 index 0000000..72dea3e --- /dev/null +++ b/express/mau1-5427/package.json @@ -0,0 +1,19 @@ +{ + "name": "mau1-5427", + "version": "1.0.0", + "description": "", + "main": "index.js", + "type": "module", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "dev":"node --watch index.js" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "dotenv": "^16.3.1", + "express": "^4.18.2", + "mongoose": "^7.3.1" + } +} diff --git a/express/mau1-5427/product/model.js b/express/mau1-5427/product/model.js new file mode 100644 index 0000000..e69de29 diff --git a/express/mau1-5427/product/router.js b/express/mau1-5427/product/router.js new file mode 100644 index 0000000..e69de29 diff --git a/express/mau1-5427/product/schema.js b/express/mau1-5427/product/schema.js new file mode 100644 index 0000000..e69de29 From 5036b33135428daf5d8a5a796cccf434d33de97f Mon Sep 17 00:00:00 2001 From: Mauricio Bedoya Date: Tue, 4 Jul 2023 12:44:53 -0400 Subject: [PATCH 2/5] product schema and model created --- express/mau1-5427/product/model.js | 6 ++++++ express/mau1-5427/product/schema.js | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/express/mau1-5427/product/model.js b/express/mau1-5427/product/model.js index e69de29..21a0b13 100644 --- a/express/mau1-5427/product/model.js +++ b/express/mau1-5427/product/model.js @@ -0,0 +1,6 @@ +import mongoose from "mongoose"; +import productSchema from "./schema.js"; + +const productModel = mongoose.model("product", productSchema); + +export default productModel; \ No newline at end of file diff --git a/express/mau1-5427/product/schema.js b/express/mau1-5427/product/schema.js index e69de29..fc3b04a 100644 --- a/express/mau1-5427/product/schema.js +++ b/express/mau1-5427/product/schema.js @@ -0,0 +1,26 @@ +import mongoose from "mongoose"; + +const productSchema = mongoose.Schema( { + product_id: { + type: String, + unique: true, + required: true + }, + name: { + type: String, + required: true, + }, + description: { + type: String, + required: true + }, + tags: { + type: [String] + }, + value: { + type: Decimal128, + required: true + } +}) + +export default productSchema; \ No newline at end of file From f2e0edebdbb22ef1fe11d768e6c1a22e5411100a Mon Sep 17 00:00:00 2001 From: Mauricio Bedoya Date: Tue, 4 Jul 2023 13:09:38 -0400 Subject: [PATCH 3/5] express router created --- express/mau1-5427/product/router.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/express/mau1-5427/product/router.js b/express/mau1-5427/product/router.js index e69de29..b82e943 100644 --- a/express/mau1-5427/product/router.js +++ b/express/mau1-5427/product/router.js @@ -0,0 +1,29 @@ +import express from "express"; +import productModel from "./model.js"; +// crypto to generate the "product_id" +import crypto from "crypto"; + +export const productRouter = express.Router(); + +// List / READ all +productRouter.get("/", async (req, res) => { + const products = await productModel.find({}); + + res.status(200).json({ + products + }); +}); + +// CREATE +productRouter.post("/", async (req, res) => { + const product_id = crypto.randomBytes(16).toString("base64url"); + + const productToSave = { ...req.body, product_id }; + + const createdProduct = await productModel.create(productToSave); + + res.status(201).json({ + product: createdProduct + }); +}); + From 5de29d27a41e109b2170add817ddd5f9832fd4a2 Mon Sep 17 00:00:00 2001 From: Mauricio Bedoya Date: Wed, 5 Jul 2023 08:27:05 -0400 Subject: [PATCH 4/5] connecting server to DB --- express/mau1-5427/README.md | 23 +++++++++++++++++++++++ express/mau1-5427/index.js | 10 +++++++++- express/mau1-5427/product/schema.js | 1 + 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/express/mau1-5427/README.md b/express/mau1-5427/README.md index e69de29..70e85ed 100644 --- a/express/mau1-5427/README.md +++ b/express/mau1-5427/README.md @@ -0,0 +1,23 @@ +# Semana 8: Crear y Listar productos con Mongoose + +Base de datos creada en MongoDB: `warehouse -> products` + +Crear producto, ejemplo: + +```bash +curl --request POST \ + http://localhost:3001/products \ + --header "Content-Type: application/json" \ + --data '{ + "name": "Olive Oil Dispenser Bottle", + "description": "Leaflai oil bottle is made of lead-free glass and is 100% healthy and environmentally friendly.", + "tags": ["kitchen", "picnic", "home"], + "value": 15.99 + }' +``` + +Listar productos: + +```bash +http://localhost:3001/products +``` diff --git a/express/mau1-5427/index.js b/express/mau1-5427/index.js index b1aaafa..d94ea1b 100644 --- a/express/mau1-5427/index.js +++ b/express/mau1-5427/index.js @@ -2,9 +2,11 @@ import dotenv from "dotenv"; dotenv.config(); import express from "express"; import mongoose from "mongoose"; +// router +import { productRouter } from "./product/router.js"; // environment variables -const { PORT, MONGO_USER, MONGO_PASSWORD, MONGO_HOSTNAME } = process.env; +const { PORT, MONGO_USER, MONGO_PASSWORD, MONGO_HOSTNAME, DB_NAME } = process.env; const MONGO_URI = `mongodb+srv://${MONGO_USER}:${MONGO_PASSWORD}@${MONGO_HOSTNAME}/?retryWrites=true&w=majority`; @@ -15,10 +17,16 @@ app.get("/", (req, res) => { res.send("API Running"); }); +// set up router +app.use("/products", productRouter); app.listen(PORT, () => { console.log(`Server running at port ${PORT} 💻`); console.log(`http://localhost:${PORT}`); + + mongoose.connect(MONGO_URI, { + dbName: `${DB_NAME}` + }) } ) \ No newline at end of file diff --git a/express/mau1-5427/product/schema.js b/express/mau1-5427/product/schema.js index fc3b04a..3844c23 100644 --- a/express/mau1-5427/product/schema.js +++ b/express/mau1-5427/product/schema.js @@ -1,4 +1,5 @@ import mongoose from "mongoose"; +import { Decimal128 } from "mongoose"; const productSchema = mongoose.Schema( { product_id: { From 5175d6f20ea45d206d56d7fac2bb898e1095508e Mon Sep 17 00:00:00 2001 From: Mauricio Bedoya Date: Sat, 8 Jul 2023 19:14:52 -0400 Subject: [PATCH 5/5] limit added --- express/mau1-5427/product/router.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/express/mau1-5427/product/router.js b/express/mau1-5427/product/router.js index b82e943..e18f60a 100644 --- a/express/mau1-5427/product/router.js +++ b/express/mau1-5427/product/router.js @@ -7,7 +7,7 @@ export const productRouter = express.Router(); // List / READ all productRouter.get("/", async (req, res) => { - const products = await productModel.find({}); + const products = await productModel.find({}).limit(10); res.status(200).json({ products