Skip to content

Commit

Permalink
[ONE-225] /articlesで記事をgetAllできる
Browse files Browse the repository at this point in the history
  • Loading branch information
ken109 committed Feb 25, 2022
1 parent 3644de1 commit 6ef33b6
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 8 deletions.
1 change: 1 addition & 0 deletions cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func Execute() {
})

r.Group("articles", nil, func(r *router.Router) {
r.Get("", articleHandler.Search)
r.Get(":id", articleHandler.GetByID)
})
})
Expand Down
35 changes: 35 additions & 0 deletions doc/postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,41 @@
},
"response": []
},
{
"name": "Search",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{base}}/articles?length=10&page=1&keyword=&recent=true",
"host": [
"{{base}}"
],
"path": [
"articles"
],
"query": [
{
"key": "length",
"value": "10"
},
{
"key": "page",
"value": "1"
},
{
"key": "keyword",
"value": ""
},
{
"key": "recent",
"value": "true"
}
]
}
},
"response": []
},
{
"name": "GetByID",
"request": {
Expand Down
2 changes: 2 additions & 0 deletions domain/repository/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type ArticleSearchOption struct {
ExcludeUserIDs []uint
Draft bool

Keyword *string

Recent bool
}

Expand Down
23 changes: 15 additions & 8 deletions infrastructure/persistence/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,40 @@ func (u article) Search(ctx context.Context, paging *util.Paging, option reposit
query := db.
Model(&entity.Article{}).
Preload("User").
Scopes(func(db *gorm.DB) *gorm.DB {
Scopes(func(query *gorm.DB) *gorm.DB {
if len(option.UserIDs) > 0 {
db.Where("user_id IN ?", option.UserIDs)
query.Where("user_id IN ?", option.UserIDs)
}
if len(option.ExcludeUserIDs) > 0 {
db.Where("user_id NOT IN ?", option.ExcludeUserIDs)
query.Where("user_id NOT IN ?", option.ExcludeUserIDs)
}

if !option.Draft {
db.Where("draft = ?", false).
query.Where("draft = ?", false).
Where("published_at <= ?", time.Now())
}

if option.Keyword != nil {
query.Where(
db.Or("title LIKE ?", "%"+*option.Keyword+"%").
Or("body LIKE ?", "%"+*option.Keyword+"%"),
)
}

if option.Recent {
db.Order("created_at desc")
query.Order("published_at desc")
} else {
db.Order("created_at asc")
query.Order("published_at asc")
}
return db
return query
})

count, err := paging.GetCount(query)
if err != nil {
return nil, 0, err
}

err = query.Order("published_at desc").Find(&articles).Error
err = query.Scopes(paging.Query()).Find(&articles).Error
if err != nil {
return nil, 0, err
}
Expand Down
19 changes: 19 additions & 0 deletions interface/handler/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/nimil-jp/gin-utils/util"

"github.com/nimil-jp/gin-utils/context"

"go-gin-ddd/resource/request"
"go-gin-ddd/resource/response"
"go-gin-ddd/usecase"
)

Expand Down Expand Up @@ -37,6 +39,23 @@ func (u Article) Create(ctx context.Context, c *gin.Context) error {
return nil
}

func (u Article) Search(ctx context.Context, c *gin.Context) error {
paging := util.NewPaging(c)

recent, err := boolQuery(c, "recent")
if err != nil {
recent = true
}

articles, count, err := u.articleUseCase.Search(ctx, paging, c.Query("keyword"), recent)
if err != nil {
return err
}

c.JSONP(http.StatusOK, response.NewSearchResponse(articles, count))
return nil
}

func (u Article) GetByID(ctx context.Context, c *gin.Context) error {
id, err := uintParam(c, "id")
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions usecase/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/nimil-jp/gin-utils/context"
"github.com/nimil-jp/gin-utils/errors"
"github.com/nimil-jp/gin-utils/util"

"go-gin-ddd/domain/entity"
"go-gin-ddd/domain/repository"
Expand All @@ -13,6 +14,7 @@ import (

type IArticle interface {
Create(ctx context.Context, req *request.ArticleCreate) (uint, error)
Search(ctx context.Context, paging *util.Paging, keyword string, recent bool) ([]*entity.Article, uint, error)
GetByID(ctx context.Context, id uint) (*entity.Article, error)
Update(ctx context.Context, id uint, req *request.ArticleUpdate) error
Delete(ctx context.Context, id uint) error
Expand All @@ -32,6 +34,15 @@ func (a article) Create(ctx context.Context, req *request.ArticleCreate) (uint,
return a.articleRepo.Create(ctx, entity.NewArticle(ctx, req))
}

func (a article) Search(ctx context.Context, paging *util.Paging, keyword string, recent bool) ([]*entity.Article, uint, error) {
return a.articleRepo.Search(ctx, paging, repository.ArticleSearchOption{
ExcludeUserIDs: []uint{ctx.UID()},
Draft: false,
Keyword: &keyword,
Recent: recent,
})
}

func (a article) GetByID(ctx context.Context, id uint) (*entity.Article, error) {
article, err := a.articleRepo.GetByID(ctx, id)
if err != nil {
Expand Down

0 comments on commit 6ef33b6

Please sign in to comment.