0

I am writing a blog with categories for the posts. I would like for users who are logged in to be able to select "My favorites" and create a private list of posts so that "My favorites" would be a category each person could set up for themselves and add to as they find a post they like. Is there a simple javascript I could write or use that would do this? Or is there another way? I realize I would have to have a button or a link on each post that says "Add to My favorites" as well, which may make this a lot more complicated than I think. thanks for any suggestions!

asked Jun 27, 2024 at 9:45
1
  • Does anyone else have a suggestion for a mySQL database? And a way for each user who wishes to do so to set up his own private category? In other words, a login within the larger blog login I guess? Commented Aug 11, 2024 at 20:06

1 Answer 1

0

Assuming you are using mongoDb as your database. You can create a separated collection which would look like this:

{
 _id: unique id assigned my mongodb,
 userId: id of the user,
 savedPosts: [ 
 { id: post id, name: post name }, 
 { id: post id, name: post name }, 
 .. ..
 ]
}

This way you can fetch saved/favourite posts of users separately and display the name on frontend

If the user clicks on the specific post you can open a new page and fetch this post using its id there.

Also you can set indexing with respect to userId field for faster retrieval. You can check this article : link

For SQL:

CREATE TABLE SavedPosts (
postId VARCHAR(255) NOT NULL,
postName VARCHAR(255) NOT NULL,
userId VARCHAR(255),
FOREIGN KEY (userId) REFERENCES Users(_id)
);
answered Jun 27, 2024 at 9:53
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Jayendra for the suggestions, but I’m using mySQL (hosting my server with GoDaddy) not mongoDb. I apologize for not specifying that.
@Id8 updated the answer for relational databases

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.