Skip to content

Commit f434bcf

Browse files
committed
File Uploaded
0 parents  commit f434bcf

File tree

2,687 files changed

+449740
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,687 files changed

+449740
-0
lines changed

.DS_Store

10 KB
Binary file not shown.

.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# .env
2+
JWT_KEY = secretKey
3+
MONGODB_ATLAS_PW = node-rest-shop

api/.DS_Store

10 KB
Binary file not shown.

api/controllers/ordersControllers.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
const mongoose = require('mongoose');
2+
const order = require('../models/order');
3+
4+
const Order = require('../models/order')
5+
const Product = require('../models/product')
6+
7+
const host = 'http://localhost:3000/orders/';
8+
9+
exports.orders_get_all = (req,res,next)=>{
10+
Order.find()
11+
.select('_id product quantity')
12+
.populate('product','name price productImage')
13+
.then(docs => {
14+
res.status(200).json({
15+
count:docs.length,
16+
orders:docs.map(doc => {
17+
return {
18+
_id: doc._id,
19+
product: doc.product,
20+
quantity: doc.quantity,
21+
request: {
22+
type: 'GET',
23+
url: host + doc._id
24+
}
25+
}
26+
})
27+
})
28+
})
29+
.catch(err => {
30+
res.status(500).json({error:err})
31+
})
32+
}
33+
34+
exports.orders_create_order = (req,res,next)=>{
35+
Product.findById(req.body.productId)
36+
.then(product => {
37+
if(!product){
38+
return res.status(404).json({
39+
message:'Product not found'
40+
})
41+
}
42+
const order = new Order({
43+
_id: mongoose.Types.ObjectId(),
44+
quantity:req.body.quantity,
45+
product:req.body.productId
46+
})
47+
return order.save()
48+
})
49+
.then(doc=>{
50+
res.status(201).json({
51+
message:'Order stored',
52+
createdOrder:{
53+
_id:doc._id,
54+
product:doc.product,
55+
quantity: doc.quantity
56+
},
57+
request: {
58+
type:'POST',
59+
url: host + doc._id
60+
}
61+
})
62+
})
63+
.catch(err=>{
64+
res.status(500).json({error: err})
65+
})
66+
}
67+
68+
exports.orders_get_order = (req,res,next)=>{
69+
Order.findById(req.params.orderId)
70+
.select('_id product quantity')
71+
.populate('product','name price productImage')
72+
.then(order => {
73+
if(!order){
74+
return res.status(404).json({
75+
message:'Order not found'
76+
})
77+
}
78+
res.status(200).json({
79+
order:order,
80+
request:{
81+
type: 'GET',
82+
url: host
83+
}
84+
})
85+
})
86+
.catch(err=>{
87+
res.status(500).json({
88+
error:err
89+
})
90+
})
91+
}
92+
93+
exports.orders_delete_order = (req,res,next)=>{
94+
Order.findById(req.params.orderId)
95+
.then(doc => {
96+
if(!doc){
97+
return res.status(404).json({
98+
message:'Order already deleted',
99+
})
100+
}
101+
return Order.remove({_id:req.params.orderId})
102+
})
103+
.then(doc => {
104+
res.status(200).json({
105+
message:'Order deleted',
106+
request:{
107+
type:'POST',
108+
url:host,
109+
body:{
110+
productId:'ID',
111+
quantity:'Number'
112+
}
113+
}
114+
})
115+
})
116+
.catch(err=>{
117+
res.status(500).json({
118+
error:err
119+
})
120+
})
121+
}
122+
123+
exports.orders_delete_all = (req,res,next)=>{
124+
Order.remove()
125+
.then(doc=>{
126+
res.status(200).json({
127+
message:'All products has been removed'
128+
})
129+
})
130+
.catch(err=>{
131+
res.status(500).json({
132+
error:err
133+
})
134+
})
135+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
const mongoose = require('mongoose')
2+
const Product = require('../models/product')
3+
4+
const host = 'http://localhost:3000/products/';
5+
6+
exports.products_get_all = (req,res,next)=>{
7+
Product.find()
8+
.select('_id name price productImage')
9+
.then(docs=>{
10+
const response = {
11+
count:docs.length,
12+
products: docs.map(doc =>{
13+
return{
14+
_id: doc._id,
15+
name:doc.name,
16+
price:doc.price,
17+
productImage:doc.productImage,
18+
request:{
19+
type: 'GET',
20+
url: host + doc._id
21+
}
22+
}
23+
})
24+
}
25+
res.status(201).json(response);
26+
})
27+
.catch(err=>{
28+
console.log(err);
29+
res.status(500).json({error:err})
30+
});
31+
}
32+
33+
exports.products_create_product = (req,res,next)=>{
34+
console.log(req.file);
35+
const product = new Product({
36+
_id: new mongoose.Types.ObjectId(),
37+
name: req.body.name,
38+
price: req.body.price,
39+
productImage: 'http://localhost:3000/'+ req.file.path
40+
});
41+
product.save()
42+
.then((docs)=>{
43+
console.log(docs);
44+
res.status(200).json({
45+
message:'Created product successfully',
46+
createdProduct:{
47+
_id: docs._id,
48+
name: docs.name,
49+
price:docs.price,
50+
productImage:docs.productImage,
51+
request:{
52+
type: 'POST',
53+
url: host+docs._id
54+
}
55+
}
56+
})
57+
}).catch((err)=>{
58+
console.log(err);
59+
res.status(500).json({error:err})
60+
});
61+
62+
63+
}
64+
65+
exports.products_get_product = (req,res,next)=>{
66+
const id = req.params.productId;
67+
Product.findById(id)
68+
.select('_id name price productImage')
69+
.then(docs =>{
70+
console.log(docs);
71+
if(docs){
72+
res.status(200).json({
73+
product:docs,
74+
request:{
75+
type:'GET',
76+
url:host + docs._id
77+
}
78+
});
79+
}
80+
else{
81+
res.status(404).json({message:'Not found'})
82+
}
83+
})
84+
.catch(err =>{
85+
console.log(err);
86+
res.status(500).json({err:err});
87+
})
88+
}
89+
90+
exports.products_edit_product = (req,res,next)=>{
91+
const id = req.params.productId;
92+
const updateOps = {};
93+
for(const ops of req.body){
94+
updateOps[ops.propName] = ops.value;
95+
}
96+
Product.update({_id:id},{$set: updateOps})
97+
.then(docs => {
98+
res.status(200).json({
99+
message:'Product updated',
100+
request:{
101+
type:'PATCH',
102+
url: host + docs.id
103+
}
104+
});
105+
})
106+
.catch(err=>{
107+
console.log(err);
108+
res.status(500).json({error:err});
109+
})
110+
}
111+
112+
exports.products_delete_product = (req,res,next)=>{
113+
const id = req.params.productId;
114+
115+
Product.findById(id)
116+
.then(doc=>{
117+
if(!doc){
118+
return res.status(404).json({
119+
message:'Product already deleted',
120+
})
121+
}
122+
return Product.remove({_id:id})
123+
})
124+
.then(docs=>{
125+
res.status(200).json({
126+
message: 'Product deleted',
127+
request:{
128+
type: 'POST',
129+
url:host,
130+
body:{
131+
name:'String',
132+
price:'Number'
133+
}
134+
}
135+
});
136+
})
137+
.catch(err=>{
138+
139+
console.log(err);
140+
res.status(500).json({error:err});
141+
throw err;
142+
})
143+
}

api/controllers/usersControllers.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
const mongoose = require('mongoose');
2+
const bcrypt = require('bcrypt');
3+
const jwt = require('jsonwebtoken');
4+
5+
const User = require('../models/user');
6+
7+
exports.users_create_user = (req,res,next) => {
8+
User.find({email:req.body.email})
9+
.then(data => {
10+
if(data.length > 0){
11+
return res.status(409).json({
12+
message:'Email exists'
13+
})
14+
}else{
15+
bcrypt.hash(req.body.password, 10, (err,hash)=>{
16+
if(err){
17+
return res.status(500).json({
18+
error:err
19+
})
20+
}else{
21+
const user = new User({
22+
_id: new mongoose.Types.ObjectId(),
23+
email: req.body.email,
24+
password: hash
25+
})
26+
user.save()
27+
.then(doc => {
28+
res.status(200).json({
29+
message:'User created',
30+
userInfo:doc
31+
})
32+
})
33+
.catch(err=>{
34+
res.status(500).json({
35+
error:err
36+
})
37+
})
38+
}
39+
})
40+
41+
}
42+
})
43+
}
44+
45+
exports.users_login_user = (req,res,next)=>{
46+
User.find({email:req.body.email})
47+
.then(user => {
48+
if(user.length < 1){
49+
return res.status(401).json({
50+
message: 'Auth failed'
51+
})
52+
}bcrypt.compare(req.body.password, user[0].password,(err,result)=> {
53+
if(err){
54+
return result.status(401).json({
55+
message: 'Auth failed'
56+
})
57+
}
58+
if(result){
59+
const token = jwt.sign({
60+
email:user[0].email,
61+
userId: user[0]._id
62+
},process.env.JWT_KEY,
63+
{
64+
expiresIn:'1h'
65+
})
66+
return res.status(200).json({
67+
message:'Auth successful',
68+
token:token,
69+
id:user[0]._id
70+
})
71+
}
72+
res.status(401).json({
73+
message: 'Auth failed'
74+
})
75+
})
76+
})
77+
.catch(err => {
78+
res.status(500).json({
79+
error:err
80+
})
81+
})
82+
}
83+
84+
exports.users_delete_user = (req,res,next)=> {
85+
User.remove({_id:req.params.userId})
86+
.then(doc => {
87+
res.status(200).json({
88+
message:'User has deleted'
89+
})
90+
})
91+
.catch(err => {
92+
res.status(500).json({
93+
error:err
94+
})
95+
})
96+
}

0 commit comments

Comments
 (0)