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

Node 5 #13

Open
wants to merge 7 commits into
base: main
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
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PORT=8080
MONGO_PASSWORD=Haiderali_560
384 changes: 0 additions & 384 deletions README.md

This file was deleted.

69 changes: 69 additions & 0 deletions controller/product.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const model = require('../model/product')

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 = async (req, res) => {
try {
const dbResponse = await Product.find();
res.status(201).json(dbResponse);
}
catch (error) {
res.status(201).json(error);
}
};

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 = 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 = 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 = 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);
}
};
40 changes: 40 additions & 0 deletions controller/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const fs = require('fs');
// const index = fs.readFileSync('index.html', 'utf-8');
const data = JSON.parse(fs.readFileSync('data.json', 'utf-8'));
const users = data.users;

exports.createUser = (req, res) => {
console.log(req.body);
users.push(req.body);
res.status(201).json(req.body);
};

exports.getAllUsers = (req, res) => {
res.json(users);
};

exports.getUser = (req, res) => {
const id = +req.params.id;
const user = users.find((p) => p.id === id);
res.json(user);
};
exports.replaceUser = (req, res) => {
const id = +req.params.id;
const userIndex = users.findIndex((p) => p.id === id);
users.splice(userIndex, 1, { ...req.body, id: id });
res.status(201).json();
};
exports.updateUser = (req, res) => {
const id = +req.params.id;
const userIndex = users.findIndex((p) => p.id === id);
const user = users[userIndex];
users.splice(userIndex, 1, { ...user, ...req.body });
res.status(201).json();
};
exports.deleteUser = (req, res) => {
const id = +req.params.id;
const userIndex = users.findIndex((p) => p.id === id);
const user = users[userIndex];
users.splice(userIndex, 1);
res.status(201).json(user);
};
Loading