diff --git a/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java b/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java index f02a564..fb6cde8 100644 --- a/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java +++ b/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java @@ -8,50 +8,62 @@ import com.opinionowl.opinionowl.repos.SurveyRepository; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.core.type.TypeReference; import java.io.BufferedReader; import java.io.IOException; import java.util.*; +/** + * Version 1 of the API layer for Opinion Owl + */ @RestController @RequestMapping("/api/v1") public class APIController { - // @Autowired - // SurveyRepository sRepo; @Autowired SurveyRepository surveyRepo; + /** + *

Api call to handle the survey answers by a user.

+ *
+ * Api route: api/v1/postSurveyResponses + * @param response HttpServletResponse server side response + * @throws IOException + */ @PostMapping("/postSurveyResponses") public void postSurveyResponses(HttpServletResponse response) throws IOException { - // change Integer to a list of Survey Entities - // handle save of data + // handle save of survey data // redirect to home response.sendRedirect("/"); } - @GetMapping("/getSurveyData") - public Survey getSurveyData(@RequestParam(value = "surveyId") Long surveyId) throws IOException { - Optional surveyO = surveyRepo.findById(surveyId); - if (surveyO.isPresent()) { - Survey survey = surveyO.get(); - System.out.println("Survey found:\n"); - System.out.println(survey); - return survey; - } else { - System.out.println("ERROR: Survey could not be found"); - System.exit(1); - } - return null; - } - + /** + *

API Call to post a generated survey by the user. A survey generated JSON is required from the client

+ *
+ * Example of a JSON: + *
+     * json = {
+     *     title: "title",
+     *     textQuestions: ["question 1", "question 2"],
+     *     radioQuestions: {
+     *         "question 1": ["radio 1", "radio 2"],
+     *         "question 2": ["radio 1", "radio 2", "radio 3"]
+     *     },
+     *     numericRanges: {
+     *         "question 1": [1, 11],
+     *         "question 2": [1, 5]
+     *     }
+     * }
+     * 
+ * @param request HttpServletRequest request from the client + * @return 200 if api was a success + * @throws IOException + */ @PostMapping("/createSurvey") - public int createSurvey(HttpServletRequest request, HttpServletResponse response) throws IOException { + public int createSurvey(HttpServletRequest request) throws IOException { System.out.println("createSurvey() API"); // read the json sent by the client BufferedReader reader = request.getReader(); diff --git a/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java b/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java index 588b137..70b641e 100644 --- a/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java +++ b/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java @@ -7,18 +7,24 @@ import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; - -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Optional; +/** + * Route controller for Opinion Owl pages + */ @Controller public class PageController { @Autowired SurveyRepository surveyRepo; + /** + *

Home route that gets all the surveys in the database, sends it to the model and directs the user to the home page

+ * @param model Model, the client Model + * @return String, the html template + */ @GetMapping("/") public String getHomePage(Model model) { List surveys = surveyRepo.findAll(); @@ -26,18 +32,35 @@ public String getHomePage(Model model) { return "index"; } + /** + *

Route for the create survey page

+ * @param model Model, the client Model + * @return String ,the html template + */ @GetMapping("/createSurvey") public String getCreateSurveyPage(Model model) { return "createSurvey"; } + /** + *

Route to direct the client to the answer survey page, given a survey id to pass the Survey object to the Model

+ *
+ * Example call: /answerSurvey?surveyId=1 + * @param surveyId Long, the ID associated with a survey + * @param model Model, the client Model + * @return String, the html template + */ @GetMapping("/answerSurvey") public String getAnswerSurveyPage(@RequestParam(value = "surveyId") Long surveyId, Model model) { + // find the survey by id Optional surveyO = surveyRepo.findById(surveyId); if (surveyO.isPresent()) { + // was able to obtain a survey from the database by id, and grab it from the Optional Object Survey survey = surveyO.get(); System.out.println("Survey found:"); System.out.println(survey); + // cast the order of the questions to the associtate subclass they belong to + // Cast in hashmaps as List q = survey.getQuestions(); HashMap longAnswerQuestions = new HashMap<>(); HashMap radioChoiceQuestions = new HashMap<>(); @@ -55,6 +78,7 @@ public String getAnswerSurveyPage(@RequestParam(value = "surveyId") Long surveyI rangeQuestionQuestions.put(questionNumber, (RangeQuestion) question); } } + // send the Model the data necessary for the page model.addAttribute("surveyId", survey.getId()); model.addAttribute("surveyTitle", title); model.addAttribute("numberOfQuestions", numQuestions); @@ -62,6 +86,8 @@ public String getAnswerSurveyPage(@RequestParam(value = "surveyId") Long surveyI model.addAttribute("radioChoiceQuestions", radioChoiceQuestions); model.addAttribute("rangeQuestionQuestions", rangeQuestionQuestions); } else { + // could not find survey, Error + // TODO: Redirect the user to a Error boundary page, or maybe the home page instead with a Toast message System.out.println("ERROR: Survey could not be found"); System.exit(1); }