You click the like button on a post in social media, on dev.to, or somewhere else. Have you ever gotten to think about how that little button and its system was made? How it worked?
It's not just "oh I pressed the like button". There is actually a lot of code behind it.
If I modeled a post table in SQL, it would look something like this:
CREATE TABLE Post ( id INTEGER NOT NULL, text VARCHAR(500), author VARCHAR(50), PRIMARY KEY (id) )
If you look in detail, the
id key will tell which post it is. The id is the primary key.
The text is a string of 500 chars or less.
The author is a string of 50 chars or less. This is the username of the author in this example.
Now you are probably wondering "hey, you forgot the LIKE stat". I didn't. I will create a new table for that and show you how to link it.
CREATE TABLE Like ( id INTEGER NOT NULL, post_for INTEGER, author VARCHAR(50), PRIMARY KEY (id) )
The ID is obvious.
post_for stat determines which post the like has been submitted to. The post's id in this example.
The author is who pressed the like button or submitted the like.
Now, I'll show you how I would link them.
First, I'll let users make posts with text in them.
I will display the text and author of a post when it is submitted. Also, I add a like button in the post route
When the like button is pressed, submit a new like with the author being the user, the post_for stat being the post's id stat.
To calculate how many likes the post has, count all the likes that have the same ID as the post.
And finally make sure that a user can only like a post one time.
Some people prefer other methods and I know that not everyone wants to do this. I just want to explain how complex a simple Like is.
If anything was confusing, please tell me.
Thanks for reading.