Hello Dalia,

I guess that's one of the tradeoffs Tim was referring to.

You can always do something like this:

router.attach("/patients/{patientID}", PatientServerResource.class);

and then:

public class PatientServerResource extends ServerResource {
  @Get
  Patient retrievePatient() {
    String patientID = (String) getRequestAttributes.get("patientID");
    // query db, or whatever you need to get the patient by its id
    Patient patient = PatientDAO.findById(patientID);
    return patient;
  }

  @Put
  Patient createOrUpdatePatient(Patient newPatient) {
    String patientID = (String) getRequestAttributes.get("patientID");
    boolean isCreate = !PatientDAO.exists(patientID);
    Patient patient = newPatient;
    if (isCreate) {
      patient.setId(computeIdForNewPatient(newPatient));
    } // else rest of the data is already set on newPatient
    PatientDAO.persist(patientID, patient);
    return patient;
  }
}

I've coded the above out of the top of my head, cannot assure the
thing will be compilable as is (it should be).

You can also add more methods, for example support for DELETE to
remove a patient, like this:

@Delete
public Patient removePatient(patientID) {
  Patient patient = PatientDAO.findById(patientID);
  PatientDAO.erase(patientID);
  return Patient; // Usually DELETE requests expect no body on the
response, check this...
}

You can also have another resource:

router.attach("/patients", PatientCollectionServerResource.class);

and PatientCollectionServerResource supports POST to create a new
patient (no need to know/compute the ID for the patient in advance),
and you can use PUT on PatientServerResource just for updating a
patient (both approaches are RESTful).

Bottomline, starting thinking about resources, the operations can all
be modelled with POST (create), GET (read), PUT (update and create)
and DELETE (delete). The book Tim pointed you to is very good, let me
also add RESTful Web Services by Leonard Richardson and Sam Ruby (code
examples are not in Java, but that doesn't really matter).

Good luck!

On Tue, Apr 24, 2012 at 5:26 PM, Dalia Sobhy <dalia.mohso...@hotmail.com> wrote:
> Thanks Tim it helps alot, but for any update do I have to make the parameter
> a separate resource??
>
> Is there any alternative?
>
> Another aspect could I make one class that adds and retrieves and updates a
> patient based on patientID.
>
> I made it using three separate resources it's working but I believe that
> this is nonsense. If for instance I have  about 20 methods for
> handling/using patient as retrieve, update, add..etc. Do I have to do 20
> resource classes is that logic???
>
> --
> View this message in context: 
> http://restlet-discuss.1400322.n2.nabble.com/How-to-make-a-URI-which-passes-two-parameters-using-restlet-Restlet-2-1-tp7493109p7497326.html
> Sent from the Restlet Discuss mailing list archive at Nabble.com.
>
> ------------------------------------------------------
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2951522



-- 
Fabián Mandelbaum
IS Engineer

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2951533

Reply via email to