Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removal of posts_ids and comments_ids from user #49

Merged
merged 5 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions seeds/UserSeed.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ async function generateRandomUsers() {
chat_request_email: true,
unsubscribe_from_all_emails: false,
},
posts_ids: [],
comments_ids: [],

followed_posts_ids: [],
saved_posts_ids: [],
hidden_and_reported_posts_ids: [],
Expand All @@ -150,13 +149,6 @@ async function generateRandomUsers() {
const salt = await bcrypt.genSalt(10);
fakeUser.password = await bcrypt.hash(fakeUser.password, salt);

fakeUser.token = jwt.sign({ _id: i + 1 }, JWT_SECRET, {
expiresIn: "1d",
});
fakeUser.refreshToken = jwt.sign({ _id: i + 1 }, JWT_SECRET, {
expiresIn: "8d",
});

users.push(fakeUser);
}

Expand Down
52 changes: 52 additions & 0 deletions src/controller/postListing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Post } from "../db/models/Post.js";
import { verifyAuthToken } from "./userAuth.js";
import { User } from "../db/models/User.js";
import { getSortCriteria } from "../utils/lisitng.js";
import { getPostsHelper } from "../services/lisitngs.js";

export async function getPostsPaginated(
request,
pageNumber = 1,
pageSize = 10,
sortBy
) {
try {
let user = null;

// Check if request has Authorization header
if (request.headers.authorization) {
const {
success,
err,
status,
user: authenticatedUser,
msg,
} = await verifyAuthToken(request);
if (!authenticatedUser) {
return { success, err, status, user: authenticatedUser, msg };
}
user = authenticatedUser;
}

// Calculate the offset based on pageNumber and pageSize
const offset = (pageNumber - 1) * pageSize;

// Fetch posts with pagination and sorting
const { posts } = await getPostsHelper(user, offset, pageSize, sortBy);

return {
success: true,
status: 200,
posts,
msg: "Posts retrieved successfully.",
};
} catch (error) {
console.error("Error:", error);
return {
success: false,
status: 500,
err: "Internal Server Error",
msg: "An error occurred while retrieving posts.",
};
}
}
145 changes: 91 additions & 54 deletions src/controller/userInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
getPostsHelper,
getCommunitiesHelper,
getModeratedCommunitiesHelper,
getUserPostsHelper,
getUserCommentsHelper,
} from "../services/users.js";

export async function getFollowers(request) {
Expand Down Expand Up @@ -79,34 +81,19 @@ export async function getFollowingCount(request) {
};
}

export async function getPosts(request, postType) {
export async function getUserPosts(request, postType) {
try {
let user = null;
if (postType !== "posts_ids") {
const {
success,
err,
status,
user: authenticatedUser,
msg,
} = await verifyAuthToken(request);
if (!authenticatedUser) {
return { success, err, status, user: authenticatedUser, msg };
}
user = authenticatedUser;
} else {
const { username } = request.params;
user = await User.findOne({ username });
if (!user) {
return {
success: false,
err: "No user found with username",
status: 404,
msg: "User not found",
};
}
const { username } = request.params;
const user = await User.findOne({ username });
if (!user) {
return {
success: false,
err: "No user found with username",
status: 404,
msg: "User not found",
};
}
const posts = await getPostsHelper(user, postType);
const posts = await getUserPostsHelper(user, postType);
return {
success: true,
status: 200,
Expand All @@ -124,34 +111,52 @@ export async function getPosts(request, postType) {
}
}

export async function getComments(request, commentType) {
export async function getPosts(request) {
let user = null;
try {
let user = null;
if (commentType !== "comments_ids") {
const {
success,
err,
status,
user: authenticatedUser,
msg,
} = await verifyAuthToken(request);
if (!authenticatedUser) {
return { success, err, status, user: authenticatedUser, msg };
}
user = authenticatedUser;
} else {
const { username } = request.params;
user = await User.findOne({ username });
if (!user) {
return {
success: false,
err: "No user found with username",
status: 404,
msg: "User not found",
};
}
const {
success,
err,
status,
user: authenticatedUser,
msg,
} = await verifyAuthToken(request);
if (!authenticatedUser) {
return { success, err, status, user: authenticatedUser, msg };
}
user = authenticatedUser;
const posts = await getPostsHelper(user);
return {
success: true,
status: 200,
posts,
msg: "Posts retrieved successfully.",
};
} catch (error) {
console.error("Error:", error);
return {
success: false,
status: 500,
err: "Internal Server Error",
msg: "An error occurred while retrieving posts.",
};
}
}

export async function getUserComments(request) {
try {
const { username } = request.params;
const user = await User.findOne({ username });
if (!user) {
return {
success: false,
err: "No user found with username",
status: 404,
msg: "User not found",
};
}
const comments = await getCommentsHelper(user, commentType);

const comments = await getUserCommentsHelper(user);
return {
success: true,
status: 200,
Expand All @@ -169,6 +174,38 @@ export async function getComments(request, commentType) {
}
}

export async function getComments(request) {
let user = null;
try {
const {
success,
err,
status,
user: authenticatedUser,
msg,
} = await verifyAuthToken(request);
if (!authenticatedUser) {
return { success, err, status, user: authenticatedUser, msg };
}
user = authenticatedUser;
const comments = await getCommentsHelper(user);
return {
success: true,
status: 200,
comments,
msg: "Comments retrieved successfully.",
};
} catch (error) {
console.error("Error:", error);
return {
success: false,
status: 500,
err: "Internal Server Error",
msg: "An error occurred while retrieving posts.",
};
}
}

export async function getOverview(request) {
try {
const { username } = request.params;
Expand All @@ -181,8 +218,8 @@ export async function getOverview(request) {
msg: "User not found",
};
}
const posts = await getPostsHelper(user, "posts_ids");
const comments = await getCommentsHelper(user, "comments_ids");
const posts = await getUserPostsHelper(user);
const comments = await getUserCommentsHelper(user);

return {
success: true,
Expand Down
15 changes: 1 addition & 14 deletions src/db/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,20 +282,7 @@ const userSchema = new mongoose.Schema({
default: false,
},
},
posts_ids: {
type: Array,
items: {
type: mongoose.Schema.Types.ObjectId,
ref: "Post",
},
},
comments_ids: {
type: Array,
items: {
type: mongoose.Schema.Types.ObjectId,
ref: "Comment",
},
},

followed_posts_ids: {
type: Array,
items: {
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from "express";
import dotenv from "dotenv";
import { usersRouter } from "./routers/users.js";
import { communityRouter } from "./routers/communities.js";
import { listingPostsRouter } from "./routers/lisitng.js";
dotenv.config();
import { connect_to_db } from "./db/mongoose.js";

Expand All @@ -27,7 +28,7 @@ app.listen(port, () => {
console.log("Server is Up");
});

app.use([usersRouter, communityRouter]);
app.use([usersRouter, communityRouter, listingPostsRouter]);

// Error handling middleware
app.use((err, req, res, next) => {
Expand Down
Loading
Loading