diff --git a/backend/app/controllers/recommendation.controller.js b/backend/app/controllers/recommendation.controller.js new file mode 100644 index 0000000..df2e21e --- /dev/null +++ b/backend/app/controllers/recommendation.controller.js @@ -0,0 +1,18 @@ +import {getClient} from "../utils/database.js"; + + +export async function getRecommendations(req, res) { + + const token = req.headers.authorization?.split(' ')[1]; + + if (!token) { + + // GET MOST USED TOKEN + let client = await getClient(); + let queryMostUsedToken = `SELECT * FROM tags ` + + } else { + + } + +} \ No newline at end of file diff --git a/backend/app/controllers/video.controller.js b/backend/app/controllers/video.controller.js index 50cda94..402b2cb 100644 --- a/backend/app/controllers/video.controller.js +++ b/backend/app/controllers/video.controller.js @@ -217,15 +217,26 @@ export async function addTags(req, res) { logger.action("try to add tags to video " + videoId); const client = await getClient(); - const tagInBaseQuery = `DELETE tags WHERE video = ${videoId}`; - client.query(tagInBaseQuery); for (const tag of tags) { - const query = `INSERT INTO tags (video, tag) VALUES (${videoId}, '${tag}')`; - await client.query(query); - } + const tagQuery = `SELECT * FROM tags WHERE name = '${tag}' AND video = ${videoId}`; + const tagResult = await client.query(tagQuery); + let id = null; + + if (tagResult.rows.length === 0) { + const insertTagQuery = `INSERT INTO tags (name, video) VALUES ('${tag}') RETURNING id`; + const result = await client.query(insertTagQuery); + id = result.rows[0].id; + } else { + logger.write("Tag " + tag + " already exists for video " + videoId, 200); + id = tagResult.rows[0].id; + } + const insertVideoTagQuery = `INSERT INTO video_tags (video, tag) VALUES (${id}, ${videoId}) ON CONFLICT DO NOTHING`; + await client.query(insertVideoTagQuery); + } logger.write("successfully added tags to video " + videoId, 200); + await client.end(); res.status(200).json({"message": "Successfully added tags to video"}); } \ No newline at end of file diff --git a/backend/app/middlewares/recommendation.middleware.js b/backend/app/middlewares/recommendation.middleware.js new file mode 100644 index 0000000..e69de29 diff --git a/backend/app/routes/redommendation.routes.js b/backend/app/routes/redommendation.routes.js new file mode 100644 index 0000000..8120463 --- /dev/null +++ b/backend/app/routes/redommendation.routes.js @@ -0,0 +1,7 @@ +import { Router } from 'express'; + +const router = Router(); + +router.get('/', [], getRecommendations); + +export default router; \ No newline at end of file diff --git a/backend/app/routes/video.route.js b/backend/app/routes/video.route.js index acb5177..4c4ec74 100644 --- a/backend/app/routes/video.route.js +++ b/backend/app/routes/video.route.js @@ -52,4 +52,5 @@ router.get("/:id/like", [addLogger, isTokenValid, Video.id, validator, doVideoEx // UPDATE TAGS router.put("/:id/tags", [addLogger, isTokenValid, Video.id, Video.tags, validator, doVideoExistsParam, isOwner], addTags); + export default router; \ No newline at end of file diff --git a/backend/app/utils/database.js b/backend/app/utils/database.js index 1d97c97..f8a4048 100644 --- a/backend/app/utils/database.js +++ b/backend/app/utils/database.js @@ -89,11 +89,16 @@ export async function initDb() { query = `CREATE TABLE IF NOT EXISTS tags( id SERIAL PRIMARY KEY, - name VARCHAR(255) NOT NULL, - video INTEGER NOT NULL REFERENCES videos(id) + name VARCHAR(255) NOT NULL )`; await client.query(query); + query = `CREATE TABLE IF NOT EXISTS video_tags ( + video INTEGER NOT NULL REFERENCES videos(id), + tag INTEGER NOT NULL REFERENCES tags(id) + )` + await client.query(query); + } catch (e) { console.error("Error initializing database:", e); } diff --git a/frontend/src/pages/Home.jsx b/frontend/src/pages/Home.jsx index 43b4a8c..f96fcaf 100644 --- a/frontend/src/pages/Home.jsx +++ b/frontend/src/pages/Home.jsx @@ -1,9 +1,30 @@ import Navbar from '../components/Navbar.jsx'; import HeroImage from '../assets/img/hero.png'; import Recommendations from "../components/Recommendations.jsx"; +import {useState} from "react"; export default function Home() { + const [recommendations, setRecommendations] = useState([]); + const [loading, setLoading] = useState(true); + const [topCreators, setTopCreators] = useState([]); + const [trendingVideos, setTrendingVideos] = useState([]); + + // Fetch recommendations, top creators, and trending videos + const fetchData = async () => { + try { + const response = await fetch('/api/home'); + const data = await response.json(); + setRecommendations(data.recommendations); + setTopCreators(data.topCreators); + setTrendingVideos(data.trendingVideos); + } catch (error) { + console.error('Error fetching data:', error); + } finally { + setLoading(false); + } + }; + return (