-
Notifications
You must be signed in to change notification settings - Fork 2
/
resolvers.js
104 lines (91 loc) · 3.47 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
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 = {
//QUERIES
Query: {
getAllStory: async(root, args ,{ Story })=> {
const allStory = await Story.find().sort({ createdDate: 'desc'});
return allStory;
},
getStory: async(root, {_id}, { Story }) => {
const story = await Story.findOne({ _id });
return story;
},
searchStories: async(root, args,{ Story })=>{
const searchStory = await Story.find().sort({ createdDate: 'desc' });
return searchStory;
},
getUserStories: async (root, {username}, {Story}) => {
const UserStories = await Story.find({ username }).sort({ createdDate: 'desc'})
return UserStories;
},
getCurrentUser: async ( root, args, { currentUser, User }) => {
if( !currentUser ) {
return null;
}
const user = await User.findOne({ username: currentUser.username })
.populate({
path:'favorites',
model: 'Story'
});
return user;
}
},
//MUTATIONS
Mutation: {
addStory: async ( root, { name, imageUrl, description, category, instructions, username }, { Story })=> {
const newStory = await new Story({
name ,
imageUrl,
description,
category,
instructions,
username
}).save();
return newStory;
},
deleteStory: async ( root, { _id }, { Story }) => {
const story = await Story.findOneAndRemove({ _id });
return story;
},
likeStory: async ( root, { _id , username}, { Story, User})=> {
const likes = await Story.findOneAndUpdate({ _id }, { $inc: { likes: +1}});
const user = await User.findOneAndUpdate({ username }, { $pull: {favorites: _id}})
likes.save();
return likes;
},
unlikeStory: async ( root, { _id , username}, { Story, User})=> {
const likes = await Story.findOneAndUpdate({ _id }, { $inc: { likes: -1}});
const user = await User.findOneAndUpdate({ username }, { $addToSet: {favorites: _id}})
likes.save();
return likes;
},
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, '1hr')};
},
signinUser: async (root, { username, password }, { User }) => {
const user = await User.findOne({ username });
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, '1hr')};
}
}
};