ajaynegi45 / LibraryMan-API

Revolutionize book management with LibraryMan! Easily track stock, borrowers, and due dates, streamlining operations for schools, companies, and libraries worldwide, ensuring efficient and organized book lending.
MIT License
23 stars 21 forks source link

[FEATURE] Implement Pagination and Sorting for APIs #38

Open Guhapriya01 opened 1 day ago

Guhapriya01 commented 1 day ago

Is this feature already requested?

Problem or Missing Functionality

When retrieving large datasets through the API, there is currently no pagination or sorting functionality. This results in inefficiencies when handling large amounts of data and makes it difficult to filter and organize results effectively. Adding pagination and sorting would improve the performance and user experience by allowing more control over how data is fetched and presented.

Feature Description

I propose adding pagination and sorting functionality to the existing APIs. By leveraging Spring Data’s Pageable interface, we can allow users to limit the number of results returned per request and sort the results based on different fields. This enhancement would streamline the process of accessing large datasets and improve overall efficiency.

Screenshots

No response

Would you like to work on this feature?

Yes

Implementation Plan

  1. Adding pagination and sorting parameters to the existing API endpoints.
  2. Modifying the service and repository layers to support pagination and sorting using Spring Data’s Pageable interface.
ajaynegi45 commented 1 day ago
  1. Adding pagination and sorting parameters to the existing

Hi @Guhapriya01,

Thank you for expressing your interest in working on the "Implement Pagination and Sorting for APIs" issue. I'm delighted to inform you that I have assigned this issue to you. Your willingness to contribute to our project is much appreciated. Follow SOLID Design Principles

Feel free to start working, and if you have any questions or need assistance during the process, please don't hesitate to reach out.

Shetu003 commented 23 hours ago

Hello Sir, Can you please assign this issue to me?

ajaynegi45 commented 22 hours ago

Hello Sir, Can you please assign this issue to me?

Hi @Shetu003,

Thank you for expressing your interest in working on the "Implement Pagination and Sorting for APIs" issue. However, this issue is currently being worked on by another contributor, Guhapriya01 . You can explore other issues in our repository or check out our other projects under the GirlScript Summer of Code and Hacktoberfest.

Here are the links to explore: https://bento.me/ajaynegi

Feel free to reach out if you have any questions or need guidance on where to start. We're excited to have your contributions!

Guhapriya01 commented 10 hours ago

Hi @ajaynegi45,

As I work on implementing pagination and sorting for the APIs, I wanted to bring to your attention that we may need to update the existing API documentation to reflect these changes. Would you like me to draft an update for the documentation, or should we discuss how to best proceed?

Additionally, I have just started implementing this feature for the Books API (endpoint: GET /api/books) and wanted to share the current response structure I’m getting. The response is sorted by title by default, and it includes 5 elements per page:

{
    "content": [
        {
            "bookId": 11,
            "title": "Circe",
            "author": "Madeline Miller",
            "isbn": "978-0316388681",
            "publisher": "Little, Brown and Company",
            "publishedYear": 2018,
            "genre": "Fantasy",
            "copiesAvailable": 7
        },
        {
            "bookId": 10,
            "title": "Educated",
            "author": "Tara Westover",
            "isbn": "978-0399590504",
            "publisher": "Random House",
            "publishedYear": 2018,
            "genre": "Memoir",
            "copiesAvailable": 5
        },
        {
            "bookId": 13,
            "title": "The Alchemist",
            "author": "Paulo Coelho",
            "isbn": "978-0062315007",
            "publisher": "HarperOne",
            "publishedYear": 1993,
            "genre": "Adventure",
            "copiesAvailable": 10
        },
        {
            "bookId": 12,
            "title": "The Night Circus",
            "author": "Erin Morgenstern",
            "isbn": "978-0385534635",
            "publisher": "Doubleday",
            "publishedYear": 2011,
            "genre": "Fantasy",
            "copiesAvailable": 2
        },
        {
            "bookId": 7,
            "title": "The Silent Patient",
            "author": "Alex Michaelides",
            "isbn": "978-1250219653",
            "publisher": "Celadon Books",
            "publishedYear": 2019,
            "genre": "Psychological Thriller",
            "copiesAvailable": 4
        }
    ],
    "pageable": {
        "pageNumber": 0,
        "pageSize": 5,
        "sort": {
            "sorted": true,
            "unsorted": false,
            "empty": false
        },
        "offset": 0,
        "paged": true,
        "unpaged": false
    },
    "last": false,
    "totalPages": 2,
    "totalElements": 7,
    "first": true,
    "numberOfElements": 5,
    "size": 5,
    "number": 0,
    "sort": {
        "sorted": true,
        "unsorted": false,
        "empty": false
    },
    "empty": false
}

Please let me know if this response format aligns with your expectations or if there are any changes I should make.

Thank you!

ajaynegi45 commented 9 hours ago

Hi @ajaynegi45,

As I work on implementing pagination and sorting for the APIs, I wanted to bring to your attention that we may need to update the existing API documentation to reflect these changes. Would you like me to draft an update for the documentation, or should we discuss how to best proceed?

Additionally, I have just started implementing this feature for the Books API (endpoint: GET /api/books) and wanted to share the current response structure I’m getting. The response is sorted by title by default, and it includes 5 elements per page:

{
    "content": [
        {
            "bookId": 11,
            "title": "Circe",
            "author": "Madeline Miller",
            "isbn": "978-0316388681",
            "publisher": "Little, Brown and Company",
            "publishedYear": 2018,
            "genre": "Fantasy",
            "copiesAvailable": 7
        },
        {
            "bookId": 10,
            "title": "Educated",
            "author": "Tara Westover",
            "isbn": "978-0399590504",
            "publisher": "Random House",
            "publishedYear": 2018,
            "genre": "Memoir",
            "copiesAvailable": 5
        },
        {
            "bookId": 13,
            "title": "The Alchemist",
            "author": "Paulo Coelho",
            "isbn": "978-0062315007",
            "publisher": "HarperOne",
            "publishedYear": 1993,
            "genre": "Adventure",
            "copiesAvailable": 10
        },
        {
            "bookId": 12,
            "title": "The Night Circus",
            "author": "Erin Morgenstern",
            "isbn": "978-0385534635",
            "publisher": "Doubleday",
            "publishedYear": 2011,
            "genre": "Fantasy",
            "copiesAvailable": 2
        },
        {
            "bookId": 7,
            "title": "The Silent Patient",
            "author": "Alex Michaelides",
            "isbn": "978-1250219653",
            "publisher": "Celadon Books",
            "publishedYear": 2019,
            "genre": "Psychological Thriller",
            "copiesAvailable": 4
        }
    ],
    "pageable": {
        "pageNumber": 0,
        "pageSize": 5,
        "sort": {
            "sorted": true,
            "unsorted": false,
            "empty": false
        },
        "offset": 0,
        "paged": true,
        "unpaged": false
    },
    "last": false,
    "totalPages": 2,
    "totalElements": 7,
    "first": true,
    "numberOfElements": 5,
    "size": 5,
    "number": 0,
    "sort": {
        "sorted": true,
        "unsorted": false,
        "empty": false
    },
    "empty": false
}

Please let me know if this response format aligns with your expectations or if there are any changes I should make.

Thank you!

Hi @Guhapriya01,

Thank you for the update and for bringing up the need to revise the API documentation. It’s a good call—I think updating the documentation to include the new pagination and sorting behavior is essential. Feel free to go ahead and draft the changes, and we can review them together.

As for the response structure, it looks great! Sorting by title and limiting to 5 elements per page works as expected. The structure is clean, and the inclusion of pagination details like totalPages, totalElements, and sort is very helpful. No changes are needed at this point, so please continue with the implementation.

Let me know if you encounter any issues or need further input!

Guhapriya01 commented 8 hours ago

Thanks for the feedback! I’ll continue with the implementation of pagination and sorting and reach out if I need further input.

Guhapriya01 commented 2 hours ago

Hi @ajaynegi45 ,

I wanted to let you know that I have created a pull request to implement the pagination and sorting functionality for the APIs. Next, I will start drafting the documentation to outline the new pagination and sorting parameters.

If you have any feedback or suggestions, please feel free to share!

Thank you!

ajaynegi45 commented 1 hour ago

Hi @ajaynegi45 ,

I wanted to let you know that I have created a pull request to implement the pagination and sorting functionality for the APIs. Next, I will start drafting the documentation to outline the new pagination and sorting parameters.

If you have any feedback or suggestions, please feel free to share!

Thank you!

Hi @Guhapriya01,

Your changes are fantastic and work seamlessly with the project. I've merged them into the project. However, I have a question: When I hit the URL for the first time, I receive the first sorted set of 5 data items. But when I hit the URL again, I still get the same 5 data items. How can I fetch the next set of data after those 5?

Guhapriya01 commented 1 hour ago

Thank you for the feedback!

To fetch the next set of data, you can use the pagination parameters I added. Here’s how:

So, if you want to get the next 5 items after the first request, you would change the page number:

Let me know if you have any questions!

ajaynegi45 commented 52 minutes ago

documentatio

Thank you for clearing up my doubt! Take your time to create a very detailed API documentation. Please include all possible parameters such as page, size, sort, title, sortBy, sortDir, and provide examples for each one individually as well as in combinations (e.g., /api/books?page=0&size=7&...). Also, make sure to highlight any relevant points that developers might need to know when working with the API.

Guhapriya01 commented 39 minutes ago

Sure, @ajaynegi45! I’ll start drafting a comprehensive API documentation, as per your suggestions.

I’ll keep you updated on the progress!