Skip to content

Commit 27ea8eb

Browse files
committed
favorite feature connected to db
1 parent c893669 commit 27ea8eb

File tree

5 files changed

+40
-23
lines changed

5 files changed

+40
-23
lines changed

client/src/components/App.jsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const navItems = [
1313
name: "About",
1414
link: "#",
1515
},
16+
{
17+
name: "My Favorite Videos",
18+
link: "#",
19+
},
1620
{
1721
name: "Services",
1822
link: "#",
@@ -44,6 +48,7 @@ class App extends React.Component {
4448
.get("/fav")
4549
.then(({ data }) => this.setState({ favorites: data }))
4650
.catch((err) => console.log(err));
51+
console.log(this.state.favorites);
4752
}
4853

4954
handleSubmit(query) {
@@ -56,13 +61,15 @@ class App extends React.Component {
5661
.then(({ data }) => {
5762
let videos = [];
5863
data.items.forEach((item) => {
64+
console.log(item);
5965
let video = {};
6066
video.liked = false;
6167
video.id = item.id.videoId;
6268
video.title = item.snippet.title;
6369
video.publishedAt = item.snippet.publishedAt;
6470
video.description = item.snippet.description;
65-
video.thumbnails = item.snippet.thumbnails;
71+
video.thumbnails = {};
72+
video.thumbnails.default = item.snippet.thumbnails.default;
6673
videos.push(video);
6774
});
6875
this.setState({
@@ -77,10 +84,13 @@ class App extends React.Component {
7784

7885
handleFavorite(idx) {
7986
let { videos } = this.state;
80-
videos[idx].liked = !videos[idx].liked;
87+
let video = videos[idx];
88+
video.liked = !video.liked;
8189
this.setState({ videos });
8290
axios
83-
.patch(`/fav/${videos[idx].id}`)
91+
.patch(`/fav/${video.id}`, {
92+
data: video,
93+
})
8494
.then((msg) => console.log(msg))
8595
.catch((err) => console.log(err));
8696
}

client/src/components/ExerciseService.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ExerciseService extends React.Component {
3232
<div
3333
className="modal fade"
3434
id="exampleModal"
35-
tabindex="-1"
35+
tabIndex="-1"
3636
role="dialog"
3737
aria-labelledby="exampleModalLabel"
3838
aria-hidden="true"

database/Model/Video.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ const videoSchema = new mongoose.Schema({
77
description: String,
88
thumbnails: {
99
default: {
10+
height: Number,
1011
url: String,
12+
width: Number,
1113
},
1214
},
1315
});
@@ -24,15 +26,25 @@ const getAllVideos = (callback) => {
2426
});
2527
};
2628

27-
const changeFavorite = (video, callback) => {
28-
Photo.create(video, (err) => {
29-
if (err) {
30-
callback(err);
31-
} else {
32-
console.log("added");
33-
callback(null);
34-
}
35-
});
29+
const changeFavorite = (id, video, callback) => {
30+
if (!Video.exists({ id })) {
31+
Video.findOneAndUpdate({ id }, video, (err) => {
32+
if (err) {
33+
callback(err);
34+
} else {
35+
callback(null);
36+
}
37+
});
38+
} else {
39+
Video.create(video, (err) => {
40+
if (err) {
41+
callback(err);
42+
} else {
43+
console.log("added");
44+
callback(null);
45+
}
46+
});
47+
}
3648
};
3749

3850
module.exports = {

public/bundle.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/index.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ app.get("/fav", (req, res) => {
2727

2828
app.patch("/fav/:id", (req, res) => {
2929
const id = req.param.id;
30-
console.log(req.body);
31-
Video.changeFavorite(id, (err, results) => {
30+
const video = req.body.data;
31+
Video.changeFavorite(id, video, (err, results) => {
3232
if (err) {
3333
res.status(500).send(err);
3434
} else {
@@ -37,11 +37,6 @@ app.patch("/fav/:id", (req, res) => {
3737
});
3838
});
3939

40-
app.post("/fav", (req, res) => {
41-
const video = req.body;
42-
console.log(video);
43-
});
44-
4540
app.listen(PORT, () => {
4641
console.log(`Server listening on port: ${PORT}...`);
4742
});

0 commit comments

Comments
 (0)