-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolvers.js
134 lines (99 loc) · 3.84 KB
/
resolvers.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const jwt = require('jsonwebtoken');
const bcrypt = require('bcrypt')
const createToken = (user, secret, expiresIn) => {
const { username, email } = user;
return jwt.sign({username, email }, secret, {expiresIn} )
}
exports.resolvers = {
Query: {
getAllPets: async (root, args, {Pet}) => {
const allPets = await Pet.find().sort({createdDate: 'desc'});
return allPets;
},
getCurrentUser: async (root, args, {currentUser, User}) => {
if (!currentUser) {
return null
}
const user = await User.findOne({email: currentUser.email})
.populate({
path: 'favorites',
model: "Pet"
})
return user;
},
getUserPosts: async (root, {username}, {Pet}) => {
const userPosts = await Pet.find({username}).sort({
createdDate: 'desc'
});
return userPosts;
},
getPet: async (root, {_id}, {Pet}) => {
const pet = await Pet.findOne({_id})
return pet;
},
searchPets: async(root, {searchParam}, {Pet}) => {
if (searchParam) {
const searchResults = await Pet.find({
$text: {$search: searchParam}
}, {
score: {$meta: "textScore"}
}).sort({
score: {$meta: "textScore"}
})
return searchResults;
} else {
const pets = await Pet.find().sort({createdDate: 'desc'});
return pets;
}
}
},
Mutation: {
addPet: async (root, {name, desc, category, text, username, imageUrl},{ Pet }) => {
const newPet = await new Pet({
name, desc, category, text, username, imageUrl
}).save()
return newPet
},
likePet: async (root, {_id, username}, {Pet, User}) => {
const pet = await Pet.findOneAndUpdate({_id}, {$inc: {likes: 1}})
const user = await User.findOneAndUpdate({username}, {$addToSet: {favorites: _id}});
return pet;
},
unlikePet: async (root, {_id, username}, {Pet, User}) => {
const pet = await Pet.findOneAndUpdate({_id}, {$inc: {likes: -1}})
const user = await User.findOneAndUpdate({username}, {$pull: {favorites: _id}});
return pet;
},
deleteUserPost: async (root, {_id}, {Pet}) => {
const post = await Pet.findOneAndRemove({_id});
return post;
},
updateUserPost: async (root, {_id, name, imageUrl, category, desc, text, username}, {Pet}) => {
const udpdatedPost = await Pet.findOneAndUpdate({_id}, {$set: {name, imageUrl, desc, category, text, username}}, {new: true});
return updatedPost;
},
signinUser: async (root, {email, password}, {User}) => {
const user = await User.findOne({email});
if(!user) {
throw new Error('User not found')
}
const isValidPassword = await bcrypt.compare(password, user.password);
if (!isValidPassword) {
throw new Error('invalid password')
}
return { token: createToken(user, process.env.SECRET, '12hr')}
},
signupUser: async (root, {username, email, password}, {User}) => {
const user = await User.findOne({username});
if(user) {
throw new Error('User already exist')
}
const newUser = new User({
username,
email,
password
}).save();
return { token: createToken(newUser, process.env.SECRET, '12hr')}
}
}
};