forked from ahendouz/dribbble-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolvers.js
142 lines (129 loc) · 3.72 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
135
136
137
138
139
140
141
142
const jwt = require("jsonwebtoken");
const bcrypt = require("bcrypt");
const createToken = (user, secret, duration) => {
const { username, email } = user;
return jwt.sign({ username, email }, secret, { expiresIn: duration });
};
exports.resolvers = {
Query: {
getAllShots: async (root, args, { Shot }) => {
const allShots = await Shot.find().sort({
createdDate: "desc"
// createdDate: "esc"
});
return allShots;
},
getShot: async (root, { _id }, { Shot }) => {
const shot = await Shot.findOne({ _id });
return shot;
},
searchShots: async (root, { searchTerm }, { Shot }) => {
if (searchTerm) {
const searchResults = await Shot.find(
{
$text: { $search: searchTerm }
},
{
score: { $meta: "textScore" }
}
).sort({
score: { $meta: "textScore" }
});
return searchResults;
} else {
const shots = await Shot.find().sort({
likes: "desc",
createdDate: "desc"
});
return shots;
}
},
getUserShots: async (root, { username }, { Shot }) => {
const userShots = await Shot.find({ username }).sort({
createdDate: "desc"
});
return userShots;
},
getCurrentUser: async (root, args, { currentUser, User }) => {
if (!currentUser) {
return null;
}
const user = await User.findOne({
// find user in our database
username: currentUser.username
}).populate({
path: "favorites",
model: "Shot"
});
return user;
}
},
Mutation: {
addShot: async (
root,
{ name, image, largeImage, description, username },
{ Shot }
) => {
const newShot = await new Shot({
name,
image,
largeImage,
description,
username
}).save();
return newShot;
},
deleteUserShot: async (root, { _id }, { Shot }) => {
const shot = await Shot.findOneAndRemove({ _id });
return shot;
},
likeShot: async (root, { _id, username }, { Shot, User }) => {
const shot = await Shot.findOneAndUpdate({ _id }, { $inc: { likes: 1 } });
const user = await User.findOneAndUpdate(
{ username },
{ $addToSet: { favorites: _id } }
);
return shot;
},
unlikeShot: async (root, { _id, username }, { Shot, User }) => {
const shot = await Shot.findOneAndUpdate(
{ _id },
{ $inc: { likes: -1 } }
);
const user = await User.findOneAndUpdate(
{ username },
{ $pull: { favorites: _id } } // pull will remove a giving id
);
return shot;
},
signinUser: async (root, { username, password }, { User }) => {
const user = await User.findOne({ username });
if (!user) {
throw new Error(
"We couldn’t find an account matching the username you entered. Please check your password and try again."
);
}
const isValidPassword = await bcrypt.compare(password, user.password);
if (!isValidPassword) {
throw new Error(
"We couldn’t find an account matching the password you entered. Please check your password and try again."
);
}
return {
token: createToken(user, process.env.SECRET, "7d")
};
},
signupUser: async (root, { username, email, password }, { User }) => {
const user = await User.findOne({ username });
if (user) {
throw new Error("User already exists");
}
const newUser = await new User({
username,
email,
password
}).save();
return { token: createToken(newUser, process.env.SECRET, "7d") };
}
}
};