-
Notifications
You must be signed in to change notification settings - Fork 0
/
savedVideosRouter.js
72 lines (68 loc) · 1.81 KB
/
savedVideosRouter.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
import { pool } from "./server.js";
// api to add video id in user saved videos
export const addVideoIdInSavedPost = (body) => {
return new Promise((resolve, reject) => {
const { userId, vid_id } = body;
pool.query(
"UPDATE video_bucket SET saved_videos = array_append(saved_videos, $2) WHERE user_id = $1",
[userId, vid_id],
(error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
}
);
});
};
// api to check if the video id already exists in the array or not
export const checkIfVideoIdExists = (body) => {
return new Promise((resolve, reject) => {
const { userId, vid_id } = body;
pool.query(
"SELECT saved_videos FROM video_bucket WHERE $2 = ANY(saved_videos) AND user_id = $1",
[userId, vid_id],
(error, result) => {
if (error) {
reject(error);
} else {
resolve(result.rowCount);
}
}
);
});
};
// api to remove video id from the saved list
export const removeVideoIdFromSavedList = (body) => {
return new Promise((resolve, reject) => {
const { userId, vid_id } = body;
pool.query(
"UPDATE video_bucket SET saved_videos = array_remove(saved_videos, $2) WHERE user_id = $1",
[userId, vid_id],
(error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
}
);
});
};
export const getAllSavedVideos = (body) => {
return new Promise((resolve, reject) => {
const { userId } = body;
pool.query(
"SELECT saved_videos FROM video_bucket WHERE user_id = $1",
[userId],
(error, result) => {
if (error) {
reject(error);
} else {
resolve(result.rows[0]?.saved_videos);
}
}
);
});
};