Skip to content

Commit

Permalink
api/news: group Option functions
Browse files Browse the repository at this point in the history
This patch define the Option type for closure function fun(*Query) so
that all the option functions can be grouped under the Option type.
Also, prefix these functions with `With` denote the option usage.
  • Loading branch information
babygoat committed Jul 3, 2020
1 parent b74a6f3 commit 6203ef0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
24 changes: 12 additions & 12 deletions controllers/news_v2.go
Expand Up @@ -239,38 +239,38 @@ func (nc *newsV2Controller) getIndexPageJobs() []job {
{
Name: news.LatestSection,
Type: typePost,
Query: news.NewQuery(news.Limit(6)),
Query: news.NewQuery(news.WithLimit(6)),
}, {
Name: news.EditorPicksSection,
Type: typePost,
Query: news.NewQuery(
news.FilterIsFeatured(true),
news.Limit(6),
news.SortUpdatedAt(false)),
news.WithFilterIsFeatured(true),
news.WithLimit(6),
news.WithSortUpdatedAt(false)),
}, {
Name: news.LatestTopicSection,
Type: typeTopic,
Query: news.NewQuery(news.Limit(1)),
Query: news.NewQuery(news.WithLimit(1)),
}, {
Name: news.ReviewsSection,
Type: typePost,
Query: news.NewQuery(
news.FilterCategoryIDs(news.Review.ID),
news.Limit(4)),
news.WithFilterCategoryIDs(news.Review.ID),
news.WithLimit(4)),
}, {
Name: news.PhotoSection,
Type: typePost,
Query: news.NewQuery(
news.FilterCategoryIDs(news.Photography.ID),
news.Limit(6)),
news.WithFilterCategoryIDs(news.Photography.ID),
news.WithLimit(6)),
}, {
Name: news.InfographicSection,
Type: typePost,
Query: news.NewQuery(news.FilterStyle("interactive"), news.Limit(6)),
Query: news.NewQuery(news.WithFilterStyle("interactive"), news.WithLimit(6)),
}, {
Name: news.TopicsSection,
Type: typeTopic,
Query: news.NewQuery(news.Offset(1), news.Limit(4)),
Query: news.NewQuery(news.WithOffset(1), news.WithLimit(4)),
},
}

Expand All @@ -281,7 +281,7 @@ func (nc *newsV2Controller) getIndexPageJobs() []job {
jobs = append(jobs, job{
v.Name,
typePost,
news.NewQuery(news.FilterCategoryIDs(v.ID), news.Limit(1)),
news.NewQuery(news.WithFilterCategoryIDs(v.ID), news.WithLimit(1)),
})
}
return jobs
Expand Down
18 changes: 10 additions & 8 deletions internal/news/query.go
Expand Up @@ -45,6 +45,8 @@ const (
queryLimit = "limit"
)

type Option func(*Query)

var defaultQuery = Query{
Pagination: query.Pagination{Offset: 0, Limit: 10},
Filter: Filter{State: "published"},
Expand All @@ -53,28 +55,28 @@ var defaultQuery = Query{

// NewQuery returns a default query along with the options(pagination/sort/filter).
// Note that if the same options are specified multiple times, the last one will be applied.
func NewQuery(options ...func(*Query)) *Query {
func NewQuery(options ...Option) *Query {
q := defaultQuery
for _, o := range options {
o(&q)
}
return &q
}

func Offset(offset int) func(*Query) {
func WithOffset(offset int) Option {
return func(q *Query) {
q.Offset = offset
}
}

func Limit(limit int) func(*Query) {
func WithLimit(limit int) Option {
return func(q *Query) {
q.Limit = limit
}
}

// FilterCategoryIDs adds category ids into filter on the query
func FilterCategoryIDs(ids ...string) func(*Query) {
func WithFilterCategoryIDs(ids ...string) Option {
return func(q *Query) {
if len(ids) > 0 {
q.Filter.Categories = ids
Expand All @@ -83,28 +85,28 @@ func FilterCategoryIDs(ids ...string) func(*Query) {
}

// FilterState adds the post publish state filter on the query
func FilterState(state string) func(*Query) {
func WithFilterState(state string) Option {
return func(q *Query) {
q.Filter.State = state
}
}

// FilterStyle adds the post style filter on the query
func FilterStyle(style string) func(*Query) {
func WithFilterStyle(style string) Option {
return func(q *Query) {
q.Filter.Style = style
}
}

// FilterIsFeatured adds the isFeatured filter on the query
func FilterIsFeatured(isFeatured bool) func(*Query) {
func WithFilterIsFeatured(isFeatured bool) Option {
return func(q *Query) {
q.Filter.IsFeatured = null.BoolFrom(isFeatured)
}
}

// SortUpdatedAt updates the query to sort by updatedAt field
func SortUpdatedAt(isAsc bool) func(*Query) {
func WithSortUpdatedAt(isAsc bool) Option {
return func(q *Query) {
q.Sort = SortBy{UpdatedAt: query.Order{IsAsc: null.BoolFrom(isAsc)}}
}
Expand Down

0 comments on commit 6203ef0

Please sign in to comment.