From bb5bde75f7a238ffed3e209ba3ee2cbfe2461640 Mon Sep 17 00:00:00 2001 From: Astri4-4 Date: Sun, 20 Jul 2025 08:55:29 +0000 Subject: [PATCH] ADD subscriptions and likes hot reload --- backend/app/controllers/channel.controller.js | 16 ++++++- backend/app/controllers/video.controller.js | 16 ++++++- backend/logs/access.log | 46 +++++++++++++++++++ frontend/src/pages/Video.jsx | 22 ++++++++- 4 files changed, 94 insertions(+), 6 deletions(-) diff --git a/backend/app/controllers/channel.controller.js b/backend/app/controllers/channel.controller.js index a2e3748..e9a90f4 100644 --- a/backend/app/controllers/channel.controller.js +++ b/backend/app/controllers/channel.controller.js @@ -125,13 +125,25 @@ export async function toggleSubscription(req, res) { // Unsubscribe const deleteQuery = `DELETE FROM subscriptions WHERE channel = $1 AND owner = $2`; await client.query(deleteQuery, [id, userId]); + + // Send back the number of remaining subscriptions + const countQuery = `SELECT COUNT(*) FROM subscriptions WHERE channel = $1`; + const countResult = await client.query(countQuery, [id]); + const remainingSubscriptions = countResult.rows[0].count; + logger.write("Successfully unsubscribed from channel", 200); - res.status(200).json({message: 'Unsubscribed successfully'}); + res.status(200).json({message: 'Unsubscribed successfully', subscriptions: remainingSubscriptions}); } else { // Subscribe const insertQuery = `INSERT INTO subscriptions (channel, owner) VALUES ($1, $2)`; await client.query(insertQuery, [id, userId]); + + // Send back the number of subscriptions after subscribing + const countQuery = `SELECT COUNT(*) FROM subscriptions WHERE channel = $1`; + const countResult = await client.query(countQuery, [id]); + const totalSubscriptions = countResult.rows[0].count; + logger.write("Successfully subscribed to channel", 200); - res.status(200).json({message: 'Subscribed successfully'}); + res.status(200).json({message: 'Subscribed successfully', subscriptions: totalSubscriptions}); } } \ No newline at end of file diff --git a/backend/app/controllers/video.controller.js b/backend/app/controllers/video.controller.js index 5a141fd..1e674cb 100644 --- a/backend/app/controllers/video.controller.js +++ b/backend/app/controllers/video.controller.js @@ -230,13 +230,25 @@ export async function toggleLike(req, res) { if (likeResult.rows.length === 0) { const query = `INSERT INTO likes (video, owner) VALUES ($1, $2)`; await client.query(query, [id, userId]); + + // GET LIKES COUNT + const likesCountQuery = `SELECT COUNT(*) AS like_count FROM likes WHERE video = $1`; + const likesCountResult = await client.query(likesCountQuery, [id]); + const likesCount = likesCountResult.rows[0].like_count; + logger.write("no likes found adding likes for video " + id, 200); - res.status(200).json({"message": "Successfully added like"}); + res.status(200).json({"message": "Successfully added like", "likes": likesCount}); } else { const query = `DELETE FROM likes WHERE owner = $1 AND video = $2`; await client.query(query, [userId, id]); + + // GET LIKES COUNT + const likesCountQuery = `SELECT COUNT(*) AS like_count FROM likes WHERE video = $1`; + const likesCountResult = await client.query(likesCountQuery, [id]); + const likesCount = likesCountResult.rows[0].like_count; + logger.write("likes found, removing like for video " + id, 200); - res.status(200).json({"message": "Successfully removed like"}); + res.status(200).json({"message": "Successfully removed like", "likes": likesCount}); } diff --git a/backend/logs/access.log b/backend/logs/access.log index f414c08..c7d1053 100644 --- a/backend/logs/access.log +++ b/backend/logs/access.log @@ -831,3 +831,49 @@ [2025-07-19 23:05:34.319] [undefined] GET(/:id/similar): successfully retrieved similar videos for video 3 with status 200 [2025-07-19 23:05:34.328] [undefined] GET(/:id/views): try to add views for video 3 [2025-07-19 23:05:34.333] [undefined] GET(/:id/views): successfully added views for video 3 with status 200 +[2025-07-20 08:47:03.500] [undefined] GET(/:id): try to get video 3 +[2025-07-20 08:47:03.511] [undefined] GET(/:id): successfully get video 3 with status 200 +[2025-07-20 08:47:03.524] [undefined] GET(/:id/similar): try to get similar videos for video 3 +[2025-07-20 08:47:03.535] [undefined] GET(/:id/similar): successfully retrieved similar videos for video 3 with status 200 +[2025-07-20 08:47:03.553] [undefined] GET(/:id/views): try to add views for video 3 +[2025-07-20 08:47:03.561] [undefined] GET(/:id/views): successfully added views for video 3 with status 200 +[2025-07-20 08:47:07.593] [undefined] GET(/:id): try to get video 3 +[2025-07-20 08:47:07.601] [undefined] GET(/:id): successfully get video 3 with status 200 +[2025-07-20 08:47:07.616] [undefined] GET(/:id/similar): try to get similar videos for video 3 +[2025-07-20 08:47:07.626] [undefined] GET(/:id/similar): successfully retrieved similar videos for video 3 with status 200 +[2025-07-20 08:47:07.670] [undefined] GET(/:id/views): try to add views for video 3 +[2025-07-20 08:47:07.679] [undefined] GET(/:id/views): successfully added views for video 3 with status 200 +[2025-07-20 08:47:09.941] [undefined] POST(/:id/subscribe): try to toggle subscription for channel with id 2 +[2025-07-20 08:47:09.947] [undefined] POST(/:id/subscribe): Successfully unsubscribed from channel with status 200 +[2025-07-20 08:51:41.655] [undefined] GET(/:id): try to get video 3 +[2025-07-20 08:51:41.668] [undefined] GET(/:id): successfully get video 3 with status 200 +[2025-07-20 08:51:41.681] [undefined] GET(/:id/similar): try to get similar videos for video 3 +[2025-07-20 08:51:41.695] [undefined] GET(/:id/similar): successfully retrieved similar videos for video 3 with status 200 +[2025-07-20 08:51:41.737] [undefined] GET(/:id/views): try to add views for video 3 +[2025-07-20 08:51:41.747] [undefined] GET(/:id/views): successfully added views for video 3 with status 200 +[2025-07-20 08:51:45.125] [undefined] POST(/:id/subscribe): try to toggle subscription for channel with id 2 +[2025-07-20 08:51:45.132] [undefined] POST(/:id/subscribe): Successfully subscribed to channel with status 200 +[2025-07-20 08:51:46.253] [undefined] POST(/:id/subscribe): try to toggle subscription for channel with id 2 +[2025-07-20 08:51:46.260] [undefined] POST(/:id/subscribe): Successfully unsubscribed from channel with status 200 +[2025-07-20 08:51:47.141] [undefined] POST(/:id/subscribe): try to toggle subscription for channel with id 2 +[2025-07-20 08:51:47.147] [undefined] POST(/:id/subscribe): Successfully subscribed to channel with status 200 +[2025-07-20 08:54:06.806] [undefined] GET(/:id): try to get video 3 +[2025-07-20 08:54:06.817] [undefined] GET(/:id): successfully get video 3 with status 200 +[2025-07-20 08:54:06.833] [undefined] GET(/:id/similar): try to get similar videos for video 3 +[2025-07-20 08:54:06.845] [undefined] GET(/:id/similar): successfully retrieved similar videos for video 3 with status 200 +[2025-07-20 08:54:06.897] [undefined] GET(/:id/views): try to add views for video 3 +[2025-07-20 08:54:06.906] [undefined] GET(/:id/views): successfully added views for video 3 with status 200 +[2025-07-20 08:54:09.393] [undefined] GET(/:id/like): try to toggle like on video 3 +[2025-07-20 08:54:09.399] [undefined] GET(/:id/like): likes found, removing like for video 3 with status 200 +[2025-07-20 08:54:59.934] [undefined] GET(/:id): try to get video 3 +[2025-07-20 08:54:59.942] [undefined] GET(/:id): successfully get video 3 with status 200 +[2025-07-20 08:54:59.953] [undefined] GET(/:id/similar): try to get similar videos for video 3 +[2025-07-20 08:54:59.964] [undefined] GET(/:id/similar): successfully retrieved similar videos for video 3 with status 200 +[2025-07-20 08:54:59.995] [undefined] GET(/:id/views): try to add views for video 3 +[2025-07-20 08:55:00.002] [undefined] GET(/:id/views): successfully added views for video 3 with status 200 +[2025-07-20 08:55:01.907] [undefined] GET(/:id/like): try to toggle like on video 3 +[2025-07-20 08:55:01.915] [undefined] GET(/:id/like): no likes found adding likes for video 3 with status 200 +[2025-07-20 08:55:02.619] [undefined] GET(/:id/like): try to toggle like on video 3 +[2025-07-20 08:55:02.625] [undefined] GET(/:id/like): likes found, removing like for video 3 with status 200 +[2025-07-20 08:55:03.148] [undefined] GET(/:id/like): try to toggle like on video 3 +[2025-07-20 08:55:03.155] [undefined] GET(/:id/like): no likes found adding likes for video 3 with status 200 diff --git a/frontend/src/pages/Video.jsx b/frontend/src/pages/Video.jsx index 9c66d90..264f138 100644 --- a/frontend/src/pages/Video.jsx +++ b/frontend/src/pages/Video.jsx @@ -179,8 +179,16 @@ export default function Video() { const data = await response.json(); console.log('Subscription successful:', data); - // You could update the UI here to show subscription status - alert('Successfully subscribed!'); + const subscriptionCount = data.subscriptions || 0; + setVideo((prevVideo) => { + return { + ...prevVideo, + creator: { + ...prevVideo.creator, + subscribers: subscriptionCount + } + }; + }) } catch (error) { console.error('Error subscribing:', error); @@ -215,6 +223,16 @@ export default function Video() { throw new Error('Failed to like video'); } + const data = await response.json(); + console.log('Video liked successfully:', data); + + setVideo((prevVideo) => { + return { + ...prevVideo, + likes: data.likes || prevVideo.likes + 1 // Update likes count + }; + }) + } catch (error) { console.error('Error liking video:', error); }