Skip to content

Commit

Permalink
comments and JavaDoc added
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony-Massaad committed Nov 4, 2023
1 parent 5731333 commit eb9df15
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* <p>Api call to handle the survey answers by a user.</p>
* <br />
* <strong>Api route: api/v1/postSurveyResponses</strong>
* @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<Survey> 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;
}

/**
* <p>API Call to post a generated survey by the user. A survey generated JSON is required from the client</p>
* <br />
* <strong>Example of a JSON:</strong>
* <pre>
* 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]
* }
* }
* </pre>
* @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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,60 @@
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;

/**
* <p>Home route that gets all the surveys in the database, sends it to the model and directs the user to the home page</p>
* @param model Model, the client Model
* @return String, the html template
*/
@GetMapping("/")
public String getHomePage(Model model) {
List<Survey> surveys = surveyRepo.findAll();
model.addAttribute("surveys", surveys);
return "index";
}

/**
* <p>Route for the create survey page</p>
* @param model Model, the client Model
* @return String ,the html template
*/
@GetMapping("/createSurvey")
public String getCreateSurveyPage(Model model) {
return "createSurvey";
}

/**
* <p>Route to direct the client to the answer survey page, given a survey id to pass the Survey object to the Model</p>
* <br />
* <strong>Example call: /answerSurvey?surveyId=1</strong>
* @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<Survey> 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 <question#, Question>
List<Question> q = survey.getQuestions();
HashMap<Integer, LongAnswerQuestion> longAnswerQuestions = new HashMap<>();
HashMap<Integer, RadioChoiceQuestion> radioChoiceQuestions = new HashMap<>();
Expand All @@ -55,13 +78,16 @@ 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);
model.addAttribute("longAnswerQuestions", longAnswerQuestions);
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);
}
Expand Down

0 comments on commit eb9df15

Please sign in to comment.