Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Pagination #44 #50

Merged
merged 3 commits into from
Mar 16, 2024
Merged

Add Pagination #44 #50

merged 3 commits into from
Mar 16, 2024

Conversation

ali-assar
Copy link
Contributor

Here is the things I've done for pagination:

I edited the database query to support pagination
Then I saw home handler use GetPosts so i modified that handler in app.go
and at the end I Changed SetupRoutes in app.go

@matheusgomes28
Copy link
Owner

Thanks @ali-assar I'll take a look after work today! Note that the pipeline is failing, think you need to run gofmt on all files

@matheusgomes28 matheusgomes28 changed the title resolve #44 Add Pagination #44 Mar 12, 2024
Copy link
Owner

@matheusgomes28 matheusgomes28 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice changes, however they don't completely work when I tried out. I've left some comments 😄

Were you able to run the project btw @ali-assar ? Let me know if you had issues running, I could do with feedback on instructions

app/app.go Outdated
@@ -29,6 +30,11 @@ func SetupRoutes(app_settings common.AppSettings, database database.Database) *g
// DO not cache as it needs to handlenew form values
r.POST("/contact-send", makeContactFormHandler())

// this will setup the route for pagination
r.GET("/page/:num", func(c *gin.Context) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add this endpoint as a cache-able one here. You can do something simlar to that's above in this file:

addCachableHandler(r, "GET", "/page/:num", homeHandler, &cache, app_settings, database)

Also, calling the homeHandler(c, app_settings, database) here won't actually serve the HTML contents, because this function will only return the HTML buffer. Adding it with the cache wrapper will take care of that for you.

app/app.go Outdated
@@ -75,7 +81,16 @@ func addCachableHandler(e *gin.Engine, method string, endpoint string, generator
// / This function will act as the handler for
// / the home page
func homeHandler(c *gin.Context, settings common.AppSettings, db database.Database) ([]byte, error) {
posts, err := db.GetPosts()
pageNumQuery := c.Param("num")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a few issues with these additions. Firstly, this is user data, so this can be a negative number or empty, hence the Atoi(pageNumQuery) will fail.

I would personally do something like

pageNum := 0
if pageNumQuery := c.Param("num"); pageNumQuery != "" {
num, err := strconv.Atoi(pageNumQuery)
if err == nil {
	pageNum = num
} else {
	log.Error().Msgf("invalid page number: %d", num)
}
}

app/app.go Outdated
}

limit := 10 // or whatever limit you want
offset := (pageNum - 1) * limit
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If pageNum is 0, then this is not gonna be a nice offset! Try something like taking the max of this and zero.

app/app.go Outdated
posts, err := db.GetPosts()
pageNumQuery := c.Param("num")
pageNum, err := strconv.Atoi(pageNumQuery)
if err != nil {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is also handling the / page (home), then this value is potentially empty and the error will be thrown. So only logging and proceeding with a nice default is probably nicer. See the comment above.

@@ -11,7 +11,7 @@ import (
)

type Database interface {
GetPosts() ([]common.Post, error)
GetPosts(int, int) ([]common.Post, error)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You changed the interface, don't forget to change the tests too! See the cmd/urchin/index_test.go

@matheusgomes28
Copy link
Owner

Also, don't forget to run gofmt -w . from the project root to go format everything before pushing.

@matheusgomes28
Copy link
Owner

@ali-assar please don't reopen another PR, just use this one. I can't accept multiple PRs!

@ali-assar ali-assar reopened this Mar 13, 2024
@ali-assar
Copy link
Contributor Author

thanks for your help I have implemented your wanted changes. Thanks for your help. Now all the test would be working fine.

Copy link

codecov bot commented Mar 13, 2024

Codecov Report

Attention: Patch coverage is 66.66667% with 7 lines in your changes are missing coverage. Please review.

Project coverage is 24.96%. Comparing base (79e6602) to head (74da431).

Files Patch % Lines
app/app.go 75.00% 2 Missing and 2 partials ⚠️
database/database.go 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #50      +/-   ##
==========================================
+ Coverage   23.83%   24.96%   +1.12%     
==========================================
  Files          14       14              
  Lines         646      661      +15     
==========================================
+ Hits          154      165      +11     
- Misses        475      477       +2     
- Partials       17       19       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Owner

@matheusgomes28 matheusgomes28 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, was quite busy and could only review this tonight. Minor comments but looking much better! I'll test again once you have pushed changes and I'll merge it

app/app.go Outdated
return []byte{}, err
}

return html_buffer.Bytes(), nil
}

func max(a, b int) int {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already a function in the standard called max(...) examples here

app/app.go Outdated
pageNum = max(pageNum, 1)

limit := 10 // or whatever limit you want
offset := (pageNum - 1) * limit
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just put the max here, remove the pageNum = max(pageNum, 1) above.

offset := max((pageNum - 1) * limit, 0)

@ali-assar
Copy link
Contributor Author

I see your point so I changed the code as requested.
Please let me know if you need any other changes.

Copy link
Owner

@matheusgomes28 matheusgomes28 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good my guy! Thanks for the changes 😄

@matheusgomes28 matheusgomes28 merged commit 9efb940 into matheusgomes28:main Mar 16, 2024
5 checks passed
@ali-assar
Copy link
Contributor Author

Thanks for your help I've learn a lot. also i want to use this opportunity and in advance wish you a happy Nowruz which is the Persian New Year. Nowruz marks the beginning of spring and symbolizes renewal, hope, and new beginnings. 🌸🌼

@matheusgomes28
Copy link
Owner

Thank you for the wishes @ali-assar! Happy Nowruz to you too, my friend. This is the nicest Github message I've received, so thank you!

I hope to see more contributions from you here at Urchin, been a pleasure reviewing your code 🤝

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants