Reddit voting consists of up voting and down voting. These votes decide rank of the post, which ultimately decides the position of the story link. Here in this article, we are going to see simple architecture and database structure to implement reddit voting system. Download Project DEMO Note It is really big project. Its difficult to explain every code snippet here. So please download the project Database



Lets build database first. Click here to read complete explanation on reddit database. Execute below SQL commands on your MySQL console CREATE schema demos ; CREATE TABLE reddit_post (parent_id BIGINT ( 20 ) NOT NULL ,post_id BIGINT ( 20 ) AUTO_INCREMENT, title varchar ( 200 ) NOT NULL , content varchar ( 2000 ) NOT NULL , link varchar ( 2083 ) NOT NULL , user_id varchar ( 25 ) NOT NULL , pic varchar ( 2083 ), status varchar ( 15 ) NOT NULL , type varchar ( 15 ) NOT NULL , votes INTEGER , created_time TIMESTAMP default CURRENT_TIMESTAMP , primary key (post_id) ); CREATE TABLE reddit_votes ( post_id BIGINT ( 20 ) NOT NULL , user_id VARCHAR ( 25 ) NOT NULL , vflag SMALLINT NOT NULL , last_updated_time TIMESTAMP default CURRENT_TIMESTAMP , primary key (post_id,user_id), foreign key (post_id) references reddit_post(post_id) ); CREATE TABLE reddit_user ( user_id VARCHAR ( 25 ), property VARCHAR ( 25 ) NOT NULL , value varchar ( 200 ) NOT NULL , last_updated_time TIMESTAMP default CURRENT_TIMESTAMP , primary key (user_id,property,value) ); CREATE TABLE reddit_ranks ( post_id BIGINT ( 20 ) NOT NULL , hot double, new double, raising double, controversial double, top double, foreign key (post_id) references reddit_post(post_id) ); CREATE TABLE reddit_rights ( post_id BIGINT ( 20 ) NOT NULL , property VARCHAR ( 25 ), user_id VARCHAR ( 25 ), assigned_user_id VARCHAR ( 25 ), foreign key (post_id) references reddit_post(post_id), foreign key (user_id) references reddit_user(user_id), foreign key (assigned_user_id) references reddit_user(user_id) ); INSERT INTO reddit_user(user_id, property, value) values ( 'admin' , 'name' , 'admin' ); INSERT INTO reddit_user(user_id, property, value) values ( 'srinivas' , 'name' , 'srinivas dasari' ); INSERT INTO reddit_user(user_id, property, value) values ( 'ramesh' , 'name' , 'ramesh' ); INSERT INTO reddit_post(parent_id, title, content, link, user_id, pic, status, type, votes) VALUES ( 0 , 'java' , 'sub reddit for java developers' , '/r/java' , 'admin' , 'java_subreddit.png' , 'active' , 'subreddit' , 0 ); INSERT INTO reddit_post(parent_id, title, content, link, user_id, pic, status, type, votes) VALUES ( 0 , 'angularjs' , 'sub reddit for AngularJS developers' , '/r/angularjs' , 'admin' , 'angularjs_subreddit.png' , 'active' , 'subreddit' , 0 ); INSERT INTO reddit_post(parent_id, title, content, link, user_id, pic, status, type, votes) VALUES ( 0 , 'sql' , 'sub reddit for sql developers' , '/r/sql' , 'admin' , 'sql_subreddit.png' , 'active' , 'subreddit' , 0 ); INSERT INTO reddit_post(parent_id, title, content, link, user_id, pic, status, type, votes) VALUES ( 1 , 'Java Restful Webservice Tutorial with Sample case study' , '' , 'http://blog.sodhanalibrary.com/2013/09/restful-web-service-tutorial-with-java.html' , 'srinivas' , 'http://4.bp.blogspot.com/-IWnld2WXOCg/UinxJK9ahMI/AAAAAAAAAAw/b8mFC6bl41w/s1600/screen2.bmp' , 'active' , 'post' , 1 ); INSERT INTO reddit_votes(post_id, user_id, vflag) VALUES (LAST_INSERT_ID(), 'srinivas' , 1 ); INSERT INTO reddit_post(parent_id, title, content, link, user_id, pic, status, type, votes) VALUES ( 2 , 'Responsive Web Design Using AngularJS Material UI' , '' , 'http://blog.sodhanalibrary.com/2015/08/responsive-web-design-using-angularjs.html' , 'srinivas' , '' , 'active' , 'post' , 1 ); INSERT INTO reddit_votes(post_id, user_id, vflag) VALUES (LAST_INSERT_ID(), 'srinivas' , 1 ); INSERT INTO reddit_post(parent_id, title, content, link, user_id, pic, status, type, votes) VALUES ( 2 , 'Responsive Web Design Using AngularJS Material UI' , '' , 'http://blog.sodhanalibrary.com/2015/08/responsive-web-design-using-angularjs.html' , 'srinivas' , '' , 'active' , 'post' , 1 ); INSERT INTO reddit_votes(post_id, user_id, vflag) VALUES (LAST_INSERT_ID(), 'srinivas' , 1 ); INSERT INTO reddit_post(parent_id, title, content, link, user_id, pic, status, type, votes) VALUES ( 4 , '' , 'good one' , '/r/java/4/comments' , 'ramesh' , '' , 'active' , 'comment' , 1 ); INSERT INTO reddit_votes(post_id, user_id, vflag) VALUES (LAST_INSERT_ID(), 'srinivas' , 1 );

Program Flow

Here we will see what are basic rules to implement this functionality

(In the given demo, the default user id is "admin", No authentication required)

Load posts

Send request from jQuery to server, Server will send details by digging database. Find below for database queries





Select only post data from reddit_posts

select * from reddit_post where TYPE = 'post'



select * from reddit_votes where POST_ID = ? and USER_ID = ? Now display the given data to user using jQuery

User Voting Select user vote for postNow display the given data to user using jQuery