From db92d73648aca79aa38bf2cc5dc632fef9718c80 Mon Sep 17 00:00:00 2001 From: Matthieu Jacquot Date: Tue, 14 Aug 2018 11:20:53 +0200 Subject: [PATCH] fix null comments & missing User.CreatedAt & User.UpdatedAt --- domain/user.go | 3 +++ implem/json.formatter/comment.go | 4 ++-- implem/json.formatter/profile.go | 4 ++++ implem/json.formatter/user.go | 24 ++++++++++++++---------- implem/memory.userRW/readWriter.go | 11 ++++++++--- implem/memory.userRW/readWriter_test.go | 10 +++++++++- testData/jsonShemas.go | 12 +++++++++++- uc/commentsGet.go | 3 +++ 8 files changed, 54 insertions(+), 17 deletions(-) diff --git a/domain/user.go b/domain/user.go index 53e7c90..9b4dfee 100644 --- a/domain/user.go +++ b/domain/user.go @@ -2,6 +2,7 @@ package domain import ( "sort" + "time" ) // User represents a user account in the system @@ -13,6 +14,8 @@ type User struct { ImageLink *string FollowIDs []string Favorites []Article + CreatedAt time.Time + UpdatedAt time.Time } type UserUpdatableProperty int diff --git a/implem/json.formatter/comment.go b/implem/json.formatter/comment.go index ce96b35..6fedc48 100644 --- a/implem/json.formatter/comment.go +++ b/implem/json.formatter/comment.go @@ -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)) } @@ -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), } } diff --git a/implem/json.formatter/profile.go b/implem/json.formatter/profile.go index 4df0cb3..aa986ef 100644 --- a/implem/json.formatter/profile.go +++ b/implem/json.formatter/profile.go @@ -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 { @@ -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), } } diff --git a/implem/json.formatter/user.go b/implem/json.formatter/user.go index f0a3a60..4524197 100644 --- a/implem/json.formatter/user.go +++ b/implem/json.formatter/user.go @@ -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 { @@ -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), } } diff --git a/implem/memory.userRW/readWriter.go b/implem/memory.userRW/readWriter.go index 35b70a2..2fc2c28 100644 --- a/implem/memory.userRW/readWriter.go +++ b/implem/memory.userRW/readWriter.go @@ -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" @@ -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) @@ -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 diff --git a/implem/memory.userRW/readWriter_test.go b/implem/memory.userRW/readWriter_test.go index 0f48634..c488bf1 100644 --- a/implem/memory.userRW/readWriter_test.go +++ b/implem/memory.userRW/readWriter_test.go @@ -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) } diff --git a/testData/jsonShemas.go b/testData/jsonShemas.go index 349faf3..a43dd2e 100644 --- a/testData/jsonShemas.go +++ b/testData/jsonShemas.go @@ -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" ] }` diff --git a/uc/commentsGet.go b/uc/commentsGet.go index 4be7c56..d8ba1c6 100644 --- a/uc/commentsGet.go +++ b/uc/commentsGet.go @@ -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 }