-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongoFun.js
More file actions
113 lines (96 loc) · 2.99 KB
/
mongoFun.js
File metadata and controls
113 lines (96 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const { MongoClient, ObjectID } = require('mongodb')
const credentials = require('./credentials')
const uri = `mongodb+srv://${credentials.username}:${credentials.password}@cluster0.vzvup.mongodb.net/ecart?retryWrites=true&w=majority`
const Client = new MongoClient(uri, {useNewUrlParser: true, useUnifiedTopology: true})
exports.getAll = async function getAll() {
try {
const client = await Client.connect()
const col = client.db("ecart").collection("products")
let data = await col.find().toArray()
return data
}
catch (e) {
console.error(e)
}
// finally {
// await Client.close()
// }
}
exports.getUserItems = async function getUserItems(email) {
try {
const client = await Client.connect()
const col = client.db("ecart").collection("products")
let data = await col.find({email: email}).toArray()
return data
}
catch (e) {
console.error(e)
}
}
exports.getWishlist = async function getWishlist(email) {
try {
const client = await Client.connect()
const col = client.db("ecart").collection("products")
let data = await col.find({wishlistUsers: email}).toArray()
return data
}
catch (e) {
console.error(e)
}
}
exports.insertProduct = async function insertProduct(product) {
try {
const client = await Client.connect()
const col = client.db("ecart").collection("products")
let data = await col.insertOne(product)
return data
}
catch (e) {
console.error(e)
}
}
exports.insertWishlist = async function insertWishlist(product) {
try {
const client = await Client.connect()
const col = client.db("ecart").collection("products")
let data = await col.updateOne({_id : new ObjectID(product.id)},{$push: {wishlistUsers: product.email}})
return data
}
catch (e) {
console.error(e)
}
}
exports.deleteProduct = async function deleteProduct(id) {
try {
const client = await Client.connect()
const col = client.db("ecart").collection("products")
let data = await col.remove({_id : new ObjectID(id)})
return data
}
catch (e) {
console.error(e)
}
}
exports.deleteWishlist = async function deleteWishlist(id,email) {
try {
const client = await Client.connect()
const col = client.db("ecart").collection("products")
let data = await col.updateOne({_id : new ObjectID(id)},{$pull: {wishlistUsers: email}})
return data
}
catch (e) {
console.error(e)
}
}
exports.updateProduct = async function updateProduct(id, product) {
try {
const client = await Client.connect()
const col = client.db("ecart").collection("products")
let data = await col.updateOne({_id : new ObjectID(id)},{$set: product})
return data
}
catch (e) {
// console.log("=================================")
console.error(e)
}
}