public JSONObject updateQuestion(JSONRPC2Request req, HttpServletRequest request) throws JSONRPC2Error, Exception { JSONObject jsonUpdateQuestion = new JSONObject(); // get question Map<String, Object> params = req.getNamedParams(); NamedParamsRetriever np = new NamedParamsRetriever(params); Map<String, Object> qParams = np.getMap("question"); NamedParamsRetriever questionNp = new NamedParamsRetriever(qParams); int questionId = questionNp.getInt("id"); String questionBody = questionNp.getString("body"); String questionAnswer = questionNp.getString("answer"); int qOwnerId = questionNp.getInt("ownerId"); User u = getCurrentUser(request); // check if this is the owner of the question if (u.getId() == qOwnerId) { // update question in database Question qUpdated = QuestionController.updateQuestion(questionId, questionBody, questionAnswer); // return result jsonUpdateQuestion.put("question", qUpdated.toJSONObject()); return jsonUpdateQuestion; } else { throw new Exception("no privileges"); } }
public JSONObject addQuestion(JSONRPC2Request req, HttpServletRequest request) throws JSONRPC2Error { // json object for the result JSONObject jsonAddQuestion = new JSONObject(); // get question System.out.println(req.toString()); Map<String, Object> params = req.getNamedParams(); NamedParamsRetriever np = new NamedParamsRetriever(params); Map<String, Object> qParams = np.getMap("question"); NamedParamsRetriever questionNp = new NamedParamsRetriever(qParams); String body = questionNp.getString("body"); String answer = questionNp.getString("answer"); // get user User u = getCurrentUser(request); int userID = (int) u.getId(); System.out.println("current user id" + userID); // return ID of Question if added successfully Question q = QuestionController.addQuestion(body, answer, userID); jsonAddQuestion.put("question", q.toJSONObject()); return jsonAddQuestion; }
public JSONObject removeTestQuestion(JSONRPC2Request req, HttpServletRequest request) throws Exception, SQLException { JSONObject jsonResult = new JSONObject(); // set retrievers Map<String, Object> params = req.getNamedParams(); NamedParamsRetriever np = new NamedParamsRetriever(params); Map<String, Object> tParams = np.getMap("test"); NamedParamsRetriever testNp = new NamedParamsRetriever(tParams); Map<String, Object> qParams = np.getMap("question"); NamedParamsRetriever questNp = new NamedParamsRetriever(qParams); MySQLDAO dao = new MySQLDAO(); Test t = dao.loadTest(testNp.getInt(("id"))); dao = new MySQLDAO(); Question q = dao.loadQuestion(questNp.getInt("id")); User u = getCurrentUser(request); if (u.getId() == t.getOwnerId()) { dao = new MySQLDAO(); t = dao.deleteTestQuestion(t, q); } else { throw new Exception("no privileges : you do not own this test"); } JSONObject jsonTest = t.toJSONObject(); JSONObject jsonQuestion = q.toJSONObject(); jsonResult.put("test", jsonTest); jsonResult.put("question", jsonQuestion); return jsonResult; }
public JSONObject addTestQuestion(JSONRPC2Request req, HttpServletRequest request) throws Exception, SQLException { JSONObject jsonResult = new JSONObject(); // set retrievers Map<String, Object> params = req.getNamedParams(); NamedParamsRetriever np = new NamedParamsRetriever(params); Map<String, Object> tParams = np.getMap("test"); NamedParamsRetriever testNp = new NamedParamsRetriever(tParams); Map<String, Object> qParams = np.getMap("question"); NamedParamsRetriever questNp = new NamedParamsRetriever(qParams); int tid = testNp.getInt(("id")); int qid = questNp.getInt(("id")); // load test and question MySQLDAO dao = new MySQLDAO(); Test t = dao.loadTest(tid); dao = new MySQLDAO(); Question q = dao.loadQuestion(qid); // load current user User u = getCurrentUser(request); if (u.getId() == t.getOwnerId()) { // check for duplicates boolean duplicate = false; for (Question quest : t.getQuestions()) { if (quest.getId() == qid) { duplicate = true; } } if (!duplicate) { dao = new MySQLDAO(); t = dao.saveTestQuestion(t, q); } } else { throw new Exception("no privileges : you do not own this test!"); } // set result JSONObject jsonTest = t.toJSONObject(); JSONObject jsonQuestion = q.toJSONObject(); jsonResult.put("test", jsonTest); jsonResult.put("question", jsonQuestion); return jsonResult; }
public JSONObject loadQuestion(JSONRPC2Request req, HttpServletRequest request) throws Exception { JSONObject jsonResult = new JSONObject(); JSONObject jsonQuestion = new JSONObject(); // set retrievers Map<String, Object> params = req.getNamedParams(); NamedParamsRetriever np = new NamedParamsRetriever(params); Map<String, Object> qParams = np.getMap("question"); NamedParamsRetriever questNp = new NamedParamsRetriever(qParams); int qid = questNp.getInt("id"); // load question MySQLDAO dao = new MySQLDAO(); Question q = dao.loadQuestion(qid); // set result jsonQuestion = q.toJSONObject(); jsonResult.put("question", jsonQuestion); return jsonResult; }