From 0f9b694694d33507613276e9318c14adace8224d Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer <paul-christian.volkmer@mni.thm.de> Date: Wed, 24 Oct 2012 08:48:25 +0200 Subject: [PATCH] Refactoring of controller class SessionController All feedback methods are available in class FeedbackController, all question methods are available in class QuestionController. None of the URIs are changed! Added second URI for method QuestionController::getSkillQuestions that matches common URI rules in ARSnova: "/session/{sessionkey}/skillquestions" Both, the old an the new URI can be used equally. --- .../controller/FeedbackController.java | 80 ++++++++++++++ .../controller/QuestionController.java | 102 ++++++++++++++++++ .../arsnova/controller/SessionController.java | 27 ----- 3 files changed, 182 insertions(+), 27 deletions(-) create mode 100644 src/main/java/de/thm/arsnova/controller/FeedbackController.java create mode 100644 src/main/java/de/thm/arsnova/controller/QuestionController.java diff --git a/src/main/java/de/thm/arsnova/controller/FeedbackController.java b/src/main/java/de/thm/arsnova/controller/FeedbackController.java new file mode 100644 index 00000000..9e3a7a34 --- /dev/null +++ b/src/main/java/de/thm/arsnova/controller/FeedbackController.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2012 THM webMedia + * + * This file is part of ARSnova. + * + * ARSnova is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ARSnova is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package de.thm.arsnova.controller; + +import javax.servlet.http.HttpServletResponse; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +import de.thm.arsnova.entities.Feedback; +import de.thm.arsnova.entities.User; +import de.thm.arsnova.services.ISessionService; +import de.thm.arsnova.services.IUserService; +import de.thm.arsnova.socket.ARSnovaSocketIOServer; + +@Controller +public class FeedbackController extends AbstractController { + + public static final Logger logger = LoggerFactory.getLogger(FeedbackController.class); + + @Autowired + ISessionService sessionService; + + @Autowired + IUserService userService; + + @Autowired + ARSnovaSocketIOServer server; + + @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; + } +} diff --git a/src/main/java/de/thm/arsnova/controller/QuestionController.java b/src/main/java/de/thm/arsnova/controller/QuestionController.java new file mode 100644 index 00000000..4e331d2c --- /dev/null +++ b/src/main/java/de/thm/arsnova/controller/QuestionController.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2012 THM webMedia + * + * This file is part of ARSnova. + * + * ARSnova is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ARSnova is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package de.thm.arsnova.controller; + +import java.util.List; + +import javax.servlet.http.HttpServletResponse; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +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 QuestionController extends AbstractController { + + public static final Logger logger = LoggerFactory.getLogger(QuestionController.class); + + @Autowired + ISessionService sessionService; + + @Autowired + IUserService userService; + + @Autowired + ARSnovaSocketIOServer server; + + @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={ + "/getSkillQuestions/{sessionkey}", + "/session/{sessionkey}/skillquestions" + }, + 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/controller/SessionController.java b/src/main/java/de/thm/arsnova/controller/SessionController.java index 16e808de..fa5dae82 100644 --- a/src/main/java/de/thm/arsnova/controller/SessionController.java +++ b/src/main/java/de/thm/arsnova/controller/SessionController.java @@ -38,7 +38,6 @@ import org.springframework.web.bind.annotation.RequestMethod; 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.Session; import de.thm.arsnova.entities.User; import de.thm.arsnova.services.ISessionService; @@ -78,32 +77,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", method=RequestMethod.POST) @ResponseBody public Session postNewSession(@RequestBody Session session, HttpServletResponse response) { -- GitLab