Skip to content

Commit 3f0b1df

Browse files
committed
fix follower/following pagination
1 parent 7b6ce5c commit 3f0b1df

File tree

5 files changed

+8
-26
lines changed

5 files changed

+8
-26
lines changed

internal/http/server/handlers/v1/likes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func LikesIndex(ctr *container.Container) echo.HandlerFunc {
1313
return func(ctx echo.Context) error {
14-
posts, err := ctr.LikeService.IndexByPostIDWithUser(
14+
posts, err := ctr.LikeService.IndexByPostId(
1515
utils.StringToID(ctx.Param("postId"), 0),
1616
utils.StringToID(ctx.QueryParams().Get("lastId"), ^uint64(0)),
1717
utils.StringToInt(ctx.QueryParams().Get("count"), 100, 10),

internal/services/followship/service.go

+6-12
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,24 @@ func (s *Service) FindByIds(followerID, followeeID uint64) (*models.Followship,
1919
return &followship, errors.Wrapf(r.Error, "followerId: %v, followeeId: %v", followerID, followeeID)
2020
}
2121

22-
func (s *Service) IndexFollowers(userId uint64, lastId uint64, count int) ([]*models.Followship, error) {
22+
func (s *Service) IndexFollowers(userId uint64, lastUserId uint64, count int) ([]*models.Followship, error) {
2323
var followships []*models.Followship
24-
if count > 100 {
25-
count = 100
26-
}
2724
r := s.database.Handler().
2825
Preload("Follower").
2926
Where("followee_id = ?", userId).
30-
Where("id < ? ORDER BY id DESC LIMIT ?", lastId, count).
27+
Where("follower_id < ? ORDER BY follower_id DESC LIMIT ?", lastUserId, count).
3128
Find(&followships)
32-
return followships, errors.Wrapf(r.Error, "userId: %v, lastId: %v, count: %v", userId, lastId, count)
29+
return followships, errors.Wrapf(r.Error, "userId: %v, lastUserId: %v, count: %v", userId, lastUserId, count)
3330
}
3431

35-
func (s *Service) IndexFollowings(userId uint64, lastId uint64, count int) ([]*models.Followship, error) {
32+
func (s *Service) IndexFollowings(userId uint64, lastUserId uint64, count int) ([]*models.Followship, error) {
3633
var followships []*models.Followship
37-
if count > 100 {
38-
count = 100
39-
}
4034
r := s.database.Handler().
4135
Preload("Followee").
4236
Where("follower_id = ?", userId).
43-
Where("id < ? ORDER BY id DESC LIMIT ?", lastId, count).
37+
Where("followee_id < ? ORDER BY followee_id DESC LIMIT ?", lastUserId, count).
4438
Find(&followships)
45-
return followships, errors.Wrapf(r.Error, "userId: %v, lastId: %v, count: %v", userId, lastId, count)
39+
return followships, errors.Wrapf(r.Error, "userId: %v, lastUserId: %v, count: %v", userId, lastUserId, count)
4640
}
4741

4842
func (s *Service) CountFollowers(userId uint64) (int64, error) {

internal/services/like/service.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@ type Service struct {
1111
database *database.Database
1212
}
1313

14-
func (s *Service) IndexByPostIDWithUser(postId uint64, lastId uint64, count int) ([]*models.Like, error) {
14+
func (s *Service) IndexByPostId(postId uint64, lastId uint64, count int) ([]*models.Like, error) {
1515
var likes []*models.Like
16-
if count > 100 {
17-
count = 100
18-
}
1916
r := s.database.Handler().
2017
Preload("User").
2118
Where("post_id = ?", postId).

internal/services/post/service.go

-6
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ func (s *Service) Index(userId uint64, lastId uint64, count int) ([]*models.Post
3434

3535
func (s *Service) Feed(userId uint64, lastId uint64, count int) ([]*models.Post, error) {
3636
var posts []*models.Post
37-
if count > 100 {
38-
count = 100
39-
}
4037
r := s.database.Handler().
4138
Where("id < ? ORDER BY id DESC LIMIT ?", lastId, count).
4239
Preload("Audio").
@@ -61,9 +58,6 @@ func (s *Service) Feed(userId uint64, lastId uint64, count int) ([]*models.Post,
6158

6259
func (s *Service) Search(q string, userId uint64, lastId uint64, count int) ([]*models.Post, error) {
6360
var posts []*models.Post
64-
if count > 100 {
65-
count = 100
66-
}
6761
r := s.database.Handler().
6862
Where("(title LIKE ? OR description LIKE ?)", "%"+q+"%", "%"+q+"%").
6963
Where("id < ? ORDER BY id DESC LIMIT ?", lastId, count).

internal/services/user/service.go

-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ func (s *Service) FindBy(field string, value interface{}) (*models.User, error)
2424

2525
func (s *Service) Search(q string, lastId uint64, count int) ([]*models.User, error) {
2626
var users []*models.User
27-
if count > 100 {
28-
count = 100
29-
}
3027
r := s.database.Handler().
3128
Where("(name LIKE ? OR username LIKE ?)", "%"+q+"%", "%"+q+"%").
3229
Where("id < ? ORDER BY id DESC LIMIT ?", lastId, count).

0 commit comments

Comments
 (0)