Skip to content

Commit

Permalink
improve get recent by filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu Jacquot committed Aug 4, 2018
1 parent 2a83b05 commit 74c1722
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 27 deletions.
23 changes: 23 additions & 0 deletions domain/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,29 @@ type Article struct {
Comments []Comment `json:"comments"`
}

type ArticleFilter func(Article) bool

func Hastag(tag string) func(article Article) bool {
return func(article Article) bool {
for _, articleTag := range article.TagList {
if articleTag == tag {
return true
}
}
return false
}
}

func HasAuthor(authorName string) func(article Article) bool {
return func(article Article) bool {
return article.Author.Name == authorName
}
}

func IsFavorited(article Article) bool {
return article.Favorited
}

type ArticleCollection []Article

func (articles ArticleCollection) ApplyLimitAndOffset(limit, offset int) ArticleCollection {
Expand Down
23 changes: 20 additions & 3 deletions implem/memory.articleRW/readWriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,27 @@ func (rw rw) GetByAuthorsNameOrderedByMostRecentAsc(usernames []string) ([]domai
return toReturn, nil
}

func (rw) GetRecentFiltered(filters uc.Filters) ([]domain.Article, error) {
// todo => check if its AND or OR filters
func (rw rw) GetRecentFiltered(filters []domain.ArticleFilter) ([]domain.Article, error) {
var recentArticles []domain.Article

return nil, nil
rw.store.Range(func(key, value interface{}) bool {
article, ok := value.(domain.Article)
if !ok {
// not an article (shouldn't happen) -> skip
return true // log this but continue
}

for _, funcToApply := range filters {
if !funcToApply(article) { // "AND filter" : if one of the filter is at false, skip the article
return true
}
}

recentArticles = append(recentArticles, article)
return true
})

return recentArticles, nil
}

func (rw rw) Save(article domain.Article) (*domain.Article, error) {
Expand Down
4 changes: 2 additions & 2 deletions implem/mock.uc/handler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions implem/mock.uc/interactor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion uc/HANDLER.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type UserLogic interface {

type ArticlesLogic interface {
ArticlesFeed(username string, limit, offset int) (articles domain.ArticleCollection, totalArticleCount int, err error)
GetArticles(limit, offset int, filters Filters) (articles domain.ArticleCollection, totalArticleCount int, err error)
GetArticles(limit, offset int, filters []domain.ArticleFilter) (articles domain.ArticleCollection, totalArticleCount int, err error)
}

type ArticleLogic interface {
Expand Down
3 changes: 2 additions & 1 deletion uc/INTERACTOR.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type ArticleRW interface {
Save(domain.Article) (*domain.Article, error)
GetBySlug(slug string) (*domain.Article, error)
GetByAuthorsNameOrderedByMostRecentAsc(usernames []string) ([]domain.Article, error)
GetRecentFiltered(filters Filters) ([]domain.Article, error)
GetRecentFiltered(filters []domain.ArticleFilter) ([]domain.Article, error)
Delete(slug string) error
}

Expand All @@ -52,6 +52,7 @@ type CommentRW interface {

type TagsRW interface {
GetAll() ([]string, error)
Add(newTags []string) error
}

type Slugger interface {
Expand Down
4 changes: 4 additions & 0 deletions uc/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func (i interactor) ArticlePost(username string, article domain.Article) (*domai
return nil, err
}

if err := i.tagsRW.Add(article.TagList); err != nil {
return nil, err
}

return completeArticle, nil
}

Expand Down
1 change: 1 addition & 0 deletions uc/article_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestInteractor_ArticlePost(t *testing.T) {
i.Slugger.EXPECT().NewSlug(article.Title).Return(slug).Times(1)
i.ArticleRW.EXPECT().GetBySlug(slug).Return(nil, errors.New("not found")).Times(1)
i.ArticleValidator.EXPECT().BeforeCreationCheck(gomock.Any()).Return(nil).Times(1)
i.TagsRW.EXPECT().Add(gomock.Any())
i.ArticleRW.EXPECT().Create(gomock.Any())

_, err := i.GetUCHandler().ArticlePost(rick.Name, article)
Expand Down
28 changes: 10 additions & 18 deletions uc/articlesRecent.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
package uc

import (
"strconv"

"github.com/err0r500/go-realworld-clean/domain"
)

type Filters struct {
AuthorFilter *string
TagFilter *string
FavoritedFilter *bool
}

func NewFilters(author, tag, favorite string) Filters {
filters := Filters{}
func NewFilters(author, tag, favorite string) []domain.ArticleFilter {
filters := []domain.ArticleFilter{}
if author != "" {
filters.AuthorFilter = &author
filters = append(filters, domain.HasAuthor(author))
}
if tag != "" {
filters.TagFilter = &tag
filters = append(filters, domain.Hastag(tag))
}

fav, err := strconv.ParseBool(favorite)
if err != nil {
return filters
}
filters.FavoritedFilter = &fav
//fav, err := strconv.ParseBool(favorite)
//if err != nil {
// return filters
//}
//filters.FavoritedFilter = &fav

return filters
}

func (i interactor) GetArticles(limit, offset int, filters Filters) (domain.ArticleCollection, int, error) {
func (i interactor) GetArticles(limit, offset int, filters []domain.ArticleFilter) (domain.ArticleCollection, int, error) {
if limit <= 0 {
return domain.ArticleCollection{}, 0, nil
}
Expand Down

0 comments on commit 74c1722

Please sign in to comment.