Skip to content

Commit

Permalink
fix 上一页、下一页问题
Browse files Browse the repository at this point in the history
  • Loading branch information
icowan committed Feb 15, 2020
1 parent fea6d7f commit 5ec245b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/pkg/post/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ func (c *service) Get(ctx context.Context, id int64) (rs map[string]interface{},
}

// prev
prev, _ := c.repository.Post().Prev(detail.PushTime)
prev, _ := c.repository.Post().Prev(detail.PushTime, []int64{int64(detail.Action)})
// next
next, _ := c.repository.Post().Next(detail.PushTime)
next, _ := c.repository.Post().Next(detail.PushTime, []int64{int64(detail.Action)})

populars, _ := c.Popular(ctx)
return map[string]interface{}{
Expand Down
10 changes: 6 additions & 4 deletions src/repository/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ type PostRepository interface {
Update(p *types.Post) error
Stars() (res []*types.Post, err error)
Index() (res []*types.Post, err error)
Prev(publishTime *time.Time) (res *types.Post, err error)
Next(publishTime *time.Time) (res *types.Post, err error)
Prev(publishTime *time.Time, action []int64) (res *types.Post, err error)
Next(publishTime *time.Time, action []int64) (res *types.Post, err error)
Count() (total int64, err error)
FindOnce(id int64) (res *types.Post, err error)
Search(keyword string, categoryId int64, offset, pageSize int) (res []*types.Post, count int64, err error)
Expand Down Expand Up @@ -68,17 +68,19 @@ func (c *post) Count() (total int64, err error) {
return
}

func (c *post) Prev(publishTime *time.Time) (res *types.Post, err error) {
func (c *post) Prev(publishTime *time.Time, action []int64) (res *types.Post, err error) {
var p types.Post
err = c.db.Where("push_time < ?", publishTime).
Where("action in (?)", action).
Where("post_status = ?", PostStatusPublish).
Order("push_time desc").Limit(1).First(&p).Error
return &p, err
}

func (c *post) Next(publishTime *time.Time) (res *types.Post, err error) {
func (c *post) Next(publishTime *time.Time, action []int64) (res *types.Post, err error) {
var p types.Post
err = c.db.Where("push_time > ?", publishTime).
Where("action in (?)", action).
Where("post_status = ?", PostStatusPublish).
Order("push_time asc").Limit(1).First(&p).Error
return &p, err
Expand Down

0 comments on commit 5ec245b

Please sign in to comment.