Re: ERRest example of non-model method with parameters
Re: ERRest example of non-model method with parameters
- Subject: Re: ERRest example of non-model method with parameters
- From: Pascal Robert <email@hidden>
- Date: Wed, 28 Nov 2012 15:44:04 -0500
Le 2012-11-28 à 11:46, Roger Perryman <email@hidden> a écrit :
>
> On Nov 28, 2012, at 11:07 AM, Pascal Robert wrote:
>
>>
>> Le 2012-11-28 à 10:40, Roger Perryman <email@hidden> a écrit :
>>
>>> Hi All!
>>>
>>> I am still getting my feet wet with ERRest. Does anyone have an example of a REST call to a custom method on an EO that is not a model property and that takes parameters? All of the examples that I have found use model properties and just "automagically" work. The framework extracts the parameters and updates modeled properties. The only example of a non-model property was derivedCurrentTime but it doesn't use any parameters.
>>
>> If the custom method is part of the EO class, you just need to add a ERXKey and add it to the keys for the response or request.
>
> If I understand you, you are saying to add these to my custom EO class:
> public static final String LIST_PRACTICES_KEY = "listPractices";
> public static final ERXKey<com.xeotech.resttest.datamodel.Practice> LIST_PRACTICES = new ERXKey<com.xeotech.resttest.datamodel.Practice>(LIST_PRACTICES_KEY);
>
> I still don't see how to retrieve the parameters to pass to this method. Also, based on a comment below, I assumed this method would live in the controller, not the EO.
Ah, I was thinking it was a variable exposed as a method.
>
>>> For example, given an address (or latitude / longitude coordinates) and a distance, find all Practices within the specified distance from the address. I'm unclear on how to extract the parameters and use them in a custom method.
>>>
>>> Another example: I need to fetch all Practices that a Physician is associated with. I can get the list of Practices and I can get the list of Physicians. What is not clear is how to retrieve the Practices for the Physician. There is a relationship defined between Physicians and Practices. I'm sure it will end up requiring me to tweak my route setup.
>>>
>>> This is from the ERRestRouteExample.
>>>
>>> routeRequestHandler.addRoute(new ERXRoute(Person.ENTITY_NAME, "/Person/{person:Person}", ERXRoute.Method.Get, PersonController.class, "show"));
>>>
>>> Would I change it to
>>>
>>> routeRequestHandler.addRoute(new ERXRoute(Physician.ENTITY_NAME, "/Physician/{physician:Physician}/{practices:Practice}", ERXRoute.Method.Get, PhysicianController.class, "show"));
>>>
>>> It doesn't seem to work. Also, is "show" the proper choice? This is being sent to a client as data for a list and not displayed directly.
>>
>> "show" is ok, as long as you have a "show" or "showAction" in PhysicianController. But for naming purposes, if you want to get a list of practices for a physician, I would call the method "listPractices".
>
> So in the example above, practices is the name of a method inside my controller, not a relationship or attribute defined in the model? I can't find it now, but I seem to recall reading somewhere that if the framework cannot find a method, it check other places. Would the method without the final argument be more appropriate? And since I don't need to specify the Practice, how do I specify no parameters?
No, practices in your example is part of the URL and is a instance of the Practice class, the method it will call, in your example, is the "show" method in your PhysicianController.
For, if the id (primary key) of the physician is 10 and his practice have an id of 15, calling this url:
/Physician/10/15.json
will call the show() method in PhysicianController.class. To get the Physician and Practice objects in the show(), you call:
Physician physician = routeObjectForKey("physician");
Practice practice = routeObjectForKey("practices");
> routeRequestHandler.addRoute(new ERXRoute(Physician.ENTITY_NAME, "/Physician/{physician:Physician}/{listPractices:null}", ERXRoute.Method.Get, PhysicianController.class));
If you wish to have all practices for a given physician, do:
routeRequestHandler.addRoute(new ERXRoute(Physician.ENTITY_NAME, "/Physician/{physician:Physician}/listPractices", ERXRoute.Method.Get, PhysicianController.class, "listPractices"));
And in PhysicianController, add a listPractices method that will use routeObjectForKey to get the physician and do whatever else that you need (fetch, update, etc.)
>>> Is it OK to use addDefaultRoutes and also define additional routes? Or do I need to manually add all the routes if I need to customize any?
>>
>> Yes it is ok.
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Webobjects-dev mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden