Skip to content

Commit

Permalink
fix: better pagination (#2)
Browse files Browse the repository at this point in the history
* remove checks

* remove pointless assertion

* sort newest-first by default

* modifications

* fix pagination

* rm newline

* spacing/inlining
  • Loading branch information
leohhhn committed Mar 15, 2024
1 parent ec434eb commit 33f05bc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 36 deletions.
30 changes: 16 additions & 14 deletions api/p/memeland/memeland.gno
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func (m *Memeland) PostMeme(data string, timestamp int64) string {
}

m.Posts = append(m.Posts, newPost)

return id
}

Expand All @@ -60,18 +59,13 @@ func (m *Memeland) Upvote(id string) string {
panic("post with specified ID does not exist")
}

p, ok := post.(*Post)
if !ok {
panic("error casting post")
}

caller := std.PrevRealm().Addr().String()

if _, exists := p.UpvoteTracker.Get(caller); exists {
if _, exists := post.UpvoteTracker.Get(caller); exists {
panic("user has already upvoted this post")
}

p.UpvoteTracker.Set(caller, struct{}{})
post.UpvoteTracker.Set(caller, struct{}{})

return "upvote successful"
}
Expand Down Expand Up @@ -103,25 +97,26 @@ func (m *Memeland) GetPostsInRange(startTimestamp, endTimestamp int64, page, pag
case "UPVOTES":
sort.Sort(UpvoteSorter(filteredPosts))
default:
break
// Sort by timestamp, beginning with newest
filteredPosts = reversePosts(filteredPosts)
}

// Pagination
startIndex := (page - 1) * pageSize
endIndex := startIndex + pageSize

// If page does not contain any posts
if startIndex >= len(filteredPosts) {
panic("page size too large")
return "[]"
}

// If page contains fewer posts than the page size
if endIndex > len(filteredPosts) {
endIndex = len(filteredPosts)
}

paginatedPosts := filteredPosts[startIndex:endIndex]

// Return JSON representation of paginated and sorted posts
return PostsToJSONString(paginatedPosts)
return PostsToJSONString(filteredPosts[startIndex:endIndex])
}

// PostsToJSONString converts a slice of Post structs into a JSON string representation.
Expand All @@ -136,8 +131,8 @@ func PostsToJSONString(posts []*Post) string {

sb.WriteString(PostToJSONString(post))
}

sb.WriteString("]")

return sb.String()
}

Expand Down Expand Up @@ -170,6 +165,13 @@ func (m *Memeland) getPost(id string) *Post {
return nil
}

func reversePosts(posts []*Post) []*Post {
for i, j := 0, len(posts)-1; i < j; i, j = i+1, j-1 {
posts[i], posts[j] = posts[j], posts[i]
}
return posts
}

func (a UpvoteSorter) Len() int { return len(a) }
func (a UpvoteSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a UpvoteSorter) Less(i, j int) bool {
Expand Down
14 changes: 7 additions & 7 deletions api/p/memeland/memeland_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ func TestGetPostsInRangePagination(t *testing.T) {
pageSize int
expectedNumOfPosts int
}{
{page: 1, pageSize: 1, expectedNumOfPosts: 1}, // one per page
{page: 2, pageSize: 1, expectedNumOfPosts: 1}, // one on second page
{page: 1, pageSize: numOfPosts, expectedNumOfPosts: numOfPosts}, // all posts on single page
//{page: 10, pageSize: 10, expectedNumOfPosts: 0}, // todo fix
{page: 5, pageSize: numOfPosts / 5, expectedNumOfPosts: 2}, // evenly distribute posts per page
{page: 1, pageSize: 1, expectedNumOfPosts: 1}, // one per page
{page: 2, pageSize: 1, expectedNumOfPosts: 1}, // one on second page
{page: 1, pageSize: numOfPosts, expectedNumOfPosts: numOfPosts}, // all posts on single page
{page: 12, pageSize: 1, expectedNumOfPosts: 0}, // empty page
{page: 1, pageSize: numOfPosts + 1, expectedNumOfPosts: numOfPosts}, // page with fewer posts than its size
{page: 5, pageSize: numOfPosts / 5, expectedNumOfPosts: 2}, // evenly distribute posts per page
}

for _, tc := range testCases {
Expand All @@ -66,7 +67,7 @@ func TestGetPostsInRangeByTimestamp(t *testing.T) {
m := NewMemeland()
now := time.Now()

numOfPosts := 10
numOfPosts := 5
var memeData []string
for i := 1; i <= numOfPosts; i++ {
// Prep meme data
Expand Down Expand Up @@ -107,7 +108,6 @@ func TestGetPostsInRangeByTimestamp(t *testing.T) {
}

// todo add test to check order

}

func TestGetPostsInRangeByUpvote(t *testing.T) {
Expand Down
17 changes: 2 additions & 15 deletions api/r/memeland/memeland.gno
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,23 @@ import (
var m = memeland.NewMemeland()

func PostMeme(data string, timestamp int64) string {
if m == nil {
return "memeland does not exist"
}
return m.PostMeme(data, timestamp)
}

func Upvote(id string) string {
if m == nil {
return "memeland does not exist"
}
return m.Upvote(id)
}

func GetPostsInRange(startTimestamp, endTimestamp int64, page, pageSize int, sortBy string) string {
if m == nil {
return "memeland does not exist"
}

return m.GetPostsInRange(startTimestamp, endTimestamp, page, pageSize, sortBy)
}

func Render(path string) string {
numOfMemes := int(m.MemeCounter)
if numOfMemes == 0 {
return "no memes yet :c"
return "No memes posted yet! :/"
}

// Default render is get Posts since year 2000 to now
postsJson := m.GetPostsInRange(952733931, time.Now().Unix(), 1, numOfMemes, "DATE_CREATED")
if postsJson == "[]" {
}
return postsJson
return m.GetPostsInRange(0, time.Now().Unix(), 1, numOfMemes, "DATE_CREATED")
}
Empty file.

0 comments on commit 33f05bc

Please sign in to comment.