Skip to content

Commit

Permalink
push
Browse files Browse the repository at this point in the history
  • Loading branch information
chryzcode committed Jul 23, 2024
1 parent 834bebb commit ed55325
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
78 changes: 64 additions & 14 deletions controllers/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import { Post, postLikes } from "../models/post.js";
import Notification from "../models/notification.js";
import User from "../models/user.js";
import Follower from "../models/follower.js";
import Tag from "../models/tag.js"
import { Comment } from "../models/comment.js";
import "dotenv/config.js";
import { uploadToCloudinary } from "../utils/cloudinaryConfig.js";

const DOMAIN = process.env.DOMAIN;



export const createPost = async (req, res) => {
req.body.author = req.user.userId;

Expand Down Expand Up @@ -144,9 +143,7 @@ export const getPost = async (req, res) => {
// Function to get likes for the post
const getLikesForPost = async postId => {
try {
const likes = await postLikes
.find({ post: postId })
.populate("user", "username firstName image lastName _id");
const likes = await postLikes.find({ post: postId }).populate("user", "username firstName image lastName _id");
return likes;
} catch (error) {
console.error(`Error fetching likes for post ${postId}:`, error);
Expand All @@ -156,10 +153,7 @@ export const getPost = async (req, res) => {

const getPostComments = async postId => {
try {
const comments = await Comment.find({ post: postId }).populate(
"user",
"username firstName image lastName _id"
);
const comments = await Comment.find({ post: postId }).populate("user", "username firstName image lastName _id");
return comments;
} catch (error) {
console.error(`Error fetching comment for post ${postId}:`, error);
Expand All @@ -181,8 +175,6 @@ export const getUserPosts = async (req, res) => {
res.status(StatusCodes.OK).json({ userPosts });
};



export const deletePost = async (req, res) => {
const { postId } = req.params;
const userId = req.user.userId;
Expand Down Expand Up @@ -253,9 +245,7 @@ export const aPostLikes = async (req, res) => {
if (!post) {
throw new NotFoundError(`Post not found`);
}
const likes = await postLikes
.find({ post: postId })
.populate("user", "username firstName lastName image _id");
const likes = await postLikes.find({ post: postId }).populate("user", "username firstName lastName image _id");
res.status(StatusCodes.OK).json({ likes });
};

Expand Down Expand Up @@ -288,3 +278,63 @@ export const getAUserPosts = async (req, res) => {
);
res.status(StatusCodes.OK).json({ posts });
};

export const searchPosts = async (req, res) => {
const { title, author, tags, type } = req.query;

// Initialize query object
let query = {};

if (title) {
query.title = { $regex: title, $options: "i" }; // Case-insensitive regex search
}

if (author) {
// Split author query to handle first and last names
const authorParts = author.split(" ").filter(part => part.trim() !== "");
let authorQuery = [];

// Create regex for each part and combine into search query
authorParts.forEach(part => {
const regex = new RegExp(part, "i"); // Case-insensitive regex
authorQuery.push({ firstName: regex }, { lastName: regex }, { username: regex });
});

// Find the author(s) matching the query using case-insensitive regex
const authorMatches = await User.find({
$or: authorQuery,
});

if (authorMatches.length > 0) {
const authorIds = authorMatches.map(user => user._id);
query.author = { $in: authorIds };
} else {
// If no matching author, return empty result
return res.status(200).json({ posts: [] });
}
}

if (tags) {
// Split tags by comma and trim spaces
const tagArray = tags.split(",").map(tag => tag.trim());
// Find the tags matching the query using case-insensitive regex
const tagMatches = await Tag.find({ name: { $in: tagArray.map(tag => new RegExp(tag, "i")) } });

if (tagMatches.length > 0) {
const tagIds = tagMatches.map(tag => tag._id);
query.tag = { $in: tagIds };
} else {
// If no matching tags, return empty result
return res.status(200).json({ posts: [] });
}
}

if (type) {
query.type = type;
}

const posts = await Post.find(query).populate("author tag");

res.status(200).json({ posts });

};
2 changes: 2 additions & 0 deletions routes/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
getPersonalisedPosts,
aPostLikes,
getAUserPosts,
searchPosts,
} from "../controllers/post.js";
import authenticateUser from "../middleware/authentication.js";
import { multerUpload } from "../utils/cloudinaryConfig.js";
Expand All @@ -28,5 +29,6 @@ router
.delete(authenticateUser, deletePost);
router.route("/like/:postId").post(authenticateUser, likePost).get(aPostLikes);
router.route("/unlike/:postId").post(authenticateUser, unlikePost);
router.route("/search-posts").post(searchPosts);

export default router;

0 comments on commit ed55325

Please sign in to comment.