This file contains hidden or 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
| { | |
| "name": "node_api", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "index.js", | |
| "scripts": { | |
| "test": "echo \"Error: no test specified\" && exit 1" | |
| }, | |
| "keywords": [], | |
| "author": "", |
This file contains hidden or 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
| const express = require('express'); | |
| const mongoose = require('mongoose'); | |
| const bodyParser = require('body-parser'); | |
| const router = require('./router'); | |
| const app = express(); | |
| mongoose | |
| .connect('mongodb://localhost:27017/node_api') | |
| .then(() => console.log('Connected to database')); |
This file contains hidden or 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
| const express = require('express'); | |
| const router = express.Router(); | |
| const productCtrl = require('./product_controller'); | |
| router.get('/', productCtrl.viewProducts); | |
| router.get('/details/:productId', productCtrl.viewSingleProduct); | |
| router.patch('/update/:productId', productCtrl.updateProduct); | |
| router.delete( '/delete/:productId', productCtrl.deleteProduct ); | |
| router.post( '/new', productCtrl.createProduct ); |
This file contains hidden or 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
| const Product = require('./product_model'); | |
| module.exports.createProduct = async (req, res, next) => { | |
| const { name, sale_price, cost_price, quantity } = req.body; | |
| const newProduct = new Product({ | |
| name, | |
| sale_price, | |
| cost_price, | |
| quantity, | |
| }); |
This file contains hidden or 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
| module.exports.deleteProduct = async (req, res, next) => { | |
| let { productId } = req.params; | |
| Product.findByIdAndDelete(productId, (error, result) => { | |
| if (error) { | |
| res.json({ status: 'ERROR', message: 'Error deleting product' }); | |
| } | |
| if (result) { | |
| res.json({ status: 'SUCCESS', message: 'Product deleted' }); |
This file contains hidden or 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
| module.exports.updateProduct = async (req, res, next) => { | |
| Product.findOneAndUpdate( | |
| { _id: req.params.productId }, | |
| { | |
| name: req.body.name, | |
| sale_price: req.body.sale_price, | |
| cost_price: req.body.cost_price, | |
| quantity: req.body.quantity, | |
| } | |
| ).exec((error, result) => { |
This file contains hidden or 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
| Product.findOne({ _id: req.params.productId }).exec((error, result) => { | |
| if (error) { | |
| res.json({ status: 'ERROR', message: 'Error getting product' }); | |
| } | |
| if (result) { | |
| res.json({ | |
| status: 'SUCCESS', | |
| message: 'Product found', | |
| product: result, |
This file contains hidden or 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
| module.exports.viewProducts = async ( req, res, next ) => { | |
| Product.find( {} ).exec( ( error, result ) => { | |
| if (error) { | |
| res.json({ status: 'ERROR', message: 'Error getting products' }); | |
| } | |
| if (result.length > 0) { | |
| res.json({ status: 'SUCCESS', message: 'Products found', products: result }); | |
| } else { | |
| res.json({ status: 'NO_PRODUCTS', message: 'No products found' }); |
This file contains hidden or 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
| module.exports.createProduct = async (req, res, next) => { | |
| const { name, sale_price, cost_price, quantity } = req.body; | |
| const newProduct = new Product({ | |
| name, | |
| sale_price, | |
| cost_price, | |
| quantity, | |
| }); | |
| newProduct.save((error, result) => { |
This file contains hidden or 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
| const Product = require('./product_model'); | |
| module.exports.createProduct = async (req, res, next) => {}; | |
| module.exports.viewProducts = async (req, res, next) => {}; | |
| module.exports.viewSingleProduct = async (req, res, next) => {}; | |
| module.exports.updateProduct = async (req, res, next) => {}; |