I have a table called “UserLevel” where the levels created by the users in my app are stored. I want to be able to fetch the 100 most recent or the 100 highest rated (using the “Rating” column) levels or the 100 most recent levels for a specific user. I have read the documentation but I can’t figure out whether I should use API event handlers och API service for this.
I tried to build an API service following your shopping cart example, but I could not figure out e.g. how to read all items (and persist after possible update) from the table from the Java code.
I also looked at API event handlers using the “find” event to get the entire collection of UserLevel objects, but I do not want to add a generic afterFind trigger since there are other cases where I just want to get the entire collection without any event handling.
Your docs are great, but can you point me to some working examples where I can see how to manipulate the object under “// add your custom logic here”?
If you want to retrieve some data from a table you definitely should use PersistenceAPI. The purpose of the event handler is to decorate some action.
The Advanced Object Retrieval that you linked to only provides a very limited way of retrieving results as compared to some business logic code where I could do practically anything. Assuming that event handlers are not fit for these purposes, could you please answer my original question in more detail:
I tried to build an API service following your shopping cart example,
but I could not figure out e.g. how to read all items (and persist
after possible update) from the table from the Java code. Your
docs are great, but can you point me to some working examples where I
can see how to manipulate the object under “// add your custom logic
Shopping cart service is not about event handlers. This is the way to aggregate your business logic.
If you want to retrieve top 100 records by some criteria you should use find combined with sortBy parameter. Here you can find some examples in Java code. So using sortBy in query you can sort data by created field (or Rating). You can also use where clause to specify user, whose records you want to retrieve.
If you want to save data, you can use corresponding methods from the API.
I see several questions in your post:
- How to retrieve all of the objects? (more than 100 I presume, which implies using paging)
- How to retrieve all of the objects which satisfy a specific criteria (most recent, by rating, etc)?
- How to get the above done from custom business logic?
Did I get it right? If not, please narrow down the scope of the support inquiry so we can focus on a specific one.