Skip to content

Commit

Permalink
remove profile type
Browse files Browse the repository at this point in the history
tags & favorite usecases
  • Loading branch information
Matthieu Jacquot committed Jul 29, 2018
1 parent d700bf9 commit c917199
Show file tree
Hide file tree
Showing 23 changed files with 218 additions and 103 deletions.
10 changes: 1 addition & 9 deletions domain/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,10 @@ type Article struct {
UpdatedAt time.Time `json:"updatedAt"`
Favorited bool `json:"favorited"`
FavoritesCount int `json:"favoritesCount"`
Author Profile `json:"author"`
Author User `json:"author"`
Comments []Comment `json:"comments"`
}

type Comment struct {
ID int `json:"id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Body string `json:"body"`
Author Profile `json:"author"`
}

type ArticleCollection []Article

func (articles ArticleCollection) ApplyLimitAndOffset(limit, offset int) ArticleCollection {
Expand Down
11 changes: 11 additions & 0 deletions domain/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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"`
}
8 changes: 4 additions & 4 deletions domain/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ 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
}
//type Profile struct {
// User
// Following bool
//}

func (user User) Follows(userName string) bool {
if user.FollowIDs == nil {
Expand Down
3 changes: 1 addition & 2 deletions implem/gin.server/profileFollowDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package server
import (
"net/http"

"github.com/err0r500/go-realworld-clean/domain"
"github.com/err0r500/go-realworld-clean/implem/json.formatter"
"github.com/gin-gonic/gin"
)
Expand All @@ -18,5 +17,5 @@ func (rH RouterHandler) profileFollowDelete(c *gin.Context) {
return
}

c.JSON(http.StatusOK, gin.H{"profile": formatter.NewProfileFromDomain(domain.Profile{User: *user, Following: false})})
c.JSON(http.StatusOK, gin.H{"profile": formatter.NewProfileFromDomain(*user, false)})
}
3 changes: 1 addition & 2 deletions implem/gin.server/profileFollowPost.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package server
import (
"net/http"

"github.com/err0r500/go-realworld-clean/domain"
"github.com/err0r500/go-realworld-clean/implem/json.formatter"
"github.com/gin-gonic/gin"
)
Expand All @@ -18,5 +17,5 @@ func (rH RouterHandler) profileFollowPost(c *gin.Context) {
return
}

c.JSON(http.StatusOK, gin.H{"profile": formatter.NewProfileFromDomain(domain.Profile{User: *user, Following: true})})
c.JSON(http.StatusOK, gin.H{"profile": formatter.NewProfileFromDomain(*user, false)})
}
8 changes: 6 additions & 2 deletions implem/gin.server/profileGet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import (

func (rH RouterHandler) profileGet(c *gin.Context) {
log := rH.log(rH.MethodAndPath(c))
requestingUserName := ""
if userName, err := rH.authHandler.GetUserName(c.GetHeader("Authorization")); err == nil {
requestingUserName = userName
}

profile, err := rH.ucHandler.ProfileGet(c.Param("username"))
user, follows, err := rH.ucHandler.ProfileGet(requestingUserName, c.Param("username"))
if err != nil {
log(err)
c.Status(http.StatusUnprocessableEntity)
return
}

c.JSON(http.StatusOK, gin.H{"profile": formatter.NewProfileFromDomain(*profile)})
c.JSON(http.StatusOK, gin.H{"profile": formatter.NewProfileFromDomain(*user, follows)})
}
36 changes: 33 additions & 3 deletions implem/gin.server/profileGet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/err0r500/go-realworld-clean/testData"
"github.com/gin-gonic/gin"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/baloo.v3"
)

Expand All @@ -19,11 +20,11 @@ func TestProfileGet_happyCase(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

janeFollowingRick := testData.Profile("janeFollowingRick")
jane := testData.User("jane")
ucHandler := uc.NewMockHandler(mockCtrl)
ucHandler.EXPECT().
ProfileGet(testData.User("rick").Name).
Return(&janeFollowingRick, nil).
ProfileGet("", testData.User("rick").Name).
Return(&jane, true, nil).
Times(1)

gE := gin.Default()
Expand All @@ -39,3 +40,32 @@ func TestProfileGet_happyCase(t *testing.T) {
JSONSchema(testData.ProfileRespDefinition).
Done()
}

func TestProfileGet_happyCaseAuthenticated(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

jane := testData.User("jane")
ucHandler := uc.NewMockHandler(mockCtrl)
ucHandler.EXPECT().
ProfileGet(jane.Name, testData.User("rick").Name).
Return(&jane, true, nil).
Times(1)

jwtHandler := jwt.NewTokenHandler("mySalt")
gE := gin.Default()
server.NewRouter(ucHandler, jwtHandler).SetRoutes(gE)
authToken, err := jwtHandler.GenUserToken(testData.User("jane").Name)
assert.NoError(t, err)

ts := httptest.NewServer(gE)
defer ts.Close()

baloo.New(ts.URL).
Get(profileGetPath).
AddHeader("Authorization", authToken).
Expect(t).
Status(200).
JSONSchema(testData.ProfileRespDefinition).
Done()
}
2 changes: 1 addition & 1 deletion implem/json.formatter/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewArticleFromDomain(article domain.Article) Article {
Body: article.Body,
CreatedAt: article.CreatedAt.UTC().Format(dateFormat),
UpdatedAt: article.UpdatedAt.UTC().Format(dateFormat),
Author: NewProfileFromDomain(article.Author),
Author: NewProfileFromDomain(article.Author, false), //fixme : check this !
Tags: article.TagList,
Favorite: article.Favorited,
FavoritesCount: article.FavoritesCount,
Expand Down
2 changes: 1 addition & 1 deletion implem/json.formatter/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ func NewCommentFromDomain(comment domain.Comment) Comment {
CreatedAt: comment.CreatedAt.UTC().Format(dateFormat),
UpdatedAt: comment.UpdatedAt.UTC().Format(dateFormat),
Body: comment.Body,
Author: NewProfileFromDomain(comment.Author),
Author: NewProfileFromDomain(comment.Author, false), //fixme check this
}
}
4 changes: 2 additions & 2 deletions implem/json.formatter/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Profile struct {
Following bool `json:"following"`
}

func NewProfileFromDomain(user domain.Profile) Profile {
func NewProfileFromDomain(user domain.User, following bool) Profile {
var bio, image string
if user.Bio != nil {
bio = *user.Bio
Expand All @@ -24,6 +24,6 @@ func NewProfileFromDomain(user domain.Profile) Profile {
Username: user.Name,
Bio: bio,
Picture: image,
Following: user.Following,
Following: following,
}
}
3 changes: 3 additions & 0 deletions implem/mock.uc/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type MockedInteractor struct {
AuthHandler *MockAuthHandler
Slugger *MockSlugger
ArticleValidator *MockArticleValidator
TagsRW *MockTagsRW
}

type SimpleLogger struct{}
Expand All @@ -36,6 +37,7 @@ func NewMockedInteractor(mockCtrl *gomock.Controller) MockedInteractor {
AuthHandler: NewMockAuthHandler(mockCtrl),
Slugger: NewMockSlugger(mockCtrl),
ArticleValidator: NewMockArticleValidator(mockCtrl),
TagsRW: NewMockTagsRW(mockCtrl),
}
}

Expand All @@ -49,5 +51,6 @@ func (i MockedInteractor) GetUCHandler() uc.Handler {
AuthHandler: i.AuthHandler,
Slugger: i.Slugger,
ArticleValidator: i.ArticleValidator,
TagsRW: i.TagsRW,
}.New()
}
30 changes: 16 additions & 14 deletions implem/mock.uc/handler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions implem/mock.uc/interactor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 14 additions & 14 deletions testData/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ var jane = domain.User{
Password: "janePassword",
}

func Profile(name string) domain.Profile {
switch name {
case "janeFollowingRick":
return janeFollowingRick
default:
return janeFollowingRick
}
}
//func Profile(name string) domain.Profile {
// switch name {
// case "janeFollowingRick":
// return janeFollowingRick
// default:
// return janeFollowingRick
// }
//}

var janeFollowingRick = domain.Profile{
User: rick,
Following: true,
}
//var janeFollowingRick = domain.Profile{
// User: rick,
// Following: true,
//}

var janeArticle = domain.Article{
Slug: "articleSlug",
Expand All @@ -66,13 +66,13 @@ var janeArticle = domain.Article{
UpdatedAt: time.Now(),
Favorited: true,
FavoritesCount: 123,
Author: domain.Profile{User: jane, Following: false},
Author: jane,
Comments: []domain.Comment{
{ID: 123,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Body: "commentBody",
Author: domain.Profile{User: rick, Following: false},
Author: rick,
},
},
}
7 changes: 6 additions & 1 deletion uc/HANDLER.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Handler interface {
}

type ProfileLogic interface {
ProfileGet(userName string) (profile *domain.Profile, err error)
ProfileGet(requestingUserName, userName string) (profile *domain.User, follows bool, err error)
ProfileUpdateFollow(loggedInUsername, username string, follow bool) (user *domain.User, err error)
}

Expand Down Expand Up @@ -62,6 +62,7 @@ type HandlerConstructor struct {
AuthHandler AuthHandler
Slugger Slugger
ArticleValidator ArticleValidator
TagsRW TagsRW
}

func (c HandlerConstructor) New() Handler {
Expand All @@ -86,6 +87,9 @@ func (c HandlerConstructor) New() Handler {
if c.ArticleValidator == nil {
log.Fatal("missing ArticleValidator")
}
if c.TagsRW == nil {
log.Fatal("missing TagsRW")
}

return interactor{
logger: c.Logger,
Expand All @@ -95,5 +99,6 @@ func (c HandlerConstructor) New() Handler {
authHandler: c.AuthHandler,
slugger: c.Slugger,
articleValidator: c.ArticleValidator,
tagsRW: c.TagsRW,
}
}
2 changes: 1 addition & 1 deletion uc/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (i interactor) ArticlePost(username string, article domain.Article) (*domai
}

article.Slug = slug
article.Author = domain.Profile{User: *user, Following: false}
article.Author = *user

if err := i.articleValidator.BeforeCreationCheck(&article); err != nil {
return nil, err
Expand Down
Loading

0 comments on commit c917199

Please sign in to comment.