You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
3.9 KiB
99 lines
3.9 KiB
import {getClient} from "../utils/database.js";
|
|
|
|
export async function search(req, res) {
|
|
try {
|
|
console.log(req.query);
|
|
const query = req.query.q;
|
|
const type = req.query.type || 'all';
|
|
const offset = parseInt(req.query.offset) || 0;
|
|
const limit = parseInt(req.query.limit) || 20;
|
|
const client = await getClient();
|
|
|
|
|
|
if (!query) {
|
|
return res.status(400).json({ message: "Query parameter 'q' is required" });
|
|
}
|
|
|
|
if (type === 'videos') {
|
|
let videoResults = [];
|
|
|
|
// Search video in database based on the video title
|
|
const videoNameQuery = `
|
|
SELECT
|
|
v.id, v.title, v.thumbnail, v.description as video_description, v.channel, v.visibility, v.file, v.slug, v.format, v.release_date,
|
|
c.id as channel_id, c.owner, c.description as channel_description, c.name,
|
|
u.picture as profilePicture,
|
|
COUNT(h.id) as views
|
|
FROM videos as v
|
|
JOIN public.channels c on v.channel = c.id
|
|
JOIN public.users u on c.owner = u.id
|
|
LEFT JOIN public.history h on h.video = v.id
|
|
WHERE v.title ILIKE $1
|
|
GROUP BY v.id, v.title, v.thumbnail, v.description, v.channel, v.visibility, v.file, v.slug, v.format, v.release_date,
|
|
c.id, c.owner, c.description, c.name, u.picture
|
|
OFFSET $2
|
|
LIMIT $3;
|
|
`;
|
|
|
|
const videoNameResult = await client.query(videoNameQuery, [`%${query}%`, offset, limit]);
|
|
const videoNames = videoNameResult.rows;
|
|
|
|
for (const video of videoNames) {
|
|
// Put all the creator's information in the creator sub-object
|
|
video.creator = {
|
|
name: video.name,
|
|
profilePicture: video.profilepicture,
|
|
description: video.channel_description
|
|
};
|
|
// Remove the creator's information from the video object
|
|
delete video.name;
|
|
delete video.profilepicture;
|
|
delete video.channel_description;
|
|
|
|
video.type = "video";
|
|
videoResults.push(video);
|
|
}
|
|
|
|
client.end()
|
|
|
|
return res.status(200).json(videoResults);
|
|
|
|
} else if (type === 'channel') {
|
|
let channelResults = [];
|
|
|
|
// Search channel in database based on the channel name
|
|
const channelNameQuery = `
|
|
SELECT c.id as channel_id, c.name, c.description as channel_description, c.owner, u.picture as profilePicture, COUNT(s.id) as subscribers
|
|
FROM public.channels c
|
|
JOIN public.users u on c.owner = u.id
|
|
LEFT JOIN public.subscriptions s ON s.channel = c.id
|
|
WHERE c.name ILIKE $1
|
|
group by c.name, c.id, c.description, c.owner, u.picture
|
|
OFFSET $2
|
|
LIMIT $3;
|
|
`;
|
|
|
|
const channelNameResult = await client.query(channelNameQuery, [`%${query}%`, offset, limit]);
|
|
const channelNames = channelNameResult.rows;
|
|
|
|
for (const channel of channelNames) {
|
|
channel.type = "channel";
|
|
channel.profilePicture = channel.profilepicture; // Rename for consistency
|
|
delete channel.profilepicture;
|
|
channelResults.push(channel);
|
|
}
|
|
|
|
client.end()
|
|
|
|
return res.status(200).json(channelResults);
|
|
|
|
} else {
|
|
return res.status(400).json({ message: "Invalid type parameter. Use 'videos' or 'channel'." });
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
console.error("Error in search controller:", error);
|
|
res.status(500).json({ message: "Internal server error" });
|
|
}
|
|
}
|