package
0.0.0-20210324111005-9b3a0200ebd5
Repository: https://github.com/kushagramehta/example-blog-server.git
Documentation: pkg.go.dev

# README

Tables & Relatioship

alt text

How To create tables

  1. Create a database

    CREATE DATABASE blog;
    

    OR

    In command-line

    createdb -h localhost -p 5432 -U postgres blog
    
  2. Select database

    \c blog;
    

    OR

    In command-line

    psql -h localhost -p 5432 -U postgress blog
    
  3. Create Table

    User table
     CREATE TABLE IF NOT EXISTS USERS(
     ID              SERIAL   PRIMARY KEY,
     USERNAME        VARCHAR(20) NOT NULL UNIQUE,
     EMAIL           VARCHAR(20) NOT NULL UNIQUE,
     PASSWORD_HASHED VARCHAR(50) NOT NULL,
     CREATED_ON      timestamp DEFAULT CURRENT_TIMESTAMP,
     UPDATED_ON      timestamp DEFAULT CURRENT_TIMESTAMP,
     LAST_LOGIN      timestamp DEFAULT CURRENT_TIMESTAMP
     );
    

    Print table \d users

    ColumnTypeNullableDefault
    idintegernot nullnextval('users_id_seq'::regclass)
    usernamecharacter varying(20)not null
    emailcharacter varying(20)not null
    password_hashedcharacter varying(50)not null
    created_ontimestamp without time zoneCURRENT_TIMESTAMP
    updated_ontimestamp without time zoneCURRENT_TIMESTAMP
    last_logintimestamp without time zoneCURRENT_TIMESTAMP

    Indexes:

    • "users_pkey" PRIMARY KEY, btree (id)
    • "users_email_key" UNIQUE CONSTRAINT, btree (email)
    • "users_username_key" UNIQUE CONSTRAINT, btree (username)
    Post Table
     CREATE TABLE IF NOT EXISTS POSTS(
     ID              SERIAL   PRIMARY KEY,
     AUTHOR_ID       INTEGER NOT NULL,
     TITLE           VARCHAR(100) NOT NULL,
     SUMMARY         VARCHAR(500),
     PUBLISHED       BOOLEAN DEFAULT FALSE,
     CREATED_ON      timestamp DEFAULT CURRENT_TIMESTAMP,
     UPDATED_ON      timestamp DEFAULT CURRENT_TIMESTAMP,
     LIKE_COUNT      INTEGER DEFAULT 0,
     VIEWS           INTEGER DEFAULT 0,
     BODY            TEXT,
     FOREIGN KEY (AUTHOR_ID)
         REFERENCES USERS (ID)
         ON DELETE NO ACTION
         ON UPDATE NO ACTION
    
     );
    

    Print table \d posts

    ColumnTypeNullableDefault
    idintegernot nullnextval('post_id_seq'::regclass)
    author_idintegernot null
    titlecharacter varying(100)not null
    summarycharacter varying(500)
    publishedbooleanfalse
    created_ontimestamp without time zoneCURRENT_TIMESTAMP
    updated_ontimestamp without time zoneCURRENT_TIMESTAMP
    like_countinteger0
    viewsinteger0
    bodytext

    Indexes:

    • "posts_pkey" PRIMARY KEY, btree (id)
    • Foreign-key constraints: "posts_author_id_fkey" FOREIGN KEY (author_id) REFERENCES users(id)
    Comments Table
    CREATE TABLE IF NOT EXISTS COMMENTS(
    ID              SERIAL   PRIMARY KEY,
    AUTHOR_ID       INTEGER NOT NULL,
    CREATED_ON      timestamp DEFAULT CURRENT_TIMESTAMP,
    UPDATED_ON      timestamp DEFAULT CURRENT_TIMESTAMP,
    BODY            TEXT,
    FOREIGN KEY (AUTHOR_ID)
        REFERENCES USERS (ID)
        ON DELETE NO ACTION
    );
    

    Print table \d COMMENTS

    ColumnTypeNullableDefault
    idintegernot nullnextval('post_id_seq'::regclass)
    author_idintegernot null
    created_ontimestamp without time zoneCURRENT_TIMESTAMP
    updated_ontimestamp without time zoneCURRENT_TIMESTAMP
    bodytext

    Indexes:

    • "comments_pkey" PRIMARY KEY, btree (id)
    • Foreign-key constraints: "comments_author_id_fkey" FOREIGN KEY (author_id) REFERENCES users(id)
    Tags Table
    CREATE TABLE IF NOT EXISTS TAGS(
    ID              SERIAL   PRIMARY KEY,
    TITLE           VARCHAR(100) NOT NULL,
    SUMMARY         VARCHAR(500),
    TOTAL_POST      INTEGER DEFAULT 0
    );
    

    Print table \d COMMENTS

    ColumnTypeNullableDefault
    idintegernot nullnextval('tags_id_seq'::regclass)
    titlecharacter varying(100)not null
    summarycharacter varying(500)
    total_postinteger0

    Indexes:

    • "tags_pkey" PRIMARY KEY, btree (id)
    Post Tags Table
    CREATE TABLE IF NOT EXISTS POST_TAGS(
    POST_ID     INTEGER NOT NULL,
    TAG_ID      INTEGER NOT NULL,
    PRIMARY KEY (POST_ID,TAG_ID),
    FOREIGN KEY (POST_ID)
        REFERENCES POSTS (ID)
        ON DELETE CASCADE,
    FOREIGN KEY (TAG_ID)
        REFERENCES TAGS (ID)
        ON DELETE CASCADE
    );
    

    Print table \d POST_TAGS

    ColumnTypeNullable
    post_idintegernot null
    tag_idintegernot null

    Indexes:

    • "post_tags_pkey" PRIMARY KEY, btree (post_id, tag_id)

    Foreign-key constraints:

    • "post_tags_post_id_fkey" FOREIGN KEY (post_id) REFERENCES post(id) ON DELETE CASCADE
    • "post_tags_tag_id_fkey" FOREIGN KEY (tag_id) REFERENCES tags(id) ON DELETE CASCADE
    Post Comments Table
    CREATE TABLE IF NOT EXISTS POST_COMMENTS(
    COMMENT_ID      INTEGER NOT NULL,
    POST_ID         INTEGER NOT NULL,
    PRIMARY KEY (COMMENT_ID,POST_ID),
    FOREIGN KEY (COMMENT_ID)
        REFERENCES COMMENTS (ID)
        ON DELETE CASCADE,
    FOREIGN KEY (POST_ID)
        REFERENCES POSTS (ID)
        ON DELETE CASCADE
    );
    

    Print table \d POST_COMMENTS

    ColumnTypeNullable
    comment_idintegernot null
    post_idintegernot null

    Indexes:

    • "post_comments_pkey" PRIMARY KEY, btree (comment_id, post_id)

    Foreign-key constraints:

    • "post_comments_comment_id_fkey" FOREIGN KEY (comment_id) REFERENCES comments(id) ON DELETE CASCADE
    • "post_comments_post_id_fkey" FOREIGN KEY (post_id) REFERENCES post(id) ON DELETE CASCADE
    Post likes Table
    CREATE TABLE IF NOT EXISTS POST_LIKES(
    AUTHOR_ID       INTEGER NOT NULL,
    POST_ID         INTEGER NOT NULL,
    PRIMARY KEY (AUTHOR_ID,POST_ID),
    FOREIGN KEY (AUTHOR_ID)
        REFERENCES USERS (ID)
        ON DELETE NO ACTION,
    FOREIGN KEY (POST_ID)
        REFERENCES POSTS (ID)
        ON DELETE CASCADE
    );
    

    Print table \d POST_LIKES

    ColumnTypeNullable
    author_idintegernot null
    post_idintegernot null

    Indexes:

    • "post_likes_pkey" PRIMARY KEY, btree (author_id, post_id)

    Foreign-key constraints:

    • "post_likes_author_id_fkey" FOREIGN KEY (author_id) REFERENCES users(id)
    • "post_likes_post_id_fkey" FOREIGN KEY (post_id) REFERENCES post(id) ON DELETE CASCADE
  4. Create Triggers

    For Updating LIKE Counter in Post
    
    CREATE FUNCTION updateLike() RETURNS TRIGGER
        AS $BODY$
            BEGIN
                IF (TG_OP = 'INSERT') THEN
                    UPDATE POSTS
                        set LIKE_COUNT = LIKE_COUNT + 1
                        where id = new.POST_ID;
                    RETURN NEW;
                ELSIF (TG_OP='DELETE') THEN
                    UPDATE POSTS
                        set LIKE_COUNT = LIKE_COUNT - 1
                        where id = old.POST_ID;
                    RETURN OLD;
                END IF;
                RETURN NULL;
            END;
        $BODY$
    LANGUAGE plpgsql;
    
    CREATE TRIGGER UPDATE_LIKE_COUNT
        AFTER INSERT OR DELETE ON POST_LIKES
        FOR EACH ROW EXECUTE PROCEDURE updateLike();
    
    For Updating Post Count in tags
    CREATE FUNCTION updatePostCount() RETURNS TRIGGER
        AS $BODY$
            BEGIN
                IF (TG_OP = 'INSERT') THEN
                    UPDATE TAGS
                        set TOTAL_POST = TOTAL_POST + 1
                        where id = new.TAG_ID;
                    RETURN NEW;
                ELSIF (TG_OP='DELETE') THEN
                    UPDATE TAGS
                        set TOTAL_POST = TOTAL_POST - 1
                        where id = old.TAG_ID;
                    RETURN OLD;
                END IF;
                RETURN NULL;
            END;
        $BODY$
    LANGUAGE plpgsql;
    
    CREATE TRIGGER UPDATE_TAG_POST_COUNT
        AFTER INSERT OR DELETE ON POST_TAGS
        FOR EACH ROW EXECUTE PROCEDURE updatePostCount();
    
    

# Functions

Delete will remove tag from a post.
FindUserByID will find a user with specific UID.
GetComments will return All the comments on a post,REQUIRE: PostID.
Get update the post object with published post stored in database REQUIRE: PostID.
GetPostsOfTag bring PostID's related to a tag.
// GetTagData Will return Tag object From Database REQUIRE: TagID.
GetTagsOfPost Will Return all the tagsID on a post REQUIRE: postID.
GetTop return ID's of top viewed Posts.
GetTopTags bring Top tags with data by limit, REQUIRE:Limit.

# Structs

Store Data regarding commments.
Post
Post is the model present in the database.
No description provided by the author
User is the model present in the database.

# Interfaces

No description provided by the author
No description provided by the author