Comments (11)
The interesting part in it is to keep the context, like reference expansion.
from commercetools-jvm-sdk.
I don't know if it is possible, but it would be nice to have something like iterable, hasNext(): boolean, next(): Query
from commercetools-jvm-sdk.
Well, my idea was that the helper should just do the calculation and accept only a PagedQueryResult. So the client has to keep track of the list (cache, session, ...) and use the helper to get something like a product or the prev/next query.
The question is then, if the client has to fetch the next "page", should the helper do the async/sync request or let the client take care of it?
from commercetools-jvm-sdk.
The "models" can't do actual http requests, you need to go through the client.
from commercetools-jvm-sdk.
The client does not see play http related stuff.
from commercetools-jvm-sdk.
For client I mean the shop (play application), not the java-client :)
from commercetools-jvm-sdk.
Yes, this is an important issue, I think I will sit together with Laura to design this.
from commercetools-jvm-sdk.
Cool
from commercetools-jvm-sdk.
@schleichardt I did some research regarding this topic and it's actually simpler then it looks. Let me explain it a bit as it might give you some ideas how to implement this.
So products are a collection of documents and they have some indexes (_id
, createdAt
, ...). If you just query without any sorting criteria, mongo returns documents in a so called natural order which it's not reliable as it might change (e.g.: update operation). Providing a sorting criteria assures that you will always get reliable results.
So far nothing new.
So how to get the previous / next document?
It turns out you just need to query for greater / less value and sort by that value asc / desc (respectively), and then limit the result to 1.
That means getting the prev / next document works if you use sorting and query for the same value, and if the value is unique and hopefully has an index.
// example: get first 5 products sorted by createdAt.asc
db.products.find().sort({createdAt: 1}).limit(5).forEach(function(doc){
var o = {}
o.id = doc._id
o.createdAt = doc.createdAt
print(o)
})
// =>
/* 0 */
{
"id" : JUUID("8c574527-a160-4eec-99d3-7af8b0f99421"),
"createdAt" : "2014-08-03T09:27:02.690Z"
}
/* 1 */
{
"id" : JUUID("98f1006e-6338-4fbb-8683-492c68c7ba5c"),
"createdAt" : "2014-08-03T09:27:02.714Z"
}
/* 2 */
{
"id" : JUUID("1fb4147d-8b5f-4033-839d-3e0011d229fc"),
"createdAt" : "2014-08-03T09:27:02.740Z"
}
/* 3 */
{
"id" : JUUID("8a94097e-c394-46b5-b311-fbb3729bac0e"),
"createdAt" : "2014-08-03T09:27:02.771Z"
}
/* 4 */
{
"id" : JUUID("f7591242-2205-43b2-87ac-bdecf9952caf"),
"createdAt" : "2014-08-03T09:27:02.793Z"
}
// now let's find prev / next of second document
{
"id" : JUUID("98f1006e-6338-4fbb-8683-492c68c7ba5c"),
"createdAt" : "2014-08-03T09:27:02.714Z"
}
// previous document
db.products.find({createdAt: {$lt: "2014-08-03T09:27:02.714Z"}}).sort({createdAt: -1}).limit(1)
// =>
{
"id" : JUUID("8c574527-a160-4eec-99d3-7af8b0f99421"),
"createdAt" : "2014-08-03T09:27:02.690Z"
}
// next document
db.products.find({createdAt: {$gt: "2014-08-03T09:27:02.714Z"}}).sort({createdAt: 1}).limit(1)
// =>
{
"id" : JUUID("1fb4147d-8b5f-4033-839d-3e0011d229fc"),
"createdAt" : "2014-08-03T09:27:02.740Z"
}
This is what I discovered so far. If you have any suggestions or ideas I'll be happy to discuss them with you :)
from commercetools-jvm-sdk.
@lauraluiz do we need that, how should it look like?
from commercetools-jvm-sdk.
I think we need to put a bit more of helpers related to pages. In the end, we have a "Paged..." class. What I use to handle pagination are:
private static long calculateTotalPages(final PagedResult<?> result, final int pageSize) {
final Double totalPages = Math.ceil((float) result.getTotal() / pageSize);
return totalPages.longValue();
}
hasPrev
and hasNext
are the negated aliases from isFirst
and isLast
. We can also put them to make the code more semantically correct.
The next
and prev
versions I wouldn't use in my case because I don't have the previous search request by the time I need them, just a page.
from commercetools-jvm-sdk.
Related Issues (20)
- Custom fields on transactions
- Support ProductSelections endpoint
- Support Token revoke
- Support ProductProjection In Store
- Support Custom Fields on Parcels
- Custom fields on ReturnItem
- Support of OrderPaymentAddedMessage
- Support Custom field for Delivery
- Support HEAD Http method for Order
- Add totalLineItemsQuantity to Cart
- io.sphere.sdk.products.commands.updateactions.SetAssetKey doesn't work as expected HOT 1
- Product Selections can now be extended with Custom Fields
- Expiration times on access and refresh tokens can now be configured.
- Added sorting capability when retrieving Shipping Methods for a Location
- Support Order Search and head method
- Support Product Selection Assignment in Store HOT 1
- JVM SDK Maitainance: authentication mode
- Support: OrderFromCartDraft missing custom field
- Add Product to ProductSelection InvalidJsonInput HOT 10
- Attribute types from a product draft attribute and CTP product type are not the same
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from commercetools-jvm-sdk.