model

package
v0.0.0-...-9b3a020 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 24, 2021 License: MIT Imports: 7 Imported by: 0

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

    Column Type Nullable Default
    id integer not null nextval('users_id_seq'::regclass)
    username character varying(20) not null
    email character varying(20) not null
    password_hashed character varying(50) not null
    created_on timestamp without time zone CURRENT_TIMESTAMP
    updated_on timestamp without time zone CURRENT_TIMESTAMP
    last_login timestamp without time zone CURRENT_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

    Column Type Nullable Default
    id integer not null nextval('post_id_seq'::regclass)
    author_id integer not null
    title character varying(100) not null
    summary character varying(500)
    published boolean false
    created_on timestamp without time zone CURRENT_TIMESTAMP
    updated_on timestamp without time zone CURRENT_TIMESTAMP
    like_count integer 0
    views integer 0
    body text

    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

    Column Type Nullable Default
    id integer not null nextval('post_id_seq'::regclass)
    author_id integer not null
    created_on timestamp without time zone CURRENT_TIMESTAMP
    updated_on timestamp without time zone CURRENT_TIMESTAMP
    body text

    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

    Column Type Nullable Default
    id integer not null nextval('tags_id_seq'::regclass)
    title character varying(100) not null
    summary character varying(500)
    total_post integer 0

    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

    Column Type Nullable
    post_id integer not null
    tag_id integer not 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

    Column Type Nullable
    comment_id integer not null
    post_id integer not 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

    Column Type Nullable
    author_id integer not null
    post_id integer not 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();
    
    

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteTags

func DeleteTags(db *pgxpool.Pool, tagID, postID int) error

Delete will remove tag from a post. REQUIRE: TagID, PostID

func GetPostsOfTag

func GetPostsOfTag(db *pgxpool.Pool, tagID int, limit int) ([]int, error)

GetPostsOfTag bring PostID's related to a tag

func GetTagsOfPost

func GetTagsOfPost(db *pgxpool.Pool, postID int) ([]int, error)

GetTagsOfPost Will Return all the tagsID on a post REQUIRE: postID

func GetTopPostIDs

func GetTopPostIDs(db *pgxpool.Pool, limit int) ([]int, error)

GetTop return ID's of top viewed Posts. limit is the max number of post require. REQUIRE:limit

Types

type Comment

type Comment struct {
	ID        int       `json:"id,omitempty"`
	AuthorID  int       `json:"author_id,omitempty"`
	Body      string    `json:"body,omitempty"`
	CreatedOn time.Time `json:"created_on,omitempty"`
	UpdatedOn time.Time `json:"updated_on,omitempty"`
}

Store Data regarding commments

func GetComments

func GetComments(db *pgxpool.Pool, postID int64) ([]Comment, error)

GetComments will return All the comments on a post,REQUIRE: PostID

func (*Comment) Delete

func (c *Comment) Delete(db *pgxpool.Pool) error

Delete a comment from a post. REQUIRE: CommentID

func (*Comment) Post

func (c *Comment) Post(db *pgxpool.Pool, postID int) error

Post will put comment on a post. REQUIRE: postID,AuthorID,Body

type Post

type Post struct {
	ID        int       `json:"id"`
	AuthorID  int       `json:"author_id"`
	Title     string    `json:"title"`
	Summary   string    `json:"summary,omitempty"`
	Body      string    `json:"body,omitempty"`
	Published bool      `json:"published,omitempty"`
	CreatedOn time.Time `json:"created_on,omitempty"`
	UpdatedOn time.Time `json:"updated_on,omitempty"`
	LikeCount int       `json:"like_count,omitempty"`
	Views     int       `json:"views,omitempty"`
}

Post is the model present in the database

func GetPostbyID

func GetPostbyID(db *pgxpool.Pool, postID int) (Post, error)

Get update the post object with published post stored in database REQUIRE: PostID

func (*Post) Create

func (p *Post) Create(db *pgxpool.Pool) error

Create will create a draft of post on the database. REQUIRE:AuthorID, Title, ?Summary

func (*Post) GetDraft

func (p *Post) GetDraft(db *pgxpool.Pool) error

GetDraftPost get the Drafted Post from Database. REQUIRE:AuthorID, ID

func (*Post) Init

func (p *Post) Init(author int, title string, summary string)

func (*Post) PatchDrafted

func (p *Post) PatchDrafted(db *pgxpool.Pool) error

PatchDrafted Update's Drafted Post, Update the values of Post object before calling it. REQUIRE: AuthorID, PostID

type PostInterface

type PostInterface interface {
	Create(db *pgxpool.Pool) error
	Get(db *pgxpool.Pool) error
	GetDraft(db *pgxpool.Pool) error
	Init(author int, title string, summary string)
	PatchDrafted(db *pgxpool.Pool) error
}

type Tag

type Tag struct {
	ID        int    `json:"id,omitempty"`
	Title     string `json:"title,omitempty"`
	Summary   string `json:"summary,omitempty"`
	TotalPost int    `json:"total_post,omitempty"`
}

func GetTagData

func GetTagData(db *pgxpool.Pool, tagID int) (Tag, error)

// GetTagData Will return Tag object From Database REQUIRE: TagID

func GetTopTags

func GetTopTags(db *pgxpool.Pool, limit int) ([]Tag, error)

GetTopTags bring Top tags with data by limit, REQUIRE:Limit

func (*Tag) AttachMe

func (t *Tag) AttachMe(db *pgxpool.Pool, postID int) error

AttachMe will add tag to a Post. REQUIRE: TagID,PostID

func (*Tag) Create

func (t *Tag) Create(db *pgxpool.Pool) error

Create will add tag In Database. REQUIRE: Title, Summary

type User

type User struct {
	ID        int       `json:"id,omitempty"`
	UserName  string    `json:"username,omitempty"`
	Email     string    `json:"email,omitempty"`
	Password  string    `json:"-"`
	CreatedOn time.Time `json:"created_on,omitempty"`
	UpdatedOn time.Time `json:"updated_on,omitempty"`
	LastLogin time.Time `json:"last_login,omitempty"`
}

User is the model present in the database

func FindUserByID

func FindUserByID(db *pgxpool.Pool, uid int) (*User, error)

FindUserByID will find a user with specific UID

func (*User) GetLikedPost

func (u *User) GetLikedPost(db *pgxpool.Pool) ([]int, error)

GetLikedPost return the array of postID liked by user. REQUIRE: ID

func (*User) Init

func (u *User) Init(username, email, password string)

init will initiate user object value.

func (*User) Login

func (u *User) Login(db *pgxpool.Pool) (int, error)

Login will check the user detail and send the UID REQUIRE: username|email, Password.

func (*User) PatchLike

func (u *User) PatchLike(db *pgxpool.Pool, postID int) error

PatchLike will can put like/Remove like from a post. REQUIRE: ID

func (*User) PutNewPassword

func (u *User) PutNewPassword(db *pgxpool.Pool, newPassword string) error

PutNewPassword will update the password. REQUIRE: ID

func (*User) SignUp

func (u *User) SignUp(db *pgxpool.Pool) (int, error)

SignUp will save user detail into database. REQUIRE: User Object init.

func (*User) Validate

func (u *User) Validate(action string) error

Validate Function check if the user Data data is filled or not. For smooth database entry.

type UserInterface

type UserInterface interface {
	GetLikedPost(db *pgxpool.Pool) ([]int, error)
	Init(username, email, password string) error
	Login(db *pgxpool.Pool) (int, error)
	PatchLike(db *pgxpool.Pool, postID int) error
	PutNewPassword(db *pgxpool.Pool, newPassword string) error
	SignUp(db *pgxpool.Pool) (int, error)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL