Le 2011-03-29 à 16:03, Andrew Kinnie a écrit :
Thanks for the tip on using create that way. I didn't know you could do that. In any event, that's the problem. NoteType is basically a lookup table. I would want to fetch an existing noteType based on some unique attribute, such as "typeName" (which ensure is unique in the EO class) I don't want to create a new noteType, I want to create the intervening object (DeviceNoteType) which has a to-one to NoteType and a to-one to Device. AND (here's the tough part - for me at least) I want to only create a new one if one doesn't already exist. So I need to fetch first, and I want to fetch based on the value passed in for the name key
So you will do:
new ERXRoute(NoteType.ENTITY_NAME, "/NoteType/{name:String}", ERXRoute.Method.Get, NoteTypesController.class, "fetchByName");
public WOActionResults fetchByNameAction() {
String typeName = routeObjectForKey("name");
NoteType type = NoteType.fetchNoteType(editingContext(), User.NAME.eq(typeName));
return response(type, yourerxkeyfilter());
}
If fetchByNameAction didn't find an object, ERRest will return the HTTP code 404, so your client can know that the object was not found, and call:
new ERXRoute(NoteType.ENTITY_NAME, "/NoteType/", ERXRoute.Method.Post, NoteTypesController.class, "create"); // If you called addDefaultRoutes for the NoteType entity, that route already exist)
public WOActionResults createAction() {
NoteType type = create(NoteType.ENTITY_NAME, yourerxkeyfilter());
editingContext().saveChanges();
return response(type, yourerxkeyfilter());
}
If you get a response with HTTP code in the 20x range, the object was created and now you can create your DeviceNoteType with a route like this:
new ERXRoute(DeviceNoteType.ENTITY_NAME, "/DeviceNoteType", ERXRoute.Method.Post, DeviceNoteTypesController.class, "create");
When you call POST /cgi-bin/WebObjects/MyApp.woa/ra/DeviceNoteType, you will have to pass the a Device and a NoteType object in JSON as the body of the request.
I feel like I'm writing my WOWODC session in real-time :-P