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 = req.query.offset || 0; const limit = req.query.limit || 20; const client = await getClient(); if (!query) { return res.status(400).json({ message: "Query parameter 'q' is required" }); } if (type === 'videos') { // Search video in database based on the query, video title, tags and author const videoNameQuery = `SELECT id FROM videos WHERE title ILIKE $1 OFFSET $3 LIMIT $2`; const videoNameResult = await client.query(videoNameQuery, [`%${query}%`, limit, offset]); // Search video from tags const tagQuery = `SELECT id FROM tags WHERE name ILIKE $1 OFFSET $3 LIMIT $2`; const tagResult = await client.query(tagQuery, [`%${query}%`, limit, offset]); const tags = tagResult.rows.map(tag => tag.name); for (const tag of tags) { const videoTagQuery = `SELECT id FROM videos WHERE id IN (SELECT video FROM video_tags WHERE tag = (SELECT id FROM tags WHERE name = $1)) OFFSET $3 LIMIT $2`; const videoTagResult = await client.query(videoTagQuery, [tag, limit, offset]); videoNameResult.rows.push(...videoTagResult.rows); } // Search video from author const authorQuery = `SELECT videos.id FROM videos JOIN channels c ON videos.channel = c.id WHERE c.name ILIKE $1`; const authorResult = await client.query(authorQuery, [`%${query}%`]); for (const author of authorResult.rows) { if (!videoNameResult.rows.some(video => video.id === author.id)) { videoNameResult.rows.push(author); } } const videos = []; for (let video of videoNameResult.rows) { video = video.id; // Extracting the video ID let videoDetails = {}; // Fetching video details const videoDetailsQuery = `SELECT id, title, description, thumbnail, channel, release_date FROM videos WHERE id = $1`; const videoDetailsResult = await client.query(videoDetailsQuery, [video]); if (videoDetailsResult.rows.length === 0) { continue; // Skip if no video details found } videoDetails = videoDetailsResult.rows[0]; // Setting the type videoDetails.type = 'video'; // Fetching views and likes const viewsQuery = `SELECT COUNT(*) AS view_count FROM history WHERE video = $1`; const viewsResult = await client.query(viewsQuery, [video]); videoDetails.views = viewsResult.rows[0].view_count; // GET CREATOR const creatorQuery = `SELECT c.id, c.name, c.owner FROM channels c JOIN videos v ON c.id = v.channel WHERE v.id = $1`; const creatorResult = await client.query(creatorQuery, [video]); videoDetails.creator = creatorResult.rows[0]; // GET CREATOR PROFILE PICTURE const profilePictureQuery = `SELECT picture FROM users WHERE id = $1`; const profilePictureResult = await client.query(profilePictureQuery, [videoDetails.creator.owner]); videoDetails.creator.profile_picture = profilePictureResult.rows[0].picture; videos.push(videoDetails); } client.end() return res.status(200).json(videos); } } catch (error) { console.error("Error in search controller:", error); res.status(500).json({ message: "Internal server error" }); } }