0

I'm using OpenRasta to create a Survey application.

I have a SurveyResource that is accessible at /surveys/{id} and editable at /surveys/{id}/edit

I'd now like to add questions to the survey, as that is the point of a survey, but I'm not sure what the most restful way of doing this is and how to set it up in OR.

I'm thinking I should have a QuestionResource (that has details of the question type, question text, etc) and it should be posted to /surveys/{id}/questions and handled by a question handler, but I can't work out how to configure OR.

I've pushed my project onto github at https://github.com/oharab/OpenSurvey/tree/add_question_to_survey

Can anyone help me?

Ben

oharab
  • 4,405
  • 3
  • 19
  • 15

1 Answers1

1

it depends on the way you want to model your resources. It's perfectly possible that you'd never explicitly provide access to a single question, and would modify the entire survey document, like so:

PUT /surveys/123

<survey>

  <link rel="update" href="/surveys/123" method="PUT"
        type="application/vnd.mycorp.survey+xml" />

  <question id="age">
    <label>How old are you?</label>
    <select>
      <option>0 - 5</option>
      <option>6 - 10</option>
      <option>10 - 13</option>
    </select>
  </question>
</survey>

If you go this route, you could even use HTML, or HTML 5 for your content so it's easy to consume by clients. Now you're just modifying the entire survey document at once.

Alternatively, you might want to separately address each question, giving them an individual URI, which I think is what you're talking about, like so:

GET /survey/123

<survey>
  <link rel="add-question" href="/survey/123/questions"      
        type="application/vnd.mycorp.surveyquestion+xml" method="POST" />

  <question>

    <link rel="delete" href="/questions/123-age" method="DELETE" />
    <link rel="update" href="/questions/123-age" type="application/vnd.mycorp.surveyquestion+xml" method="PUT" />

    <label>How old are you?</label>
    <select>
      <option>0 - 5</option>
      <option>6 - 10</option>
      <option>10 - 13</option>
    </select>
  </question>
</survey>

Neither of these is more RESTful than the other, the difference is only in granularity of call. If you need the granularity of the latter, then configure yourself a separate handler per resource as in

using(OpenRastaConfiguration.Manual)
{
   ResourceSpace.Has.ResourcesOfType<Survey>().AtUri("/survey/{id}").HandledBy<SurveyHandler>();
   ResourceSpace.Has.ResourcesOfType<Question>().AtUri("/questions/{id}").HandleBy<QuestionHandler>();
}
  • Thanks for getting back to me. – oharab Aug 16 '11 at 09:36
  • Thanks for getting back to me. It was more the first option I was going for, the Survey & it's questions will be accessed only through the survey URI when submitting the completed survey, but I need to add the questions to the survey one at a time, altering the SurveyResource. – oharab Aug 16 '11 at 09:47
  • On a side-note, using random rels is not recommended, neither is using rels as verbs (this is reintroducing rpc by the backdoor). The rel attribute is there to describe what the resource pointed to by the current link is relative to this resource. SO i'll also object to using the link@rel for what is forms (having a method). Even if the update link was turned into a proper form, then the type should be an accept. – SerialSeb Aug 16 '11 at 10:23