-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresolvers.js
179 lines (173 loc) · 4.93 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const jwt = require('jsonwebtoken');
const bcrypt = require('bcrypt');
const http = require('http');
const createToken = (user, secret, expiresIn) => {
const { username, email } = user;
return jwt.sign({ username, email }, secret, { expiresIn });
};
exports.resolvers = {
Query: {
getAllVideos: async (root, args, { Video }) => {
const allVideos = await Video.find();
return allVideos;
},
getVideo: async (root, { _id }, { Video }) => {
const video = await Video.findOne({ _id });
return video;
},
searchVideos: async (root, { searchTerm }, { Video }) => {
if (searchTerm) {
const searchResults = await Video.find(
{
$text: { $search: searchTerm },
},
{
score: { $meta: 'textScore' },
}
).sort({
score: { $meta: 'textScore' },
});
return searchResults;
} else {
const videos = await Video.find().sort({
likes: 'desc',
createdDate: 'desc',
});
return videos;
}
},
// getCaptureVideo: async(root,'', ''),
getCurrentUser: async (root, args, { currentUser, User }) => {
if (!currentUser) {
return null;
}
const user = await User.findOne({
username: currentUser.username,
}).populate({
path: 'favorites',
model: 'Video',
options: { retainNullValues: false },
});
// await User.findOne({
// username: currentUser.username
// }).populate({
// path: 'journal',
// model: 'Journal',
// options: { retainNullValues: false }
// });
return user;
},
getUserJournal: async (root, { username }, { Journal }) => {
const userJournal = await Journal.find({ username });
return userJournal;
},
getUserVideos: async (root, { username }, { User }) => {
const { favorites } = await User.findOne(
{ username },
{ favorites: true },
{ options: { retainNullValues: false } }
);
return favorites;
},
},
Mutation: {
addVideo: async (root, { name, gifs, videoId, imageUrl }, { Video }) => {
const newVideo = await new Video({
name,
gifs,
videoId,
imageUrl,
}).save();
return newVideo;
},
likeVideo: async (root, { _id, username }, { Video, User }) => {
try {
const video = await Video.findOne({ videoId: _id });
const user = await User.findOneAndUpdate(
{ username },
{ $push: { favorites: video } }
);
} catch (err) {
console.error(err);
}
return video;
},
unlikeVideo: async (root, { _id, username }, { Video, User }) => {
const video = await Video.findOneAndUpdate(
{ _id },
{ $inc: { likes: -1 } }
);
const user = await User.findOneAndUpdate(
{ username },
{ $pull: { favorites: video } }
);
return video;
},
deleteUserVideo: async (root, { _id }, { Video }) => {
const video = await Video.findOne({ _id });
return video;
},
addVideoImage: async (root, { name, imageUrl }, { Video }) => {
const existingVideo = await Video.findOne()
.where('name')
.equals(name);
if (existingVideo) {
existingVideo.imageUrl = imageUrl;
return existingVideo;
} else {
const newVideo = await new Video({
name,
imageUrl,
}).save();
return newVideo;
}
},
addVideoGif: async (root, { name, gifs }, { Video }) => {
const existingVideo = await Video.findOne()
.where('name')
.equals(name);
if (existingVideo) {
existingVideo.gifs.push(gifs);
return existingVideo;
} else {
const newVideo = await new Video({
name,
gifs,
}).save();
return newVideo;
}
},
addJournal: async (root, { title, text, username }, { Journal }) => {
const newJournal = await new Journal({
title,
text,
username,
}).save();
return newJournal;
},
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');
} else {
return { token: createToken(user, process.env.SECRET, '24hr') };
}
},
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, '24hr') };
},
},
};