Skip to content

Commit

Permalink
add tag & favorite usecases
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu Jacquot committed Jul 29, 2018
1 parent 4e4bb4c commit d700bf9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
17 changes: 17 additions & 0 deletions domain/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type User struct {
Bio *string
ImageLink *string
FollowIDs []string
Favorites []Article
}

func UpdateUser(initial *User, opts ...func(fields *User)) {
Expand Down Expand Up @@ -101,3 +102,19 @@ 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
}
}
5 changes: 5 additions & 0 deletions uc/INTERACTOR.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type interactor struct {
authHandler AuthHandler
slugger Slugger
commentRW CommentRW
tagsRW TagsRW
}

// Logger : only used to log stuff
Expand Down Expand Up @@ -49,6 +50,10 @@ type CommentRW interface {
Delete(id int) error
}

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

type Slugger interface {
NewSlug(string) string
}
Expand Down
19 changes: 17 additions & 2 deletions uc/favorite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ package uc

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

func (i interactor) FavoritesUpdate(username, slug string, favortie bool) (*domain.Article, error) {
return nil, nil
func (i interactor) FavoritesUpdate(username, slug string, favorite bool) (*domain.Article, error) {
article, err := i.articleRW.GetBySlug(slug)
if err != nil {
return nil, err
}

user, err := i.userRW.GetByName(username)
if err != nil {
return nil, err
}

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

return article, nil
}
2 changes: 1 addition & 1 deletion uc/tags.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package uc

func (i interactor) Tags() ([]string, error) {
return nil, nil
return i.tagsRW.GetAll()
}

0 comments on commit d700bf9

Please sign in to comment.