diff --git a/src/main/java/de/thm/arsnova/controller/SessionController.java b/src/main/java/de/thm/arsnova/controller/SessionController.java index 906fe66041bee8c6094dd82a0edfecca1a36cbb5..de3fee32b43f564b443a56077121a7270d8c0a82 100644 --- a/src/main/java/de/thm/arsnova/controller/SessionController.java +++ b/src/main/java/de/thm/arsnova/controller/SessionController.java @@ -39,13 +39,14 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import de.thm.arsnova.entities.Feedback; +import de.thm.arsnova.entities.Question; import de.thm.arsnova.entities.LoggedIn; import de.thm.arsnova.entities.Session; import de.thm.arsnova.entities.User; import de.thm.arsnova.services.ISessionService; import de.thm.arsnova.services.IUserService; import de.thm.arsnova.socket.ARSnovaSocketIOServer; -import de.thm.arsnova.socket.message.Question; + @Controller public class SessionController extends AbstractController { @@ -79,32 +80,6 @@ public class SessionController extends AbstractController { return sessionService.getSession(sessionkey); } - @RequestMapping(value="/session/{sessionkey}/feedback", method=RequestMethod.GET) - @ResponseBody - public Feedback getFeedback(@PathVariable String sessionkey) { - return sessionService.getFeedback(sessionkey); - } - - @RequestMapping(value="/session/{sessionkey}/feedback", method=RequestMethod.POST) - @ResponseBody - public Feedback postFeedback(@PathVariable String sessionkey, @RequestBody int value, HttpServletResponse response) { - User user = userService.getUser(SecurityContextHolder.getContext().getAuthentication()); - if (sessionService.saveFeedback(sessionkey, value, user)) { - Feedback feedback = sessionService.getFeedback(sessionkey); - if (feedback != null) { - // TODO: Broadcast feedback changes via websocket - response.setStatus(HttpStatus.CREATED.value()); - return feedback; - } - - response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); - return null; - } - - response.setStatus(HttpStatus.BAD_REQUEST.value()); - return null; - } - @RequestMapping(value="/session/{sessionkey}/online", method=RequestMethod.POST) @ResponseBody public LoggedIn registerAsOnlineUser(@PathVariable String sessionkey, HttpServletResponse response) { @@ -132,35 +107,7 @@ public class SessionController extends AbstractController { return null; } - @RequestMapping(value="/session/{sessionkey}/question/{questionId}", method=RequestMethod.GET) - @ResponseBody - public Question getQuestion(@PathVariable String sessionkey, @PathVariable String questionId, HttpServletResponse response) { - Question question = sessionService.getQuestion(questionId); - if (question != null && question.getSession().equals(sessionkey)) { - return question; - } - - response.setStatus(HttpStatus.NOT_FOUND.value()); - return null; - } - - @RequestMapping(value="/session/{sessionkey}/question", method=RequestMethod.POST) - @ResponseBody - public void postQuestion(@PathVariable String sessionkey, @RequestBody Question question, HttpServletResponse response) { - if (! sessionkey.equals(question.getSession())) { - response.setStatus(HttpStatus.PRECONDITION_FAILED.value()); - return; - } - - if (sessionService.saveQuestion(question)) { - response.setStatus(HttpStatus.CREATED.value()); - return; - } - - response.setStatus(HttpStatus.BAD_REQUEST.value()); - return; - } - + @RequestMapping(value="/socketurl", method=RequestMethod.GET) @ResponseBody public String getSocketUrl() { @@ -187,16 +134,5 @@ public class SessionController extends AbstractController { } return sessions; } - - @RequestMapping(value="/getSkillQuestions/{sessionkey}", method=RequestMethod.GET) - @ResponseBody - public List<Question> getSkillQuestions(@PathVariable String sessionkey, @RequestParam(value="sort", required=false) String sort, HttpServletResponse response) { - List<Question> questions = sessionService.getSkillQuestions(sessionkey, sort); - if(questions == null || questions.isEmpty()) { - response.setStatus(HttpStatus.NOT_FOUND.value()); - return null; - } - logger.info(questions.toString()); - return questions; - } + } diff --git a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java index 9609b2ed73016f7d0a4bab9d09bf26636848b4f8..68ea763333062375154ac3d5cc175e91dd2dd5c0 100644 --- a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java +++ b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java @@ -69,8 +69,6 @@ import de.thm.arsnova.exceptions.ForbiddenException; import de.thm.arsnova.exceptions.NotFoundException; import de.thm.arsnova.services.ISessionService; import de.thm.arsnova.services.IUserService; -import de.thm.arsnova.socket.message.PossibleAnswer; -import de.thm.arsnova.socket.message.Question; @Component public class CouchDBDao implements IDatabaseDao { @@ -521,4 +519,45 @@ public class CouchDBDao implements IDatabaseDao { } return null; } + + @Override + public LoggedIn registerAsOnlineUser(User u, Session s) { + try { + View view = new View("logged_in/all"); + view.setKey(URLEncoder.encode("\"" + u.getUsername() + "\"", "UTF-8")); + ViewResults results = this.getDatabase().view(view); + + LoggedIn loggedIn = new LoggedIn(); + if (results.getJSONArray("rows").optJSONObject(0) != null) { + JSONObject json = results.getJSONArray("rows").optJSONObject(0).optJSONObject("value"); + loggedIn = (LoggedIn) JSONObject.toBean(json, LoggedIn.class); + Collection<VisitedSession> visitedSessions = JSONArray.toCollection(json.getJSONArray("visitedSessions"), VisitedSession.class); + loggedIn.setVisitedSessions(new ArrayList<VisitedSession>(visitedSessions)); + } + + loggedIn.setUser(u.getUsername()); + loggedIn.setSessionId(s.get_id()); + loggedIn.addVisitedSession(s); + + JSONObject json = JSONObject.fromObject(loggedIn); + Document doc = new Document(json); + if (doc.getId() == "") { + // If this is a new user without a logged_in document, we have to remove the following + // pre-filled fields. Otherwise, CouchDB will take these empty fields as genuine + // identifiers, and will throw errors afterwards. + doc.remove("_id"); + doc.remove("_rev"); + } + this.getDatabase().saveDocument(doc); + + LoggedIn l = (LoggedIn) JSONObject.toBean(doc.getJSONObject(), LoggedIn.class); + Collection<VisitedSession> visitedSessions = JSONArray.toCollection(doc.getJSONObject().getJSONArray("visitedSessions"), VisitedSession.class); + l.setVisitedSessions(new ArrayList<VisitedSession>(visitedSessions)); + return l; + } catch (UnsupportedEncodingException e) { + return null; + } catch (IOException e) { + return null; + } + } } diff --git a/src/main/java/de/thm/arsnova/services/ISessionService.java b/src/main/java/de/thm/arsnova/services/ISessionService.java index 392c0f36948d07982caf6ad87745fe374650a436..570617ec367441d52d28534927924aa14d2d98b1 100644 --- a/src/main/java/de/thm/arsnova/services/ISessionService.java +++ b/src/main/java/de/thm/arsnova/services/ISessionService.java @@ -47,6 +47,7 @@ public interface ISessionService { public List<Session> getMySessions(String username); public boolean saveQuestion(Question question); public Question getQuestion(String id); + public LoggedIn registerAsOnlineUser(User user, String sessionkey); public List<Question> getSkillQuestions(String sessionkey, String sort); public int getSkillQuestionCount(String sessionkey); } \ No newline at end of file