Skip to content

Commit

Permalink
fix article.Favorite field
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu Jacquot committed Aug 5, 2018
1 parent 74c1722 commit 906b0d7
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 45 deletions.
32 changes: 27 additions & 5 deletions domain/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ type Article struct {
TagList []string `json:"tagList"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Favorited bool `json:"favorited"`
FavoritedBy []User `json:"favoritedBy"`
FavoritesCount int `json:"favoritesCount"`
Author User `json:"author"`
Comments []Comment `json:"comments"`
}

type ArticleFilter func(Article) bool

func Hastag(tag string) func(article Article) bool {
func ArticleHasTag(tag string) func(article Article) bool {
return func(article Article) bool {
for _, articleTag := range article.TagList {
if articleTag == tag {
Expand All @@ -31,14 +31,24 @@ func Hastag(tag string) func(article Article) bool {
}
}

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

func IsFavorited(article Article) bool {
return article.Favorited
func ArticleIsFavoritedBy(username string) func(article Article) bool {
return func(article Article) bool {
if username == "" {
return false
}
for _, user := range article.FavoritedBy {
if user.Name == username {
return true
}
}
return false
}
}

type ArticleCollection []Article
Expand Down Expand Up @@ -78,3 +88,15 @@ func (article *Article) UpdateComments(comment Comment, add bool) {
}
}
}
func (article *Article) UpdateFavoritedBy(user User, add bool) {
if add {
article.FavoritedBy = append(article.FavoritedBy, user)
return
}

for i := 0; i < len(article.Comments); i++ {
if article.FavoritedBy[i].Name == user.Name {
article.FavoritedBy = append(article.FavoritedBy[:i], article.FavoritedBy[i+1:]...) // memory leak ? https://github.com/golang/go/wiki/SliceTricks
}
}
}
16 changes: 0 additions & 16 deletions domain/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,3 @@ func (user *User) UpdateFollowees(followeeName string, follow bool) {
user.FollowIDs = nil
}
}

func (user *User) UpdateFavorites(favorite Article, fav bool) {
if fav {
user.Favorites = append(user.Favorites, favorite)
return
}

for i := 0; i < len(user.Favorites); i++ {
if user.Favorites[i].Slug == favorite.Slug {
user.Favorites = append(user.Favorites[:i], user.Favorites[i+1:]...) // memory leak ? https://github.com/golang/go/wiki/SliceTricks
}
}
if len(user.Favorites) == 0 {
user.Favorites = nil
}
}
6 changes: 3 additions & 3 deletions implem/gin.server/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (rH RouterHandler) articlePost(c *gin.Context) {
return
}

c.JSON(http.StatusCreated, gin.H{"article": formatter.NewArticleFromDomain(*article)})
c.JSON(http.StatusCreated, gin.H{"article": formatter.NewArticleFromDomain(*article, rH.getUserName(c))})
}

func (rH RouterHandler) articlePut(c *gin.Context) {
Expand All @@ -62,7 +62,7 @@ func (rH RouterHandler) articlePut(c *gin.Context) {
return
}

c.JSON(http.StatusOK, gin.H{"article": formatter.NewArticleFromDomain(*article)})
c.JSON(http.StatusOK, gin.H{"article": formatter.NewArticleFromDomain(*article, rH.getUserName(c))})
}

func (rH RouterHandler) articleGet(c *gin.Context) {
Expand All @@ -74,7 +74,7 @@ func (rH RouterHandler) articleGet(c *gin.Context) {
c.Status(http.StatusUnprocessableEntity)
return
}
c.JSON(http.StatusOK, gin.H{"article": formatter.NewArticleFromDomain(*article)})
c.JSON(http.StatusOK, gin.H{"article": formatter.NewArticleFromDomain(*article, rH.getUserName(c))})
}

func (rH RouterHandler) articleDelete(c *gin.Context) {
Expand Down
2 changes: 1 addition & 1 deletion implem/gin.server/articleFavorite.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ func (rH RouterHandler) updateFavorite(c *gin.Context) {
return
}

c.JSON(http.StatusOK, gin.H{"article": formatter.NewArticleFromDomain(*article)})
c.JSON(http.StatusOK, gin.H{"article": formatter.NewArticleFromDomain(*article, rH.getUserName(c))})
}
4 changes: 2 additions & 2 deletions implem/gin.server/articles.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (rH RouterHandler) articlesFilteredGet(c *gin.Context) {
return
}

c.JSON(http.StatusOK, gin.H{"articles": formatter.NewArticlesFromDomain(articles...), "articlesCount": count})
c.JSON(http.StatusOK, gin.H{"articles": formatter.NewArticlesFromDomain(rH.getUserName(c), articles...), "articlesCount": count})
}

func (rH RouterHandler) articlesFeedGet(c *gin.Context) {
Expand All @@ -66,5 +66,5 @@ func (rH RouterHandler) articlesFeedGet(c *gin.Context) {
return
}

c.JSON(http.StatusOK, gin.H{"articles": formatter.NewArticlesFromDomain(articles...), "articlesCount": count})
c.JSON(http.StatusOK, gin.H{"articles": formatter.NewArticlesFromDomain(rH.getUserName(c), articles...), "articlesCount": count})
}
1 change: 0 additions & 1 deletion implem/gin.server/articles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func TestArticlesFiltered(t *testing.T) {

func TestArticlesFeed(t *testing.T) {
t.Run("happyCase", func(t *testing.T) {

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

Expand Down
9 changes: 5 additions & 4 deletions implem/json.formatter/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Article struct {
FavoritesCount int `json:"favoritesCount"`
}

func NewArticleFromDomain(article domain.Article) Article {
func NewArticleFromDomain(article domain.Article, username string) Article {
return Article{
Slug: article.Slug,
Title: article.Title,
Expand All @@ -27,15 +27,16 @@ func NewArticleFromDomain(article domain.Article) Article {
UpdatedAt: article.UpdatedAt.UTC().Format(dateFormat),
Author: NewProfileFromDomain(article.Author, false), //fixme : check this !
Tags: article.TagList,
Favorite: article.Favorited,
Favorite: domain.ArticleIsFavoritedBy(username)(article),
FavoritesCount: article.FavoritesCount,
}
}

func NewArticlesFromDomain(articles ...domain.Article) []Article {
func NewArticlesFromDomain(username string, articles ...domain.Article) []Article {
ret := []Article{}

for _, article := range articles {
ret = append(ret, NewArticleFromDomain(article))
ret = append(ret, NewArticleFromDomain(article, username))
}

return ret
Expand Down
2 changes: 1 addition & 1 deletion testData/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var janeArticle = domain.Article{
TagList: []string{"tagList"},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Favorited: true,
FavoritedBy: []domain.User{rick},
FavoritesCount: 123,
Author: jane,
Comments: []domain.Comment{
Expand Down
15 changes: 6 additions & 9 deletions uc/articlesRecent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ import (
)

func NewFilters(author, tag, favorite string) []domain.ArticleFilter {
filters := []domain.ArticleFilter{}
var filters []domain.ArticleFilter
if author != "" {
filters = append(filters, domain.HasAuthor(author))
filters = append(filters, domain.ArticleHasAuthor(author))
}
if tag != "" {
filters = append(filters, domain.Hastag(tag))
filters = append(filters, domain.ArticleHasTag(tag))
}
if favorite != "" {
filters = append(filters, domain.ArticleIsFavoritedBy(favorite))
}

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

return filters
}
Expand Down
5 changes: 2 additions & 3 deletions uc/favorite.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ func (i interactor) FavoritesUpdate(username, slug string, favorite bool) (*doma
return nil, err
}

user.UpdateFavorites(*article, favorite)
article.UpdateFavoritedBy(*user, favorite)

if err := i.userRW.Save(*user); err != nil {
return nil, err
}

article.Favorited = favorite

return article, nil
}

0 comments on commit 906b0d7

Please sign in to comment.