-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.js
88 lines (77 loc) · 2.1 KB
/
user.js
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
function createUser(){
//add a new user to mongoDB
}
async function checkUserExist(client,username){
let db = client.db("vue-users");
let collection = db.collection("my-collection");
console.log("querying db",username);
const count = await collection.countDocuments({name:username});
if(count == 0){
return 0;
}
else{
return 1;
}
};
async function createUser(client,username){
let db = client.db("vue-users");
let collection = db.collection("my-collection");
console.log("creating user",username);
const result = await collection.insertOne({name:username});
if(result.acknowledged){
console.log("inserted user, id is",result.insertedId);
return result.insertedId;
}
else{
return false;
}
}
async function getToDo(client,username){
let db = client.db("vue-users");
let collection = db.collection("todos");
console.log("querying todo for",username);
const cursor = collection.find({name:username,done:{$eq:false}});
let result = [];
await cursor.forEach((doc)=>{
result.push(doc);
})
return result;
}
async function addToDo(client,task,username){
let db = client.db("vue-users");
let collection = db.collection("todos");
let doc = {
task:task,
name:username,
done:false
}
let result = await collection.insertOne(doc);
if(result.acknowledged){
return result.insertedId;
}
else{
return false;
}
}
async function removeToDo(client,task,username){
let db = client.db("vue-users");
let collection = db.collection("todos");
let query = {
task:task,
name:username
};
let result = await collection.deleteOne(query);
if(result.acknowledged){
console.log("deletion count:",result.deletedCount);
if(result.deletedCount == 1){
return "success"
}
else{
return "error"
}
}
else{
console.log("deletion not acknowledged");
}
}
module.exports = {checkUserExist,createUser,getToDo,addToDo,removeToDo};