Skip to content

Commit

Permalink
Merge branch 'main' into T35-JWT-Middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennull committed Apr 21, 2024
2 parents c247002 + e739bad commit 2c53c9a
Show file tree
Hide file tree
Showing 7 changed files with 365 additions and 170 deletions.
30 changes: 16 additions & 14 deletions controllers/ProfileController.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
const pool = require("../database/db");
const profileQueries = require("../models/ProfileModel");

const getOwnTweets = async (req, res) => {
const getUserPosts = async (req, res) => {
try {
const { userId } = req.query;
const query = await pool.query(profileQueries.getOwnTweets, [userId]);
const { username } = req.query;
const query = await pool.query(profileQueries.getUserPosts, [username]);
res.send(query.rows);
} catch (error) {
console.log(error);
res.status(500).send(error);
}
};

const getOwnReplies = async (req, res) => {
const getUserReplies = async (req, res) => {
try {
const { userId } = req.query;
const query = await pool.query(profileQueries.getOwnReplies, [userId]);
const { username } = req.query;
const query = await pool.query(profileQueries.getUserReplies, [username]);
res.send(query.rows);
} catch (error) {
console.log(error);
res.status(500).send(error);
}
};

const getOwnLikes = async (req, res) => {
const getUserLikes = async (req, res) => {
try {
const { userId } = req.query;
const query = await pool.query(profileQueries.getOwnLikes, [userId]);
const { username } = req.query;
const query = await pool.query(profileQueries.getUserLikes, [username]);
res.send(query.rows);
} catch (error) {
console.log(error);
Expand All @@ -36,8 +36,10 @@ const getOwnLikes = async (req, res) => {

const getProfileContents = async (req, res) => {
try {
const { userId } = req.query;
const query = await pool.query(profileQueries.getProfileContents, [userId]);
const { username } = req.query;
const query = await pool.query(profileQueries.getProfileContents, [
username,
]);
res.send(query.rows[0]);
} catch (error) {
console.log(error);
Expand All @@ -46,8 +48,8 @@ const getProfileContents = async (req, res) => {
};

module.exports = {
getOwnTweets,
getOwnReplies,
getOwnLikes,
getUserPosts,
getUserReplies,
getUserLikes,
getProfileContents,
};
66 changes: 44 additions & 22 deletions models/MessagesModel.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,56 @@
const addMessage = `INSERT INTO message (timestamp, "textContent", "sentUserId", "receivedUserId") VALUES ($1, $2, $3, $4)
const addMessage = `
INSERT INTO message (
timestamp,
"textContent",
"sentUserId",
"receivedUserId"
) VALUES ($1, $2, $3, $4)
RETURNING *;`;

const getDirectMessage = `SELECT * FROM message
WHERE "sentUserId" in ($1, $2) AND "receivedUserId" in ($1, $2)
const getDirectMessage = `
SELECT * FROM message
WHERE "sentUserId" in ($1, $2)
AND "receivedUserId" in ($1, $2)
ORDER BY timestamp`;

const getConversationList = `SELECT DISTINCT ON("otherUserId")
"displayName",
username,
"textContent",
timestamp,
s."otherUserId"
FROM
(SELECT m.*,
CASE WHEN "sentUserId" = $1 THEN "receivedUserId" ELSE "sentUserId" END AS "otherUserId"
const getConversationList = `
SELECT DISTINCT ON("otherUserId")
"displayName",
username,
"textContent",
timestamp,
s."otherUserId"
FROM (
SELECT m.*,
CASE WHEN "sentUserId" = $1
THEN "receivedUserId"
ELSE "sentUserId"
END AS "otherUserId"
FROM message m
WHERE m."sentUserId" = $1 OR m."receivedUserId" = $1) s
JOIN app_user u ON s."otherUserId" = u."userId"
ORDER BY "otherUserId", timestamp DESC;`;
WHERE m."sentUserId" = $1 OR m."receivedUserId" = $1
) s
JOIN app_user u
ON s."otherUserId" = u."userId"
ORDER BY
"otherUserId",
timestamp
DESC;`;

const getOtherUser = `SELECT
username,
"displayName"
const getOtherUser = `
SELECT
username,
"displayName"
FROM app_user
WHERE "userId" = $1`;

const getFollowedList = `SELECT u."userId", u."displayName", u."username"
FROM app_user u
JOIN follow f ON u."userId" = f."followedUserId"
WHERE f."followerUserId" = $1 AND f."followStatus" = true;`;
const getFollowedList = `
SELECT
u."userId",
u."displayName",
u."username"
FROM app_user u
JOIN follow f ON u."userId" = f."followedUserId"
WHERE f."followerUserId" = $1 AND f."followStatus" = true;`;

module.exports = {
addMessage,
Expand Down
197 changes: 142 additions & 55 deletions models/PostModel.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,152 @@
const addPost = `INSERT INTO post ("userId", timestamp, "textContent", "isRepost", "isQuotePost") VALUES ($1, $2, $3, $4, $5)
RETURNING "postId", "textContent", timestamp, "isRepost", "isQuotePost"`;
const addPost = `
INSERT INTO post (
"userId",
timestamp,
"textContent",
"isRepost",
"isQuotePost"
) VALUES ($1, $2, $3, $4, $5)
RETURNING
"postId",
"textContent",
timestamp,
"isRepost",
"isQuotePost"`;

const addReply = `INSERT INTO post ("userId", "parentPostId", timestamp, "textContent", "isRepost", "isQuotePost") VALUES ($1, $2, $3, $4, $5, $6)
RETURNING "postId", "parentPostId", "textContent", timestamp, "isRepost", "isQuotePost"`;
const addReply = `
INSERT INTO post (
"userId",
"parentPostId",
timestamp,
"textContent",
"isRepost",
"isQuotePost"
) VALUES ($1, $2, $3, $4, $5, $6)
RETURNING
"postId",
"parentPostId",
"textContent",
timestamp,
"isRepost",
"isQuotePost"`;

const getAllPosts = `WITH post_likes AS (
SELECT "postId",
COUNT(*)::INT AS "numberOfLikes"
FROM liked_post
GROUP BY "postId"
const getAllPosts = `
WITH post_likes AS (
SELECT "postId",
COUNT(*)::INT AS "numberOfLikes"
FROM liked_post
GROUP BY "postId"
),
post_replies_reposts AS (
SELECT "parentPostId",
COUNT(*)::INT AS "numberOfReplies",
COUNT(CASE WHEN "isRepost" = TRUE THEN 1 END) AS "numberOfReposts"
FROM post
WHERE "parentPostId" IS NOT NULL
GROUP BY "parentPostId"
)
SELECT p."postId",
u.username,
u."displayName",
p."textContent",
p.timestamp,
EXISTS(SELECT 1 FROM liked_post li WHERE li."userId" = $1 AND li."postId" = p."postId" LIMIT 1) AS "isLikedByCurrentUser",
COALESCE(l."numberOfLikes",0) AS "numberOfLikes"
FROM post AS p
LEFT JOIN post_likes AS l ON p."postId" = l."postId"
INNER JOIN app_user AS u ON p."userId" = u."userId"
WHERE p."parentPostId" IS NULL
ORDER BY p.timestamp DESC`;
SELECT
p."postId",
u.username,
u."displayName",
u."userId",
p."textContent",
p.timestamp,
EXISTS(
SELECT 1
FROM liked_post li
WHERE li."userId" = $1
AND li."postId" = p."postId"
LIMIT 1
) AS "isLikedByCurrentUser",
COALESCE(l."numberOfLikes",0) AS "numberOfLikes",
COALESCE(r."numberOfReplies", 0) AS "numberOfReplies",
COALESCE(r."numberOfReposts", 0) AS "numberOfReposts"
FROM
post AS p
LEFT JOIN post_likes AS l ON p."postId" = l."postId"
LEFT JOIN post_replies_reposts AS r ON p."postId" = r."parentPostId"
INNER JOIN app_user AS u ON p."userId" = u."userId"
WHERE p."parentPostId" IS NULL
ORDER BY p.timestamp DESC`;

const getPost = `WITH post_likes AS (
SELECT "postId",
COUNT(*)::INT AS "numberOfLikes"
FROM liked_post
GROUP BY "postId"
const getPost = `
WITH post_likes AS (
SELECT "postId",
COUNT(*)::INT AS "numberOfLikes"
FROM liked_post
GROUP BY "postId"
),
post_replies_reposts AS (
SELECT "parentPostId",
COUNT(*)::INT AS "numberOfReplies",
COUNT(CASE WHEN "isRepost" = TRUE THEN 1 END) AS "numberOfReposts"
FROM post
WHERE "parentPostId" IS NOT NULL
GROUP BY "parentPostId"
)
SELECT p."postId",
u.username,
u."displayName",
p."textContent",
p.timestamp,
EXISTS(SELECT 1 FROM liked_post li WHERE li."userId" = $1 AND li."postId" = p."postId" LIMIT 1) AS "isLikedByCurrentUser",
COALESCE(l."numberOfLikes",0) AS "numberOfLikes"
FROM post AS p
LEFT JOIN post_likes AS l ON p."postId" = l."postId"
INNER JOIN app_user AS u ON p."userId" = u."userId"
WHERE p."postId" = $2`;
SELECT
p."postId",
u.username,
u."displayName",
u."userId",
p."textContent",
p.timestamp,
EXISTS(
SELECT 1
FROM liked_post li
WHERE li."userId" = $1
AND li."postId" = p."postId"
LIMIT 1
) AS "isLikedByCurrentUser",
COALESCE(l."numberOfLikes",0) AS "numberOfLikes",
COALESCE(r."numberOfReplies", 0) AS "numberOfReplies",
COALESCE(r."numberOfReposts", 0) AS "numberOfReposts"
FROM post AS p
LEFT JOIN post_likes AS l ON p."postId" = l."postId"
LEFT JOIN post_replies_reposts AS r ON p."postId" = r."parentPostId"
INNER JOIN app_user AS u ON p."userId" = u."userId"
WHERE p."postId" = $2`;

const getReplies = `WITH post_likes AS (
SELECT "postId",
COUNT(*)::INT AS "numberOfLikes"
FROM liked_post
GROUP BY "postId"
const getReplies = `
WITH post_likes AS (
SELECT "postId",
COUNT(*)::INT AS "numberOfLikes"
FROM liked_post
GROUP BY "postId"
),
post_replies_reposts AS (
SELECT "parentPostId",
COUNT(*)::INT AS "numberOfReplies",
COUNT(CASE WHEN "isRepost" = TRUE THEN 1 END) AS "numberOfReposts"
FROM post
WHERE "parentPostId" IS NOT NULL
GROUP BY "parentPostId"
)
SELECT p."postId",
u.username,
u."displayName",
p."textContent",
p.timestamp,
p."parentPostId",
EXISTS(SELECT 1 FROM liked_post li WHERE li."userId" = $1 AND li."postId" = p."postId" LIMIT 1) AS "isLikedByCurrentUser",
COALESCE(l."numberOfLikes",0) AS "numberOfLikes"
FROM post AS p
LEFT JOIN post_likes AS l ON p."postId" = l."postId"
INNER JOIN app_user AS u ON p."userId" = u."userId"
WHERE p."parentPostId" = $2
ORDER BY "numberOfLikes" DESC`;
SELECT
p."postId",
u.username,
u."displayName",
u."userId",
p."textContent",
p.timestamp,
p."parentPostId",
EXISTS(
SELECT 1
FROM liked_post li
WHERE li."userId" = $1
AND li."postId" = p."postId"
LIMIT 1
) AS "isLikedByCurrentUser",
COALESCE(l."numberOfLikes",0) AS "numberOfLikes",
COALESCE(r."numberOfReplies", 0) AS "numberOfReplies",
COALESCE(r."numberOfReposts", 0) AS "numberOfReposts"
FROM post AS p
LEFT JOIN post_likes AS l ON p."postId" = l."postId"
LEFT JOIN post_replies_reposts AS r ON p."postId" = r."parentPostId"
INNER JOIN app_user AS u ON p."userId" = u."userId"
WHERE p."parentPostId" = $2
ORDER BY "numberOfLikes" DESC`;

const likePost = `INSERT INTO liked_post ("userId", "postId") VALUES ($1, $2)`;

Expand Down
Loading

0 comments on commit 2c53c9a

Please sign in to comment.