diff --git a/controllers/news_v2.go b/controllers/news_v2.go index 24d7c268..fd37d1cf 100644 --- a/controllers/news_v2.go +++ b/controllers/news_v2.go @@ -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)), }, } @@ -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 diff --git a/internal/news/query.go b/internal/news/query.go index fa71710a..64227470 100644 --- a/internal/news/query.go +++ b/internal/news/query.go @@ -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"}, @@ -53,7 +55,7 @@ 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) @@ -61,20 +63,20 @@ func NewQuery(options ...func(*Query)) *Query { 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 @@ -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)}} }