Skip to content

Instantly share code, notes, and snippets.

View paulobunga's full-sized avatar
🏠
Available for work

Paul Obunga paulobunga

🏠
Available for work
View GitHub Profile
@paulobunga
paulobunga / package.json
Created December 18, 2019 00:41
Current package.js
{
"name": "node_api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
@paulobunga
paulobunga / index.js
Last active December 18, 2019 00:37
Main index.js file
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'));
@paulobunga
paulobunga / router.js
Last active December 18, 2019 01:07
API endpoints for node_api
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 );
@paulobunga
paulobunga / complete_controller.js
Created December 18, 2019 00:27
Complete controller code
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,
});
@paulobunga
paulobunga / delete_product.js
Created December 18, 2019 00:26
Delete single product by product ID
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' });
@paulobunga
paulobunga / update_product.js
Created December 18, 2019 00:24
Update single product using product ID
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) => {
@paulobunga
paulobunga / view_single_product.js
Created December 18, 2019 00:19
Code to return single product details based on the ID
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,
@paulobunga
paulobunga / view_products.js
Created December 18, 2019 00:14
Code to query products from mongoose
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' });
@paulobunga
paulobunga / create_product.js
Created December 18, 2019 00:05
Code for creating a single product
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) => {
@paulobunga
paulobunga / product_controller.js
Created December 18, 2019 00:00
Boilerplate code for product controller
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) => {};