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!
-
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?ld8– ld82024年08月11日 20:06:31 +00:00Commented Aug 11, 2024 at 20:06
1 Answer 1
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)
);