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

Support for Pagination in Database Posts #44

Closed
matheusgomes28 opened this issue Mar 10, 2024 · 4 comments
Closed

Support for Pagination in Database Posts #44

matheusgomes28 opened this issue Mar 10, 2024 · 4 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@matheusgomes28
Copy link
Owner

Add support for pagination in posts returned from db.GetPosts(...). This way, we can make the number of displayed posts in the index page configurable.

This can be achieved quite easily by adding LIMIT and OFFSET to the query, but we would need to capture the current offset in perhaps a GET url parameter.

@matheusgomes28 matheusgomes28 added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed labels Mar 10, 2024
@ali-assar
Copy link
Contributor

ali-assar commented Mar 11, 2024

Hi. I understood the issue and Here’s a proposed solution:

  • Modify the GetPosts function in the database package to take two additional parameters like this:
    func (db SqlDatabase) GetPosts(limit int, offset int) (all_posts []common.Post, err error) { ...}
  • Then in the function that handles requests to the page where posts are displayed, you can use the http.Request object to get the values of limit and offset from the URL parameters.

So I think, I can handle it.

@matheusgomes28
Copy link
Owner Author

@ali-assar Sounds like a solid plan mate!

That's along the lines of what I was thinking about. Feel free to send the PR my way whenever you have time to complete it, happy to review it with you 😆

@matheusgomes28
Copy link
Owner Author

I think that usually, websites have like a URL parameter to indicate which page it is, then you can deduce what limit and offset value you want to serve.

for more info on this for gin, take a look at the query parameters documentation.

To add some stuff to your plan:

  • Add a new handler for something like /page/:num request
  • Perhaps refactor the current handler that's currently handling / so you can reuse in the other page/:num handlers. If you think about it, the index is just /page/0 or something.

downside is that there is a cache on top of the / handler, but let me know if you are struggling because I can come in and help. For now, if you just want to add a separate handler for /page/:num, that would be great

@matheusgomes28
Copy link
Owner Author

Closed with #50

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants