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.
 
 
 
 

95 lines
3.6 KiB

import {getClient} from "../utils/database.js";
export async function search(req, res) {
try {
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 = [];
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 AND v.visibility = 'public'
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) {
video.creator = {
name: video.name,
profilePicture: video.profilepicture,
description: video.channel_description
};
delete video.name;
delete video.profilepicture;
delete video.channel_description;
video.type = "video";
videoResults.push(video);
}
client.release()
return res.status(200).json(videoResults);
} else if (type === 'channel') {
let channelResults = [];
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;
delete channel.profilepicture;
channelResults.push(channel);
}
client.release()
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" });
}
}