Skip to content

Commit

Permalink
improve json formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu Jacquot committed Aug 5, 2018
1 parent 906b0d7 commit 888693d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 36 deletions.
28 changes: 14 additions & 14 deletions domain/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ import (
)

type Article struct {
Slug string `json:"slug"`
Title string `json:"title"`
Description string `json:"description"`
Body string `json:"body"`
TagList []string `json:"tagList"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
FavoritedBy []User `json:"favoritedBy"`
FavoritesCount int `json:"favoritesCount"`
Author User `json:"author"`
Comments []Comment `json:"comments"`
Slug string
Title string
Description string
Body string
TagList []string
CreatedAt time.Time
UpdatedAt time.Time
FavoritedBy []User
//FavoritesCount int
Author User
Comments []Comment
}

type ArticleFilter func(Article) bool

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

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

func ArticleIsFavoritedBy(username string) func(article Article) bool {
func ArticleIsFavoritedBy(username string) ArticleFilter {
return func(article Article) bool {
if username == "" {
return false
Expand Down
10 changes: 5 additions & 5 deletions domain/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package domain
import "time"

type Comment struct {
ID int `json:"id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Body string `json:"body"`
Author User `json:"author"`
ID int
CreatedAt time.Time
UpdatedAt time.Time
Body string
Author User
}
6 changes: 0 additions & 6 deletions domain/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@ func SetUserPassword(input *string) func(fields *User) {
}
}

// Profile represents a user account with follow property if his followed by the user
//type Profile struct {
// User
// Following bool
//}

func (user User) Follows(userName string) bool {
if user.FollowIDs == nil {
return false
Expand Down
2 changes: 1 addition & 1 deletion implem/json.formatter/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewArticleFromDomain(article domain.Article, username string) Article {
Author: NewProfileFromDomain(article.Author, false), //fixme : check this !
Tags: article.TagList,
Favorite: domain.ArticleIsFavoritedBy(username)(article),
FavoritesCount: article.FavoritesCount,
FavoritesCount: len(article.FavoritedBy),
}
}

Expand Down
19 changes: 9 additions & 10 deletions testData/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,15 @@ var jane = domain.User{
const TokenPrefix = "Token "

var janeArticle = domain.Article{
Slug: "articleSlug",
Title: "articleTitle",
Description: "description",
Body: "body",
TagList: []string{"tagList"},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
FavoritedBy: []domain.User{rick},
FavoritesCount: 123,
Author: jane,
Slug: "articleSlug",
Title: "articleTitle",
Description: "description",
Body: "body",
TagList: []string{"tagList"},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
FavoritedBy: []domain.User{rick},
Author: jane,
Comments: []domain.Comment{
{ID: 123,
CreatedAt: time.Now(),
Expand Down

0 comments on commit 888693d

Please sign in to comment.