123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package controller;
-
- import com.github.cliftonlabs.json_simple.JsonObject;
- import model.questionnaire.Answer;
- import model.questionnaire.Question;
- import model.questionnaire.Session;
- import java.util.UUID;
-
- import static api.JsonKeys.KEY_ANSWER;
- import static api.JsonKeys.KEY_QUESTION;
-
- public class SessionController extends ModelController {
-
- public JsonObject get(JsonObject iJsonObject) {
- return new Session().asJsonObject();
- }
-
- public JsonObject getEntriesForId(String iId, JsonObject iBody) {
- Session vSession = Session.getSessionForUUID(UUID.fromString(iId));
- return vSession.asJsonObject();
- }
-
- /**
- * For a specific question in a session, sets the given answer according to data in the json request body
- * @param iID the session UUID
- * @param iBody JSON Body containing tag 'question' and 'answer' with their respective UUIDs
- * @return an empty JsonObject
- * @throws Exception if the session, question or answer can't be found
- */
- public JsonObject updateEntry(String iID, JsonObject iBody) throws Exception{
- Session vSession = Session.getSessionForUUID(UUID.fromString(iID));
- if(null == vSession)
- throw new Exception(String.format("No session for UUID %s", iID));
-
- String vQuestionId = iBody.getString(KEY_QUESTION);
- String vAnswerId = iBody.getString(KEY_ANSWER);
- if(null == vQuestionId || null == vAnswerId )
- throw new Exception(String.format("Tags '%s' & '%s' expected in request body",KEY_QUESTION.getKey(), KEY_ANSWER.getKey()));
- Question vQuestion = Question.getForUUID(UUID.fromString(vQuestionId));
- if(null == vQuestion)
- throw new Exception(String.format("No question for UUID %s",vQuestionId));
- Answer vAnswer = vQuestion.getAnswerForUUID(UUID.fromString(vAnswerId));
- if(null == vAnswer)
- throw new Exception(String.format("No answer for UUID %s", vAnswerId));
-
- vSession.getSessionAnswers().setAnswer(vQuestion, vAnswer);
- System.out.println(vSession.asJsonObject().toString());
- return new JsonObject();
- }
-
- }
|