Skip to content

Commit

Permalink
fix null comments & missing User.CreatedAt & User.UpdatedAt
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu Jacquot committed Aug 14, 2018
1 parent e6c2ad9 commit db92d73
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 17 deletions.
3 changes: 3 additions & 0 deletions domain/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package domain

import (
"sort"
"time"
)

// User represents a user account in the system
Expand All @@ -13,6 +14,8 @@ type User struct {
ImageLink *string
FollowIDs []string
Favorites []Article
CreatedAt time.Time
UpdatedAt time.Time
}
type UserUpdatableProperty int

Expand Down
4 changes: 2 additions & 2 deletions implem/json.formatter/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Comment struct {
}

func NewCommentsFromDomain(comments ...domain.Comment) []Comment {
var ret []Comment
ret := []Comment{} // return at least an empty array (not nil)
for _, comment := range comments {
ret = append(ret, NewCommentFromDomain(comment))
}
Expand All @@ -27,6 +27,6 @@ func NewCommentFromDomain(comment domain.Comment) Comment {
CreatedAt: comment.CreatedAt.UTC().Format(dateLayout),
UpdatedAt: comment.UpdatedAt.UTC().Format(dateLayout),
Body: comment.Body,
Author: NewProfileFromDomain(comment.Author, false), //fixme check this
Author: NewProfileFromDomain(comment.Author, false),
}
}
4 changes: 4 additions & 0 deletions implem/json.formatter/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ type Profile struct {
Bio string `json:"bio"`
Picture string `json:"image"`
Following bool `json:"following"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
}

func NewProfileFromDomain(user domain.User, following bool) Profile {
Expand All @@ -25,5 +27,7 @@ func NewProfileFromDomain(user domain.User, following bool) Profile {
Bio: bio,
Picture: image,
Following: following,
CreatedAt: user.CreatedAt.UTC().Format(dateLayout),
UpdatedAt: user.UpdatedAt.UTC().Format(dateLayout),
}
}
24 changes: 14 additions & 10 deletions implem/json.formatter/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package formatter
import "github.com/err0r500/go-realworld-clean/domain"

type UserResp struct {
Email string `json:"email"`
Token string `json:"token"`
Username string `json:"username"`
Bio string `json:"bio"`
Image string `json:"image"`
Email string `json:"email"`
Token string `json:"token"`
Username string `json:"username"`
Bio string `json:"bio"`
Image string `json:"image"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
}

func NewUserResp(user domain.User, token string) UserResp {
Expand All @@ -19,10 +21,12 @@ func NewUserResp(user domain.User, token string) UserResp {
image = *user.ImageLink
}
return UserResp{
Email: user.Email,
Token: token,
Username: user.Name,
Bio: bio,
Image: image,
Email: user.Email,
Token: token,
Username: user.Name,
Bio: bio,
Image: image,
CreatedAt: user.CreatedAt.UTC().Format(dateLayout),
UpdatedAt: user.UpdatedAt.UTC().Format(dateLayout),
}
}
11 changes: 8 additions & 3 deletions implem/memory.userRW/readWriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

"log"

"time"

"github.com/err0r500/go-realworld-clean/domain"
"github.com/err0r500/go-realworld-clean/testData"
"github.com/err0r500/go-realworld-clean/uc"
Expand Down Expand Up @@ -37,9 +39,11 @@ func (rw rw) Create(username, email, password string) (*domain.User, error) {
}

rw.store.Store(username, domain.User{
Name: username,
Email: email,
Password: password,
Name: username,
Email: email,
Password: password,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
})

return rw.GetByName(username)
Expand Down Expand Up @@ -86,6 +90,7 @@ func (rw rw) Save(user domain.User) error {
return uc.ErrNotFound
}

user.UpdatedAt = time.Now()
rw.store.Store(user.Name, user)

return nil
Expand Down
10 changes: 9 additions & 1 deletion implem/memory.userRW/readWriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,13 @@ func TestRw_GetByEmailAndPassword(t *testing.T) {
user, err := rw.GetByEmailAndPassword(jane.Email, jane.Password)

assert.NoError(t, err)
assert.Equal(t, jane, *user)
assert.Equal(t, jane.Name, user.Name)
assert.Equal(t, jane.Email, user.Email)
assert.Equal(t, jane.Password, user.Password)
assert.Equal(t, jane.Bio, user.Bio)
assert.Equal(t, jane.ImageLink, user.ImageLink)
assert.Equal(t, jane.FollowIDs, user.FollowIDs)
assert.Equal(t, jane.Favorites, user.Favorites)
assert.NotNil(t, jane.CreatedAt)
assert.NotNil(t, jane.UpdatedAt)
}
12 changes: 11 additions & 1 deletion testData/jsonShemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,23 @@ var ProfileDefinition = `
},
"following": {
"type": "boolean"
},
"createdAt": {
"type": "string",
"format": "date-time"
},
"updatedAt": {
"type": "string",
"format": "date-time"
}
},
"required": [
"username",
"bio",
"image",
"following"
"following",
"createdAt",
"updatedAt"
]
}`

Expand Down
3 changes: 3 additions & 0 deletions uc/commentsGet.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ func (i interactor) CommentsGet(slug string) ([]domain.Comment, error) {
if err != nil {
return nil, err
}
if article.Comments == nil {
article.Comments = []domain.Comment{}
}

return article.Comments, nil
}

0 comments on commit db92d73

Please sign in to comment.