Skip to content

Commit

Permalink
add comment usecases
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu Jacquot committed Jul 29, 2018
1 parent 82a1bab commit 4e4bb4c
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 16 deletions.
15 changes: 14 additions & 1 deletion domain/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Article struct {
}

type Comment struct {
ID string `json:"id"`
ID int `json:"id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Body string `json:"body"`
Expand Down Expand Up @@ -50,3 +50,16 @@ func (articles ArticleCollection) ApplyLimitAndOffset(limit, offset int) Article

return articles[min:max]
}

func (article *Article) UpdateComments(comment Comment, add bool) {
if add {
article.Comments = append(article.Comments, comment)
return
}

for i := 0; i < len(article.Comments); i++ {
if article.Comments[i].ID == comment.ID {
article.Comments = append(article.Comments[:i], article.Comments[i+1:]...) // memory leak ? https://github.com/golang/go/wiki/SliceTricks
}
}
}
10 changes: 8 additions & 2 deletions implem/gin.server/articleComment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package server
import (
"net/http"

"strconv"

"github.com/err0r500/go-realworld-clean/implem/json.formatter"
"github.com/gin-gonic/gin"
)
Expand Down Expand Up @@ -47,8 +49,12 @@ func (rH RouterHandler) commentPost(c *gin.Context) {

func (rH RouterHandler) commentDelete(c *gin.Context) {
log := rH.log(rH.MethodAndPath(c))

if err := rH.ucHandler.CommentsDelete(rH.getUserName(c), c.Param("slug"), c.Param("id")); err != nil {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.Status(http.StatusBadRequest)
return
}
if err := rH.ucHandler.CommentsDelete(rH.getUserName(c), c.Param("slug"), id); err != nil {
log(err)
c.Status(http.StatusUnprocessableEntity)
return
Expand Down
6 changes: 4 additions & 2 deletions implem/gin.server/articleComment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"net/http/httptest"
"testing"

"strconv"

"github.com/err0r500/go-realworld-clean/implem/gin.server"
"github.com/err0r500/go-realworld-clean/implem/jwt.authHandler"
"github.com/err0r500/go-realworld-clean/implem/mock.uc"
Expand Down Expand Up @@ -138,7 +140,7 @@ func TestArticleCommentDelete(t *testing.T) {

t.Run("happyCase", func(t *testing.T) {
baloo.New(ts.URL).
Delete(articleCommentPath+"/"+testData.Article("jane").Comments[0].ID).
Delete(articleCommentPath+"/"+strconv.Itoa(testData.Article("jane").Comments[0].ID)).
AddHeader("Authorization", authToken).
Expect(t).
Status(200).
Expand All @@ -147,7 +149,7 @@ func TestArticleCommentDelete(t *testing.T) {

t.Run("no auth", func(t *testing.T) {
baloo.New(ts.URL).
Delete(articleCommentPath + "/" + testData.Article("jane").Comments[0].ID).
Delete(articleCommentPath + "/" + strconv.Itoa(testData.Article("jane").Comments[0].ID)).
Expect(t).
Status(401).
Done()
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 @@ -5,7 +5,7 @@ import (
)

type Comment struct {
ID string `json:"id"`
ID int `json:"id"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
Body string `json:"body"`
Expand Down
4 changes: 2 additions & 2 deletions implem/mock.uc/handler.go

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

61 changes: 61 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.

2 changes: 1 addition & 1 deletion testData/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var janeArticle = domain.Article{
FavoritesCount: 123,
Author: domain.Profile{User: jane, Following: false},
Comments: []domain.Comment{
{ID: "1",
{ID: 123,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Body: "commentBody",
Expand Down
3 changes: 1 addition & 2 deletions testData/jsonShemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,13 @@ var ArticleMultipleRespDefinition = `{
]
}`

//fixme : id is integer in API contract
var CommentDefinition = `
` + ProfileDefinition + `,
"Comment": {
"type": "object",
"properties": {
"id": {
"type": "string"
"type": "integer"
},
"createdAt": {
"type": "string",
Expand Down
2 changes: 1 addition & 1 deletion uc/HANDLER.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type ArticleLogic interface {
type CommentsLogic interface {
CommentsGet(slug string) ([]domain.Comment, error)
CommentsPost(username, slug, comment string) (*domain.Comment, error)
CommentsDelete(username, slug, id string) error
CommentsDelete(username, slug string, id int) error
}

type FavoritesLogic interface {
Expand Down
7 changes: 7 additions & 0 deletions uc/INTERACTOR.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type interactor struct {
articleValidator ArticleValidator
authHandler AuthHandler
slugger Slugger
commentRW CommentRW
}

// Logger : only used to log stuff
Expand Down Expand Up @@ -42,6 +43,12 @@ type ArticleRW interface {
Delete(slug string) error
}

type CommentRW interface {
Create(comment domain.Comment) (*domain.Comment, error)
GetByID(id int) (*domain.Comment, error)
Delete(id int) error
}

type Slugger interface {
NewSlug(string) string
}
Expand Down
30 changes: 30 additions & 0 deletions uc/articlesRecent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package uc_test

import (
"testing"

"github.com/err0r500/go-realworld-clean/domain"
mock "github.com/err0r500/go-realworld-clean/implem/mock.uc"
"github.com/err0r500/go-realworld-clean/uc"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)

func TestInteractor_GetArticles(t *testing.T) {
expectedArticles := domain.ArticleCollection{art1, art2, art3, art4}

t.Run("most obvious", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

offset := 2
filters := uc.NewFilters("jane", "", "")
i := mock.NewMockedInteractor(mockCtrl)
i.ArticleRW.EXPECT().GetRecentFiltered(filters).Return(expectedArticles, nil).Times(1)

articles, count, err := i.GetUCHandler().GetArticles(10, offset, filters)
assert.NoError(t, err)
assert.Equal(t, 4, count)
assert.Equal(t, expectedArticles[offset:], articles)
})
}
64 changes: 60 additions & 4 deletions uc/comments.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,71 @@
package uc

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

func (i interactor) CommentsGet(slug string) ([]domain.Comment, error) {
return nil, nil
article, err := i.articleRW.GetBySlug(slug)
if err != nil {
return nil, err
}

return article.Comments, nil
}

func (i interactor) CommentsPost(username, slug, comment string) (*domain.Comment, error) {
return nil, nil
commentPoster, err := i.userRW.GetByName(username)
if err != nil {
return nil, err
}

article, err := i.articleRW.GetBySlug(slug)
if err != nil {
return nil, err
}

rawComment := domain.Comment{
Body: comment,
Author: domain.Profile{User: *commentPoster},
}

insertedComment, err := i.commentRW.Create(rawComment)
if err != nil {
return nil, err
}

article.Comments = append(article.Comments, *insertedComment)

if _, err := i.articleRW.Save(*article); err != nil {
return nil, err
}

return insertedComment, nil
}

func (i interactor) CommentsDelete(username string, slug, id string) error {
func (i interactor) CommentsDelete(username, slug string, id int) error {
comment, err := i.commentRW.GetByID(id)
if err != nil {
return err
}
if comment.Author.Name != username {
return errWrongUser
}

if err := i.commentRW.Delete(id); err != nil {
return err
}

article, err := i.articleRW.GetBySlug(slug)
if err != nil {
return err
}

article.UpdateComments(*comment, false)

if _, err := i.articleRW.Save(*article); err != nil {
return err
}

return nil
}

0 comments on commit 4e4bb4c

Please sign in to comment.