Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

learning-mongodb #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

Open
wants to merge 2 commits into
base: node-5
Choose a base branch
Loading
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
2 changes: 2 additions & 0 deletions 2 .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PORT=8080
MONGO_PASSWORD=Haiderali_560
93 changes: 61 additions & 32 deletions 93 controller/product.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,69 @@
const fs = require('fs');
// const index = fs.readFileSync('index.html', 'utf-8');
const data = JSON.parse(fs.readFileSync('data.json', 'utf-8'));
const products = data.products;
const model = require('../model/product')

exports.createProduct = (req, res) => {
console.log(req.body);
products.push(req.body);
res.status(201).json(req.body);
};
console.log(model.Product);
const Product = model.Product;

exports.createProduct = async (req, res) => {
try {
const product = new Product(req.body);
const dbResponse = await product.save();
res.status(201).json(dbResponse);
}
catch (error) {
res.status(201).json(error);
}
}

exports.getAllProducts = (req, res) => {
res.json(products);
exports.getAllProducts = async (req, res) => {
try {
const dbResponse = await Product.find();
res.status(201).json(dbResponse);
}
catch (error) {
res.status(201).json(error);
}
};

exports.getProduct = (req, res) => {
const id = +req.params.id;
const product = products.find((p) => p.id === id);
res.json(product);
exports.getProduct = async (req, res) => {
try {
const id = req.params.id;
const dbResponse = await Product.findById(id);
res.status(201).json(dbResponse);
}
catch (error) {
res.status(201).json(error);
}
};
exports.replaceProduct = (req, res) => {
const id = +req.params.id;
const productIndex = products.findIndex((p) => p.id === id);
products.splice(productIndex, 1, { ...req.body, id: id });
res.status(201).json();

exports.replaceProduct = async (req, res) => {
try {
const id = req.params.id;
const dbResponse = await Product.findOneAndReplace({ _id: id }, req.body, { new: true });
res.status(201).json(dbResponse);
}
catch (error) {
res.status(400).json(error);
}
};
exports.updateProduct = (req, res) => {
const id = +req.params.id;
const productIndex = products.findIndex((p) => p.id === id);
const product = products[productIndex];
products.splice(productIndex, 1, { ...product, ...req.body });
res.status(201).json();

exports.updateProduct = async (req, res) => {
try {
const id = req.params.id;
const dbResponse = await Product.findOneAndUpdate({ _id: id }, req.body, { new: true });
res.status(201).json(dbResponse);
}
catch (error) {
res.status(400).json(error);
}
};
exports.deleteProduct = (req, res) => {
const id = +req.params.id;
const productIndex = products.findIndex((p) => p.id === id);
const product = products[productIndex];
products.splice(productIndex, 1);
res.status(201).json(product);

exports.deleteProduct = async (req, res) => {
try {
const id = req.params.id;
const dbResponse = await Product.findOneAndDelete({ _id: id });
res.status(201).json(dbResponse);
}
catch (error) {
res.status(400).json(error);
}
};
16 changes: 14 additions & 2 deletions 16 index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
require('dotenv').config();
const express = require('express');
const morgan = require('morgan');
const mongoose = require('mongoose');
const server = express();
const productRouter = require('./routes/product')
const userRouter = require('./routes/user')

main().catch((err) => console.log(err))

async function main() {
await mongoose.connect('mongodb+srv://haider:Haiderali_560@cluster0.otwulzw.mongodb.net/e-commerce?retryWrites=true&w=majority');
console.log('connected')
}



//bodyParser
server.use(express.json());
server.use(morgan('default'));
server.use(express.static('public'));
server.use('/products',productRouter.router);
server.use('/users',userRouter.router);
server.use('/products', productRouter.router);
server.use('/users', userRouter.router);

console.log(process.env.MONGO_PASSWORD)
server.listen(8080, () => {
console.log('server started');
});
17 changes: 17 additions & 0 deletions 17 model/product.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const mongoose = require('mongoose');

const { Schema } = mongoose;

const ProductSchema = new Schema({
title: { type: String, required: true },
description: String,
price: { type: Number, min: [0, 'Price should be greater than zero.'], required: true },
discountPercentage: { type: Number, min: [0, 'wrong min value',], max: [50, 'wrong max value'] },
rating: { type: Number, min: [0, 'wrong min rating'], max: [5, 'wrong max rating'] },
brand: { type: String, required: true },
category: { type: String, required: true },
thumbnail: { type: String, required: true },
images: [String]
});

exports.Product = mongoose.model('Product', ProductSchema)
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.