-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
138 lines (129 loc) · 4.72 KB
/
router.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
import { pool } from "./server.js";
// api so that user can upload youtube video
export async function addVideo(reqBody) {
const client = await pool.connect();
try {
await client.query("BEGIN"); // Start transaction
// Insert data into the users table
const videoData = {
userId: reqBody.userId,
title: reqBody.title,
vid_id: reqBody.vid_id,
tags: reqBody.tags,
};
const videoQuery =
"INSERT INTO ytvids (user_id, title, vid_id, created_at, tags) VALUES ($1, $2, $3, CURRENT_TIMESTAMP, $4) RETURNING id";
const videoValues = [
videoData.userId,
videoData.title,
videoData.vid_id,
videoData.tags,
];
const userResult = await client.query(videoQuery, videoValues);
const id = userResult.rows[0].id;
// Insert data into the video_likes_and_dislikes table
const likeDislikeData = {
id: id,
vid_id: reqBody.vid_id,
};
const likeDislikeQuery =
"INSERT INTO videos_like_and_dislike (id, vid_id) VALUES ($1, $2) RETURNING *";
const likeDislikeValues = [likeDislikeData.id, likeDislikeData.vid_id];
await client.query(likeDislikeQuery, likeDislikeValues);
await client.query("COMMIT"); // Commit transaction
return { success: true, id };
} catch (error) {
await client.query("ROLLBACK"); // Rollback transaction if there's an error
throw error;
} finally {
client.release(); // Release client back to the pool
}
}
//api to get all youtube videos
export const getAllVideoData = (page, limit, query) => {
return new Promise(function (resolve, reject) {
const offset = (page - 1) * limit;
!query
? pool.query(
"SELECT ytvids.id, user_id, title, ytvids.vid_id, created_at, tags, like_count FROM ytvids INNER JOIN videos_like_and_dislike ON ytvids.id = videos_like_and_dislike.id LIMIT $1 OFFSET $2",
[limit, offset],
(error, result) => {
if (error) {
reject(error);
}
if (result && result.rows.length !== 0) {
resolve({
message: 1,
data: result.rows,
});
} else if (result && result.rows.length === 0) {
resolve({
data: [],
message: 0,
});
} else {
reject(new Error("Data not founded, try again later"));
}
}
)
: pool.query(
"SELECT ytvids.id, user_id, title, ytvids.vid_id, created_at, tags, like_count FROM ytvids INNER JOIN videos_like_and_dislike ON ytvids.id = videos_like_and_dislike.id WHERE ts @@ to_tsquery('english', $1) LIMIT $2 OFFSET $3",
[query, limit, offset],
(error, result) => {
if (error) {
reject(error);
}
if (result && result.rows.length !== 0) {
resolve({
message: 1,
data: result.rows,
});
} else if (result && result.rows.length === 0) {
resolve({
data: [],
message: 0,
});
} else {
reject(new Error("Data not founded, try again later"));
}
}
);
});
};
export const fetchPopularVideos = (page, limit) => {
return new Promise((resolve, reject) => {
const offset = (page - 1) * limit;
pool.query(
"SELECT ytvids.id, user_id, title, ytvids.vid_id, created_at, tags, like_count FROM ytvids INNER JOIN videos_like_and_dislike ON ytvids.id = videos_like_and_dislike.id ORDER BY like_count DESC, ytvids.id LIMIT $1 OFFSET $2",
[limit, offset],
(error, result) => {
if (error) {
reject(error);
} else if (result && result.rows.length !== 0) {
resolve({ data: result.rows, message: 1 });
} else if (result && result.rows.length === 0) {
resolve({ data: [], message: 0 });
}
}
);
});
};
export const fetchPopularVideoWithQuery = (query, page, limit) => {
const promise = new Promise((resolve, reject) => {
const offset = (page - 1) * limit;
pool.query(
"SELECT ytvids.id, user_id, title, ytvids.vid_id, created_at, tags, like_count FROM ytvids INNER JOIN videos_like_and_dislike ON ytvids.id = videos_like_and_dislike.id WHERE ts @@ to_tsquery('english', $1) ORDER BY like_count DESC, ytvids.id LIMIT $2 OFFSET $3",
[query, limit, offset],
(error, result) => {
if (error) {
reject(error);
} else if (result && result.rows.length !== 0) {
resolve({ data: result.rows, message: 1 });
} else if (result && result.rows.length === 0) {
resolve({ data: [], message: 0 });
}
}
);
});
return promise;
};