You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SessionController.java 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package controller;
  2. import com.github.cliftonlabs.json_simple.JsonObject;
  3. import model.questionnaire.Answer;
  4. import model.questionnaire.Question;
  5. import model.questionnaire.Session;
  6. import java.util.UUID;
  7. import static api.JsonKeys.KEY_ANSWER;
  8. import static api.JsonKeys.KEY_QUESTION;
  9. public class SessionController extends ModelController {
  10. public JsonObject get(JsonObject iJsonObject) {
  11. return new Session().asJsonObject();
  12. }
  13. public JsonObject getEntriesForId(String iId, JsonObject iBody) {
  14. Session vSession = Session.getSessionForUUID(UUID.fromString(iId));
  15. return vSession.asJsonObject();
  16. }
  17. /**
  18. * For a specific question in a session, sets the given answer according to data in the json request body
  19. * @param iID the session UUID
  20. * @param iBody JSON Body containing tag 'question' and 'answer' with their respective UUIDs
  21. * @return an empty JsonObject
  22. * @throws Exception if the session, question or answer can't be found
  23. */
  24. public JsonObject updateEntry(String iID, JsonObject iBody) throws Exception{
  25. Session vSession = Session.getSessionForUUID(UUID.fromString(iID));
  26. if(null == vSession)
  27. throw new Exception(String.format("No session for UUID %s", iID));
  28. String vQuestionId = iBody.getString(KEY_QUESTION);
  29. String vAnswerId = iBody.getString(KEY_ANSWER);
  30. if(null == vQuestionId || null == vAnswerId )
  31. throw new Exception(String.format("Tags '%s' & '%s' expected in request body",KEY_QUESTION.getKey(), KEY_ANSWER.getKey()));
  32. Question vQuestion = Question.getForUUID(UUID.fromString(vQuestionId));
  33. if(null == vQuestion)
  34. throw new Exception(String.format("No question for UUID %s",vQuestionId));
  35. Answer vAnswer = vQuestion.getAnswerForUUID(UUID.fromString(vAnswerId));
  36. if(null == vAnswer)
  37. throw new Exception(String.format("No answer for UUID %s", vAnswerId));
  38. vSession.getSessionAnswers().setAnswer(vQuestion, vAnswer);
  39. System.out.println(vSession.asJsonObject().toString());
  40. return new JsonObject();
  41. }
  42. }