generated from glrodasz/semana-template
-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Semana 8: Crear y listar productos #6
Open
MauBedoya
wants to merge
6
commits into
undefined-academy:main
Choose a base branch
from
MauBedoya:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
533d761
setting up Express server
MauBedoya 5036b33
product schema and model created
MauBedoya f2e0ede
express router created
MauBedoya 5de29d2
connecting server to DB
MauBedoya 5175d6f
limit added
MauBedoya bc34703
Merge branch 'undefined-academy:main' into main
MauBedoya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
PORT=3001 | ||
MONGO_USER= | ||
MONGO_PASSWORD= | ||
MONGO_HOSTNAME= | ||
DB_NAME= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}` | ||
}) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import mongoose from "mongoose"; | ||
import productSchema from "./schema.js"; | ||
|
||
const productModel = mongoose.model("product", productSchema); | ||
|
||
export default productModel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
}) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aunque es un ejercicio, siempre es bueno almacenar la fecha de alta del registro, ya que cuando menos lo esperes vas a ocupar saber en que fecha se dio de alta o se modifico el registro Esto es a criterio. |
||
export default productSchema; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unicamente sugiero se agregue el sort u ordenamiento por alguna columna de la lista de productos