Skip to content
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

Alberto-Perdomo-Platzi-Master-practical-test #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
//Routes
const products_api_router = require('./routes/api/products')
const categories_api_router = require('./routes/api/categories')

//Variables
const express = require('express');
const cors = require('cors');

//App function
function createApp() {
const app = express();
app.use(cors());
app.use(express.json());

// ADD YOUR ROUTES
//Products
app.use("/api/products", products_api_router)
//Categories
app.use("/api/categories", categories_api_router)
return app;
}

Expand Down
73 changes: 73 additions & 0 deletions src/controllers/api/category_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//Declaration of variables
const { object_id } = require('mongodb')
const mongo_client = require('../../db/mongo')
const collection = "categories"
const collection_products = "products"
const db_name = process.env.MONGO_DB_NAME

//Controller settings
const controller = {
list: async (req, res) => {
await mongo_client.connect()
const database = mongo_client.db( db_name )
const categories = database.collection( collection )

categories.find().toArray( (err, categories) => {
res.status(200).json(categories)
} )
},
show: async (req, res) => {
const { category_id } = reg.params
await mongo_client.connect()
const database = mongo_client.db( db_name )
const categories = database.collection( collection )

categories.findOne({ _id: object_id( category_id ) }, (err, category) => {
res.status(200).json(category)
})
},
store: async (req, res) => {
await mongo_client.connect()
const database = mongo_client.db( db_name )
const categories = database.collection( collection )

categories.insertOne({ ...req.body }, (err, category) => {
res.status(201).json(category.ops[0])
})
},
update: async (req, res) => {
const { category_id } = req.params
await mongo_client.connect()
const database = mongo_client.db( db_name )
const categories = database.collection( collection )

categories.updateOne({ _id: object_id(category_id) }, { $set: { ...req.body } })
categories.findOne({ _id: object_id( category_id ) }, (err, category) => {
res.status(200).json(category)
})
},
destroy: async (req, res) => {
const { category_id } = req.params
await mongo_client.connect()
const database = mongo_client.db( db_name )
const categories = database.collection( collection )

categories.deleteOne({ _id: object_id(category_id) }, (err, categories) => {
if( categories.deletedCount === 1 ){
res.status(200).json(true)
}
})
},
list_products: async (req, res) => {
const { category_id } = req.params
await mongo_client.connect()
const database = mongo_client.db( db_name )
const products = database.collection( collection_products )

products.find({ category_id: category_id }).toArray( (err, products) => {
res.status(200).json(products)
})
}
}

module.exports = controller
62 changes: 62 additions & 0 deletions src/controllers/api/product_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//Declaration of variables
const { object_id } = require('mongodb')
const mongo_client = require('../../db/mongo')
const collection = "products"
const db_name = process.env.MONGO_DB_NAME

//Controller settings
const controller = {
list: async (req, res) => {
await mongo_client.connect()
database = mongo_client.db(db_name)
const products = database.collection(collection)

products.find().toArray((err, products ) => {
res.status(200).json(products)
})
},
show: async (req, res) => {
const { product_id } = req.params
await mongo_client.connect()
database = mongo_client.db(db_name)
const products = database.collection(collection)

const product = await products.findOne({_id: object_id(product_id)})

res.status(200).json(product)
},
store: async (req, res) => {
await mongo_client.connect()
database = mongo_client.db(db_name)
const products = database.collection(collection)

products.insertOne({ ...req.body }, (err, product) => {
res.status(201).json(product.ops[0])
})
},
update: async (req, res) => {
const { product_id } = req.params
await mongo_client.connect()
database = mongo_client.db(db_name)
const products = database.collection(collection)

await products.updateOne({_id: object_id(product_id)},{$set:{...req.body}})
products.findOne({_id: object_id(product_id)}, (err, product) => {
res.status(200).json(product)
})
},
destroy: async (req, res) => {
const { product_id } = req.params
await mongo_client.connect()
database = mongo_client.db(db_name)
const products = database.collection(collection)

products.deleteOne({ _id: object_id(product_id) }, (err, deleted) => {
if(deleted.deletedCount === 1){
res.status(200).json(true)
}
})
}
}

module.exports = controller
16 changes: 16 additions & 0 deletions src/db/mongo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//Declaration of db user settings
const { MongoClient, ObjectId } = require('mongodb')
const { config } = require('../config')

const USER = encodeURIComponent(config.dbUser)
const PASSWORD = encodeURIComponent(config.dbPassword)

//Declaration of db client settings
const MONGO_URI = `${config.dbConnection}://${USER}:${PASSWORD}@${config.dbHost}:${config.dbPort}?retryWrites=true&w=majority`

const client = new MongoClient( MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
} )

module.exports = client
14 changes: 14 additions & 0 deletions src/routes/api/categories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//Declaration of variables
const express = require('express')
const router = express.Router()
const api_categories_controller = require('../../controllers/api/category_controller')

//Router settings
router.get('/', api_categories_controller.list )
router.get('/:categoryId', api_categories_controller.show )
router.post('/', api_categories_controller.store )
router.put('/:categoryId', api_categories_controller.update )
router.delete('/:categoryId', api_categories_controller.destroy )
router.get('/:categoryId/products', api_categories_controller.list_products )

module.exports = router
13 changes: 13 additions & 0 deletions src/routes/api/products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//Declaration of variables
const express = require('express')
const router = express.Router()
const api_products_controller = require('../../controllers/api/product_controller')

//Router settings
router.get('/', api_products_controller.list )
router.get('/:productId', api_products_controller.show )
router.post('/', api_products_controller.store )
router.put('/:productId', api_products_controller.update )
router.delete('/:productId', api_products_controller.destroy )

module.exports = router