github.com/ngocphuongnb/tetua@v0.0.7-alpha/app/entities/comment.go (about)

     1  package entities
     2  
     3  import (
     4  	"net/url"
     5  	"strconv"
     6  	"time"
     7  
     8  	"github.com/ngocphuongnb/tetua/app/utils"
     9  )
    10  
    11  type Comment struct {
    12  	ID          int        `json:"id,omitempty"`
    13  	CreatedAt   *time.Time `json:"created_at,omitempty"`
    14  	UpdatedAt   *time.Time `json:"updated_at,omitempty"`
    15  	DeletedAt   *time.Time `json:"deleted_at,omitempty"`
    16  	Content     string     `json:"content,omitempty" validate:"required"`
    17  	ContentHTML string     `json:"content_html,omitempty" validate:"required"`
    18  	Votes       int64      `json:"votes,omitempty"`
    19  	PostID      int        `json:"post_id,omitempty"`
    20  	UserID      int        `json:"user_id,omitempty"`
    21  	ParentID    int        `json:"parent_id,omitempty"`
    22  	Parent      *Comment
    23  	Post        *Post
    24  	User        *User
    25  }
    26  
    27  type CommentMutation struct {
    28  	PostID   int    `json:"post_id" form:"post_id" validate:"required"`
    29  	ParentID int    `json:"parent_id" form:"parent_id"`
    30  	Content  string `json:"content" form:"content" validate:"required"`
    31  }
    32  
    33  type CommentFilter struct {
    34  	*Filter
    35  	PostIDs   []int `form:"post_ids" json:"post_ids"`
    36  	UserIDs   []int `form:"user_ids" json:"user_ids"`
    37  	ParentIDs []int `form:"parent_ids" json:"parent_ids"`
    38  }
    39  
    40  func (p *CommentFilter) Base() string {
    41  	q := url.Values{}
    42  	if !utils.SliceContains(p.IgnoreUrlParams, "search") && p.Search != "" {
    43  		q.Add("q", p.Search)
    44  	}
    45  	if !utils.SliceContains(p.IgnoreUrlParams, "post") && len(p.PostIDs) > 0 {
    46  		q.Add("post", strconv.Itoa(p.PostIDs[0]))
    47  	}
    48  	if !utils.SliceContains(p.IgnoreUrlParams, "user") && len(p.UserIDs) > 0 {
    49  		q.Add("user", strconv.Itoa(p.UserIDs[0]))
    50  	}
    51  	if !utils.SliceContains(p.IgnoreUrlParams, "parent") && len(p.ParentIDs) > 0 {
    52  		q.Add("parent", strconv.Itoa(p.ParentIDs[0]))
    53  	}
    54  
    55  	if queryString := q.Encode(); queryString != "" {
    56  		return p.FilterBaseUrl() + "?" + q.Encode()
    57  	}
    58  
    59  	return p.FilterBaseUrl()
    60  }