Skip to content

Commit

Permalink
[ONE-187] /users/:user_id/articlesのエンドポイントで新着順でソートできる
Browse files Browse the repository at this point in the history
  • Loading branch information
ken109 committed Feb 25, 2022
1 parent 04b3967 commit 3644de1
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
6 changes: 5 additions & 1 deletion doc/postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
"method": "GET",
"header": [],
"url": {
"raw": "{{base}}/users/:user_id/articles?length=10&page=1",
"raw": "{{base}}/users/:user_id/articles?length=10&page=1&recent=true",
"host": [
"{{base}}"
],
Expand All @@ -132,6 +132,10 @@
{
"key": "page",
"value": "1"
},
{
"key": "recent",
"value": "true"
}
],
"variable": [
Expand Down
2 changes: 2 additions & 0 deletions domain/repository/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type ArticleSearchOption struct {
UserIDs []uint
ExcludeUserIDs []uint
Draft bool

Recent bool
}

type IArticle interface {
Expand Down
6 changes: 6 additions & 0 deletions infrastructure/persistence/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func (u article) Search(ctx context.Context, paging *util.Paging, option reposit
db.Where("draft = ?", false).
Where("published_at <= ?", time.Now())
}

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

Expand Down
8 changes: 8 additions & 0 deletions interface/handler/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ func uintParam(c *gin.Context, key string) (uint, error) {
}
return uint(id), nil
}

func boolQuery(c *gin.Context, key string) (bool, error) {
value, err := strconv.ParseBool(c.Query(key))
if err != nil {
return false, errors.NewUnexpected(err)
}
return value, nil
}
7 changes: 6 additions & 1 deletion interface/handler/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,12 @@ func (u User) Articles(ctx context.Context, c *gin.Context) error {
return err
}

articles, count, err := u.userUseCase.Articles(ctx, paging, id)
recent, err := boolQuery(c, "recent")
if err != nil {
recent = true
}

articles, count, err := u.userUseCase.Articles(ctx, paging, id, recent)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions usecase/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type IUser interface {
// Timeline のリターンがArticleになっているが、複数コンテンツに対応した場合にはinterface{}型になる
Timeline(ctx context.Context, paging *util.Paging, kinds []TimelineKind) ([]*entity.Article, error)

Articles(ctx context.Context, paging *util.Paging, id uint) ([]*entity.Article, uint, error)
Articles(ctx context.Context, paging *util.Paging, id uint, recent bool) ([]*entity.Article, uint, error)

Following(ctx context.Context, paging *util.Paging, id uint) ([]*entity.User, uint, error)
Followers(ctx context.Context, paging *util.Paging, id uint) ([]*entity.User, uint, error)
Expand Down Expand Up @@ -229,10 +229,11 @@ func (u user) Timeline(ctx context.Context, paging *util.Paging, kinds []Timelin
return articles, nil
}

func (u user) Articles(ctx context.Context, paging *util.Paging, id uint) ([]*entity.Article, uint, error) {
func (u user) Articles(ctx context.Context, paging *util.Paging, id uint, recent bool) ([]*entity.Article, uint, error) {
return u.articleRepo.Search(ctx, paging, repository.ArticleSearchOption{
UserIDs: []uint{id},
Draft: ctx.UID() == id,
Recent: recent,
})
}

Expand Down

0 comments on commit 3644de1

Please sign in to comment.