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

     1  package entities
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"strconv"
     7  	"time"
     8  
     9  	"github.com/ngocphuongnb/tetua/app/utils"
    10  )
    11  
    12  // Post is the model entity for the Post schema.
    13  type Post struct {
    14  	ID              int        `json:"id,omitempty"`
    15  	CreatedAt       *time.Time `json:"created_at,omitempty"`
    16  	UpdatedAt       *time.Time `json:"updated_at,omitempty"`
    17  	DeletedAt       *time.Time `json:"deleted_at,omitempty"`
    18  	Name            string     `json:"name,omitempty" validate:"max=255"`
    19  	Slug            string     `json:"slug,omitempty" validate:"max=255"`
    20  	Description     string     `json:"description,omitempty" validate:"max=255"`
    21  	Content         string     `json:"content,omitempty" validate:"required"`
    22  	ContentHTML     string     `json:"content_html,omitempty"`
    23  	ViewCount       int64      `json:"view_count,omitempty"`
    24  	CommentCount    int64      `json:"comment_count,omitempty"`
    25  	RatingCount     int64      `json:"rating_count,omitempty"`
    26  	RatingTotal     int64      `json:"rating_total,omitempty"`
    27  	Draft           bool       `json:"draft,omitempty"`
    28  	Approved        bool       `json:"approved,omitempty"`
    29  	FeaturedImageID int        `json:"featured_image_id,omitempty"`
    30  	UserID          int        `json:"user_id,omitempty"`
    31  	User            *User      `json:"user,omitempty"`
    32  	FeaturedImage   *File      `json:"featured_image,omitempty"`
    33  	Topics          []*Topic   `json:"topics,omitempty"`
    34  	TopicIDs        []int      `json:"topic_ids,omitempty"`
    35  }
    36  
    37  type PostMutation struct {
    38  	Name            string `form:"name" json:"name"`
    39  	Slug            string `form:"name" json:"slug"`
    40  	Description     string `form:"description" json:"description"`
    41  	Content         string `form:"content" json:"content"`
    42  	ContentHTML     string `form:"content_html" json:"content_html"`
    43  	TopicIDs        []int  `form:"topic_ids" json:"topic_ids"`
    44  	Draft           bool   `form:"draft" json:"draft"`
    45  	FeaturedImageID int    `form:"featured_image_id" json:"featured_image_id"`
    46  }
    47  
    48  type PostFilter struct {
    49  	*Filter
    50  	Approve  string `form:"approve" json:"approve"`           // approve = all, approved, pending
    51  	Publish  string `form:"publish_type" json:"publish_type"` // publish_type = all, published, draft
    52  	UserIDs  []int  `form:"user_ids" json:"user_ids"`
    53  	TopicIDs []int  `form:"topic_ids" json:"topic_ids"`
    54  }
    55  
    56  func (p *Post) Url() string {
    57  	return utils.Url(fmt.Sprintf("%s-%d.html", p.Slug, p.ID))
    58  }
    59  
    60  func (p *PostFilter) Base() string {
    61  	q := url.Values{}
    62  	if !utils.SliceContains(p.IgnoreUrlParams, "search") && p.Search != "" {
    63  		q.Add("q", p.Search)
    64  	}
    65  	if !utils.SliceContains(p.IgnoreUrlParams, "topic") && len(p.TopicIDs) > 0 {
    66  		q.Add("topic", strconv.Itoa(p.TopicIDs[0]))
    67  	}
    68  	if !utils.SliceContains(p.IgnoreUrlParams, "user") && len(p.UserIDs) > 0 {
    69  		q.Add("user", strconv.Itoa(p.UserIDs[0]))
    70  	}
    71  	if !utils.SliceContains(p.IgnoreUrlParams, "publish") && p.Publish != "" {
    72  		q.Add("publish", p.Publish)
    73  	}
    74  	if !utils.SliceContains(p.IgnoreUrlParams, "approve") && p.Approve != "" {
    75  		q.Add("approve", p.Approve)
    76  	}
    77  
    78  	if queryString := q.Encode(); queryString != "" {
    79  		return p.FilterBaseUrl() + "?" + q.Encode()
    80  	}
    81  
    82  	return p.FilterBaseUrl()
    83  }