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..70e85ed --- /dev/null +++ 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 new file mode 100644 index 0000000..d94ea1b --- /dev/null +++ b/express/mau1-5427/index.js @@ -0,0 +1,32 @@ +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, DB_NAME } = 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"); +}); + +// 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/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..21a0b13 --- /dev/null +++ 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/router.js b/express/mau1-5427/product/router.js new file mode 100644 index 0000000..e18f60a --- /dev/null +++ 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({}).limit(10); + + 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 + }); +}); + diff --git a/express/mau1-5427/product/schema.js b/express/mau1-5427/product/schema.js new file mode 100644 index 0000000..3844c23 --- /dev/null +++ b/express/mau1-5427/product/schema.js @@ -0,0 +1,27 @@ +import mongoose from "mongoose"; +import { Decimal128 } 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