From 69265ebca3884d0ba356c3bb91f3e2d979f84a6b Mon Sep 17 00:00:00 2001 From: Anthony Massaad Date: Thu, 2 Nov 2023 17:46:22 -0400 Subject: [PATCH 01/10] inital commit for templates --- pom.xml | 10 +++++ .../opinionowl/controllers/APIController.java | 44 +++++++++++++++++++ .../controllers/PageController.java | 38 ++++++++++++++++ .../resources/templates/answerSurvey.html | 11 +++++ .../resources/templates/createSurvey.html | 11 +++++ src/main/resources/templates/index.html | 30 +++++++++++++ .../resources/templates/viewResponse.html | 11 +++++ 7 files changed, 155 insertions(+) create mode 100644 src/main/java/com/opinionowl/opinionowl/controllers/APIController.java create mode 100644 src/main/java/com/opinionowl/opinionowl/controllers/PageController.java create mode 100644 src/main/resources/templates/answerSurvey.html create mode 100644 src/main/resources/templates/createSurvey.html create mode 100644 src/main/resources/templates/index.html create mode 100644 src/main/resources/templates/viewResponse.html diff --git a/pom.xml b/pom.xml index ff89545..610266b 100644 --- a/pom.xml +++ b/pom.xml @@ -27,11 +27,21 @@ h2 runtime + org.springframework.boot spring-boot-starter-test test + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-web + diff --git a/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java b/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java new file mode 100644 index 0000000..706e01c --- /dev/null +++ b/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java @@ -0,0 +1,44 @@ +package com.opinionowl.opinionowl.controllers; + +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +@RestController +@RequestMapping("/api/v1") +public class APIController { + + // @Autowired + // SurveyRepository sRepo; + + @GetMapping("/getAllSurveys") + public List getAllSurveys() { + // change Integer to a list of Survey Entities + List surveys = new ArrayList<>(); + surveys.add(1); + surveys.add(2); + return surveys; + } + + @PostMapping("/postSurveyResponses") + public void postSurveyResponses(HttpServletResponse response) throws IOException { + // change Integer to a list of Survey Entities + // handle save of data + // redirect to home + response.sendRedirect("/"); + } + + @PostMapping("/createSurvey") + public void createSurvey(HttpServletResponse response) throws IOException { + // change Integer to a list of Survey Entities + // handle save of data + // redirect to home + response.sendRedirect("/"); + } +} diff --git a/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java b/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java new file mode 100644 index 0000000..8186d58 --- /dev/null +++ b/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java @@ -0,0 +1,38 @@ +package com.opinionowl.opinionowl.controllers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +@Controller +public class PageController { + + // @Autowired + // SurveyRepository sRepo; + + @GetMapping("/") + public String getHomePage(Model model) { + // mocking survey results + return "index"; + } + + @GetMapping("/createSurvey") + public String getCreateSurveyPage(Model model) { + return "createSurvey"; + } + + @GetMapping("/answerSurvey") + public String getAnswerSurveyPage(Model model) { + return "answerSurvey"; + } + + @GetMapping("/viewResponse") + public String getViewResponsePage(Model model) { + return "viewResponse"; + } +} diff --git a/src/main/resources/templates/answerSurvey.html b/src/main/resources/templates/answerSurvey.html new file mode 100644 index 0000000..0c35e4d --- /dev/null +++ b/src/main/resources/templates/answerSurvey.html @@ -0,0 +1,11 @@ + + + + Created AddressBook + + + + +

Answer Survey

+ + \ No newline at end of file diff --git a/src/main/resources/templates/createSurvey.html b/src/main/resources/templates/createSurvey.html new file mode 100644 index 0000000..43a8d1c --- /dev/null +++ b/src/main/resources/templates/createSurvey.html @@ -0,0 +1,11 @@ + + + + Created AddressBook + + + + +

Create Survey

+ + \ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html new file mode 100644 index 0000000..6b92cf9 --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,30 @@ + + + + Created AddressBook + + + + +

Welcome To OpinionOwl

+

+ +
+

Surveys Available

+ +
+ + \ No newline at end of file diff --git a/src/main/resources/templates/viewResponse.html b/src/main/resources/templates/viewResponse.html new file mode 100644 index 0000000..81cf6aa --- /dev/null +++ b/src/main/resources/templates/viewResponse.html @@ -0,0 +1,11 @@ + + + + Created AddressBook + + + + +

View Response

+ + \ No newline at end of file From 1bf353bed3fdca27ac658afc0f443493be36d784 Mon Sep 17 00:00:00 2001 From: anthony-massaad Date: Thu, 2 Nov 2023 22:23:19 -0400 Subject: [PATCH 02/10] added basic templates for all pages. Finish create survey basic form building. --- .../opinionowl/controllers/APIController.java | 30 ++- .../controllers/PageController.java | 4 +- src/main/resources/application.properties | 3 +- src/main/resources/static/images/owl.png | Bin 0 -> 11515 bytes .../resources/templates/answerSurvey.html | 2 +- .../resources/templates/createSurvey.html | 202 +++++++++++++++++- .../resources/templates/fragments/footer.html | 1 + .../resources/templates/fragments/header.html | 6 + src/main/resources/templates/index.html | 53 +++-- 9 files changed, 259 insertions(+), 42 deletions(-) create mode 100644 src/main/resources/static/images/owl.png create mode 100644 src/main/resources/templates/fragments/footer.html create mode 100644 src/main/resources/templates/fragments/header.html diff --git a/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java b/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java index 706e01c..ffeb9aa 100644 --- a/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java +++ b/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java @@ -1,11 +1,14 @@ package com.opinionowl.opinionowl.controllers; +import com.fasterxml.jackson.databind.ObjectMapper; // You might need to import this class +import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.io.BufferedReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -17,15 +20,6 @@ public class APIController { // @Autowired // SurveyRepository sRepo; - @GetMapping("/getAllSurveys") - public List getAllSurveys() { - // change Integer to a list of Survey Entities - List surveys = new ArrayList<>(); - surveys.add(1); - surveys.add(2); - return surveys; - } - @PostMapping("/postSurveyResponses") public void postSurveyResponses(HttpServletResponse response) throws IOException { // change Integer to a list of Survey Entities @@ -35,10 +29,26 @@ public void postSurveyResponses(HttpServletResponse response) throws IOException } @PostMapping("/createSurvey") - public void createSurvey(HttpServletResponse response) throws IOException { + public void createSurvey(HttpServletRequest request, HttpServletResponse response) throws IOException { // change Integer to a list of Survey Entities // handle save of data // redirect to home + // Read the JSON data from the request + System.out.println("Hit create Survey"); + BufferedReader reader = request.getReader(); + System.out.println(reader); + StringBuilder jsonBuilder = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + jsonBuilder.append(line); + } + + String jsonData = jsonBuilder.toString(); + + ObjectMapper objectMapper = new ObjectMapper(); +// SurveyData surveyData = objectMapper.readValue(jsonData, SurveyData.class); + + response.sendRedirect("/"); } } diff --git a/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java b/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java index 8186d58..25c6219 100644 --- a/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java +++ b/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java @@ -4,6 +4,7 @@ import org.springframework.stereotype.Controller; 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; @@ -27,7 +28,8 @@ public String getCreateSurveyPage(Model model) { } @GetMapping("/answerSurvey") - public String getAnswerSurveyPage(Model model) { + public String getAnswerSurveyPage(@RequestParam(value = "surveyId") Long surveyId, Model model) { + model.addAttribute("surveyId", surveyId); return "answerSurvey"; } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..0e621e4 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,2 @@ - +spring.thymeleaf.prefix=classpath:/templates/ +spring.thymeleaf.suffix=.html \ No newline at end of file diff --git a/src/main/resources/static/images/owl.png b/src/main/resources/static/images/owl.png new file mode 100644 index 0000000000000000000000000000000000000000..b2135ad9876e52a3cd2990159967d808d4332650 GIT binary patch literal 11515 zcmXYXWl$Wx*ER0$?oM%sEl%-W+@X}>P$<5O6)gn{l%flZ%OV9zkrtQY4vV`MclU?i z|9xk2Ztgv~=T1Ikl1V1fIzUx?92y)XBqV%wHDx^{BoxpKOt4U27_EV`PcL1Lj+TLn z>PIaPVf6ns3=iS|H9<5VA*BC&A@t{Lq(>Oii+%c^eQ`WRFdsI5K0iMT5+l`_u!YGI zJ^vJVYSMUyQ$2UTe;Nt6Un#kn%Xykk$kC#Cyg7;1Vd>3v|JRo?TOaW|*1Qy=p8j6+ z;$VF-0;OD!@%doLSXO9iWU#TSOp${!?|W>dm(xcxP__Zuemmr9xh8bJ$?0XPgx-2U zeWXa+wz*-wr04M_k5)$u&y0U@+c>ch0!E@mc3EuH$l6q5@jAo&7zoF+r zm*M^Mim%$Bx+|k;ryUYr-)`E{!Jz%;xa>Emg~1Pk@nb$HqX`JnxYxb-pNP_l+1*w_ zahgFZxO5Wv@oXJhg`krX0oVPp)7JVCKcf~p))|lhi`o}{iB!fp65jS==EX2uV-G+~{#q3Y-65f-+aIw02M) z*IqB7YN5xA(vz&t-{UOeuHO5aTMa(7;3>=cRP7#nh4YRlo_xX6inWzOF|ryhXPQ?x z+G2qvpOk*5;|n4mn_U}TDz?9{({N^KZof=&%Z+5jq06j>0OTXC69;1AzZ9u1g>6s> zM0Ps6#Ug)g>~@O}CnGAS@{cB*`xRN*m=wlh6-Y(y|C3&mvM$L(-|^M`L=ahsgsZwL zM7<1YaF~EaRi$mcJ3T@Wm|_ieZ?_1F`|3Gdst|KmqxD7#r>X8M!ELGHtFO8^bG6A- zJZe)mQeR^hO+2b7e)9S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ates/answerSurvey.html b/src/main/resources/templates/answerSurvey.html index 0c35e4d..1714a43 100644 --- a/src/main/resources/templates/answerSurvey.html +++ b/src/main/resources/templates/answerSurvey.html @@ -6,6 +6,6 @@ -

Answer Survey

+

Responding to Survey:

\ No newline at end of file diff --git a/src/main/resources/templates/createSurvey.html b/src/main/resources/templates/createSurvey.html index 43a8d1c..d9067aa 100644 --- a/src/main/resources/templates/createSurvey.html +++ b/src/main/resources/templates/createSurvey.html @@ -1,11 +1,199 @@ - + Created AddressBook - - - -

Create Survey

- - \ No newline at end of file + + + +
+

Create a Survey

+
+ + + +
+ +
+

Form Title

+
+ + +
+ +
+
+
+ + + diff --git a/src/main/resources/templates/fragments/footer.html b/src/main/resources/templates/fragments/footer.html new file mode 100644 index 0000000..28d587d --- /dev/null +++ b/src/main/resources/templates/fragments/footer.html @@ -0,0 +1 @@ +

© 2023 OpinionOwl. All rights reserved.

diff --git a/src/main/resources/templates/fragments/header.html b/src/main/resources/templates/fragments/header.html new file mode 100644 index 0000000..abcdf23 --- /dev/null +++ b/src/main/resources/templates/fragments/header.html @@ -0,0 +1,6 @@ +
+ +
diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index 6b92cf9..34c5201 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -1,30 +1,39 @@ - + Created AddressBook - - - -

Welcome To OpinionOwl

-

- -
-

Surveys Available

- +
+
+ + From 85b9be9ad07eaa180fd19b66b618b350f32e4510 Mon Sep 17 00:00:00 2001 From: Anthony Massaad Date: Fri, 3 Nov 2023 15:56:15 -0400 Subject: [PATCH 03/10] fixed create survey, survey saved to database repo, home page shows all available surveys --- pom.xml | 5 ++ .../opinionowl/controllers/APIController.java | 74 +++++++++++++++---- .../controllers/PageController.java | 21 +++++- .../opinionowl/models/QuestionType.java | 15 +++- .../opinionowl/repos/SurveyRepository.java | 3 + .../resources/templates/answerSurvey.html | 9 ++- .../resources/templates/createSurvey.html | 31 ++++---- src/main/resources/templates/index.html | 18 ++--- 8 files changed, 124 insertions(+), 52 deletions(-) diff --git a/pom.xml b/pom.xml index ea04300..a8c1904 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,11 @@ org.projectlombok lombok + + org.json + json + LATEST + diff --git a/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java b/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java index ffeb9aa..f02a564 100644 --- a/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java +++ b/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java @@ -1,17 +1,22 @@ package com.opinionowl.opinionowl.controllers; import com.fasterxml.jackson.databind.ObjectMapper; // You might need to import this class +import com.opinionowl.opinionowl.models.LongAnswerQuestion; +import com.opinionowl.opinionowl.models.RadioChoiceQuestion; +import com.opinionowl.opinionowl.models.RangeQuestion; +import com.opinionowl.opinionowl.models.Survey; +import com.opinionowl.opinionowl.repos.SurveyRepository; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +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.ArrayList; -import java.util.List; +import java.util.*; @RestController @RequestMapping("/api/v1") @@ -19,6 +24,8 @@ public class APIController { // @Autowired // SurveyRepository sRepo; + @Autowired + SurveyRepository surveyRepo; @PostMapping("/postSurveyResponses") public void postSurveyResponses(HttpServletResponse response) throws IOException { @@ -28,27 +35,62 @@ public void postSurveyResponses(HttpServletResponse response) throws IOException 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; + } + @PostMapping("/createSurvey") - public void createSurvey(HttpServletRequest request, HttpServletResponse response) throws IOException { - // change Integer to a list of Survey Entities - // handle save of data - // redirect to home - // Read the JSON data from the request - System.out.println("Hit create Survey"); + public int createSurvey(HttpServletRequest request, HttpServletResponse response) throws IOException { + System.out.println("createSurvey() API"); + // read the json sent by the client BufferedReader reader = request.getReader(); - System.out.println(reader); + // create a string format of the json from the reader StringBuilder jsonBuilder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { jsonBuilder.append(line); } - String jsonData = jsonBuilder.toString(); + System.out.println("JSONDATA: " + jsonData); + // Parse the JSON data using Jackson ObjectMapper + //create the objects as java objects ObjectMapper objectMapper = new ObjectMapper(); -// SurveyData surveyData = objectMapper.readValue(jsonData, SurveyData.class); + HashMap surveyData = objectMapper.readValue(jsonData, new TypeReference>() {}); + // Extract specific data from the parsed JSON + String title = (String) surveyData.get("title"); + List textQuestions = (List) surveyData.get("textQuestions"); + HashMap> radioQuestions = (HashMap>) surveyData.get("radioQuestions"); + HashMap> numericRanges = (HashMap>) surveyData.get("numericRanges"); + Survey survey = new Survey(title); + // add all the question types to the survey + for (String questionTitle : textQuestions) { + survey.addQuestion(new LongAnswerQuestion(questionTitle, 50)); + } - response.sendRedirect("/"); + for (String questionTitle : radioQuestions.keySet()) { + String[] radioQuestionsArr = new String[radioQuestions.get(questionTitle).size()]; + survey.addQuestion(new RadioChoiceQuestion(questionTitle, radioQuestions.get(questionTitle).toArray(radioQuestionsArr))); + } + + for (String questionTitle : numericRanges.keySet()) { + List ranges = numericRanges.get(questionTitle); + survey.addQuestion(new RangeQuestion(questionTitle, ranges.get(0), ranges.get(1), 1)); + } + surveyRepo.save(survey); + System.out.println("survey generated\n\n" + survey); + return 200; } } diff --git a/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java b/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java index 25c6219..0033929 100644 --- a/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java +++ b/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java @@ -1,5 +1,7 @@ package com.opinionowl.opinionowl.controllers; +import com.opinionowl.opinionowl.models.Survey; +import com.opinionowl.opinionowl.repos.SurveyRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -9,16 +11,18 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Optional; @Controller public class PageController { - // @Autowired - // SurveyRepository sRepo; + @Autowired + SurveyRepository surveyRepo; @GetMapping("/") public String getHomePage(Model model) { - // mocking survey results + List surveys = surveyRepo.findAll(); + model.addAttribute("surveys", surveys); return "index"; } @@ -29,7 +33,16 @@ public String getCreateSurveyPage(Model model) { @GetMapping("/answerSurvey") public String getAnswerSurveyPage(@RequestParam(value = "surveyId") Long surveyId, Model model) { - model.addAttribute("surveyId", surveyId); + Optional surveyO = surveyRepo.findById(surveyId); + if (surveyO.isPresent()) { + Survey survey = surveyO.get(); + System.out.println("Survey found:\n"); + System.out.println(survey); + model.addAttribute("survey", survey); + } else { + System.out.println("ERROR: Survey could not be found"); + System.exit(1); + } return "answerSurvey"; } diff --git a/src/main/java/com/opinionowl/opinionowl/models/QuestionType.java b/src/main/java/com/opinionowl/opinionowl/models/QuestionType.java index b655f43..5b11c9f 100644 --- a/src/main/java/com/opinionowl/opinionowl/models/QuestionType.java +++ b/src/main/java/com/opinionowl/opinionowl/models/QuestionType.java @@ -1,10 +1,19 @@ package com.opinionowl.opinionowl.models; +import lombok.Getter; + /** * An Enum to keep track of the different question types. */ + +@Getter public enum QuestionType { - LONG_ANSWER, - RADIO_CHOICE, - RANGE + LONG_ANSWER("Long Answer"), + RADIO_CHOICE("Radio Choice"), + RANGE("Range"); + + private final String type; + QuestionType(String type) { + this.type = type; + } } diff --git a/src/main/java/com/opinionowl/opinionowl/repos/SurveyRepository.java b/src/main/java/com/opinionowl/opinionowl/repos/SurveyRepository.java index 99c686f..0a39e8f 100644 --- a/src/main/java/com/opinionowl/opinionowl/repos/SurveyRepository.java +++ b/src/main/java/com/opinionowl/opinionowl/repos/SurveyRepository.java @@ -4,6 +4,7 @@ import org.springframework.data.repository.CrudRepository; import java.util.List; +import java.util.Optional; /** * The repository in charge of managing the CRUD operations for Survey Entity. @@ -11,4 +12,6 @@ public interface SurveyRepository extends CrudRepository { // Essentially performs SELECT * FROM survey List findAll(); + + Survey findById(long Id); } diff --git a/src/main/resources/templates/answerSurvey.html b/src/main/resources/templates/answerSurvey.html index 1714a43..f51f9a7 100644 --- a/src/main/resources/templates/answerSurvey.html +++ b/src/main/resources/templates/answerSurvey.html @@ -6,6 +6,13 @@ -

Responding to Survey:

+
+

Survey#:

+
+
+ +
+
+
\ No newline at end of file diff --git a/src/main/resources/templates/createSurvey.html b/src/main/resources/templates/createSurvey.html index d9067aa..fa7d584 100644 --- a/src/main/resources/templates/createSurvey.html +++ b/src/main/resources/templates/createSurvey.html @@ -6,7 +6,7 @@ -
+

Create a Survey

@@ -15,7 +15,7 @@

Create a Survey

-

Form Title

+

Form Title

@@ -23,11 +23,12 @@

Form Title

-
+
diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index 1d223a2..8eef2f2 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -1,7 +1,7 @@ - Created AddressBook + OpinionOwl diff --git a/src/main/resources/templates/viewResponse.html b/src/main/resources/templates/viewResponse.html index 81cf6aa..e26f3ad 100644 --- a/src/main/resources/templates/viewResponse.html +++ b/src/main/resources/templates/viewResponse.html @@ -1,7 +1,7 @@ - Created AddressBook + OpinionOwl | Responses From eb9df155f5a6886f001933e8db53203fe7b34496 Mon Sep 17 00:00:00 2001 From: anthony-massaad Date: Sat, 4 Nov 2023 17:53:41 -0400 Subject: [PATCH 05/10] comments and JavaDoc added --- .../opinionowl/controllers/APIController.java | 56 +++++++++++-------- .../controllers/PageController.java | 30 +++++++++- 2 files changed, 62 insertions(+), 24 deletions(-) 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); } From 056eec98e4342f3d276071cb644fdb21b7d7a825 Mon Sep 17 00:00:00 2001 From: anthony-massaad Date: Sat, 4 Nov 2023 18:01:55 -0400 Subject: [PATCH 06/10] removed dependency in pom --- pom.xml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pom.xml b/pom.xml index a8c1904..ea04300 100644 --- a/pom.xml +++ b/pom.xml @@ -44,11 +44,6 @@ org.projectlombok lombok - - org.json - json - LATEST - From 32db9ba314d68324a3c0354cdceb33505c855a1f Mon Sep 17 00:00:00 2001 From: anthony-massaad Date: Sat, 4 Nov 2023 18:07:36 -0400 Subject: [PATCH 07/10] added no args constructor for the controllers --- .../com/opinionowl/opinionowl/controllers/APIController.java | 2 ++ .../com/opinionowl/opinionowl/controllers/PageController.java | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java b/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java index fb6cde8..19812aa 100644 --- a/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java +++ b/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java @@ -8,6 +8,7 @@ import com.opinionowl.opinionowl.repos.SurveyRepository; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import lombok.NoArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import com.fasterxml.jackson.core.type.TypeReference; @@ -21,6 +22,7 @@ */ @RestController @RequestMapping("/api/v1") +@NoArgsConstructor public class APIController { @Autowired diff --git a/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java b/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java index 70b641e..ca77778 100644 --- a/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java +++ b/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java @@ -2,6 +2,7 @@ import com.opinionowl.opinionowl.models.*; import com.opinionowl.opinionowl.repos.SurveyRepository; +import lombok.NoArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -15,6 +16,7 @@ * Route controller for Opinion Owl pages */ @Controller +@NoArgsConstructor public class PageController { @Autowired From 5fc46e0148ec21c9d586ec23b008be66fc57f7c1 Mon Sep 17 00:00:00 2001 From: anthony-massaad Date: Sat, 4 Nov 2023 18:21:45 -0400 Subject: [PATCH 08/10] merged main to branch for up to date version. Updated code to match recent changes --- .../opinionowl/controllers/APIController.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java b/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java index 19812aa..cb41639 100644 --- a/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java +++ b/src/main/java/com/opinionowl/opinionowl/controllers/APIController.java @@ -1,11 +1,9 @@ package com.opinionowl.opinionowl.controllers; import com.fasterxml.jackson.databind.ObjectMapper; // You might need to import this class -import com.opinionowl.opinionowl.models.LongAnswerQuestion; -import com.opinionowl.opinionowl.models.RadioChoiceQuestion; -import com.opinionowl.opinionowl.models.RangeQuestion; -import com.opinionowl.opinionowl.models.Survey; +import com.opinionowl.opinionowl.models.*; import com.opinionowl.opinionowl.repos.SurveyRepository; +import com.opinionowl.opinionowl.repos.UserRepository; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import lombok.NoArgsConstructor; @@ -28,6 +26,11 @@ public class APIController { @Autowired SurveyRepository surveyRepo; + + @Autowired + private UserRepository userRepository; + + /** *

Api call to handle the survey answers by a user.

*
@@ -88,20 +91,23 @@ public int createSurvey(HttpServletRequest request) throws IOException { HashMap> radioQuestions = (HashMap>) surveyData.get("radioQuestions"); HashMap> numericRanges = (HashMap>) surveyData.get("numericRanges"); - Survey survey = new Survey(title); + AppUser user = new AppUser("username", "password"); + userRepository.save(user); + Survey survey = new Survey(user, title); + user.addSurvey(survey); // add all the question types to the survey for (String questionTitle : textQuestions) { - survey.addQuestion(new LongAnswerQuestion(questionTitle, 50)); + survey.addQuestion(new LongAnswerQuestion(survey, questionTitle, 50)); } for (String questionTitle : radioQuestions.keySet()) { String[] radioQuestionsArr = new String[radioQuestions.get(questionTitle).size()]; - survey.addQuestion(new RadioChoiceQuestion(questionTitle, radioQuestions.get(questionTitle).toArray(radioQuestionsArr))); + survey.addQuestion(new RadioChoiceQuestion(survey, questionTitle, radioQuestions.get(questionTitle).toArray(radioQuestionsArr))); } for (String questionTitle : numericRanges.keySet()) { List ranges = numericRanges.get(questionTitle); - survey.addQuestion(new RangeQuestion(questionTitle, ranges.get(0), ranges.get(1), 1)); + survey.addQuestion(new RangeQuestion(survey, questionTitle, ranges.get(0), ranges.get(1), 1)); } surveyRepo.save(survey); System.out.println("survey generated\n\n" + survey); From ffcaaeba871446688a36b96c1a08604299cfce31 Mon Sep 17 00:00:00 2001 From: anthony-massaad Date: Sat, 4 Nov 2023 20:06:35 -0400 Subject: [PATCH 09/10] addressed PR review. added scripts folder for javascript files --- .../resources/static/scripts/createSurvey.js | 168 ++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 src/main/resources/static/scripts/createSurvey.js diff --git a/src/main/resources/static/scripts/createSurvey.js b/src/main/resources/static/scripts/createSurvey.js new file mode 100644 index 0000000..2ec8be9 --- /dev/null +++ b/src/main/resources/static/scripts/createSurvey.js @@ -0,0 +1,168 @@ +let counter = 0; +let numOfQuestions = 0; + +const formTitle = $(".form-title"); +const survey = $("#survey"); +const addTextQuestion = $("#add-text"); +const addRadioChoice = $("#add-radio-choice"); +const addNumericRange = $("#add-numeric-range"); +const submitButton = $("#create-survey"); +submitButton.attr("disabled", true); +const generateUniqueID = () => { + counter++; + return `unique-${counter}-${Math.floor(Math.random() * 1000)}`; +}; + +/** + * + * @param {string} radioQuestionContainer + * @param {string} uniqueName + */ +const addMoreRadioOptions = (radioQuestionContainer, uniqueName) => { + const uniqueId = generateUniqueID(); + $(radioQuestionContainer).append(` +
+ + + `); +}; + +/** + * + * @param {string} tableRowId + */ +const removeTableRow = (tableRowId) => { + $(tableRowId).remove(); + decrementNumOfQuestion(); +}; + +const incrementNumOfQuestions = () => { + numOfQuestions++; + submitButton.removeAttr('disabled'); +}; + +const decrementNumOfQuestion = () => { + numOfQuestions--; + if (numOfQuestions === 0){ + submitButton.attr("disabled", true); + } +}; + +addTextQuestion.click((e) => { + e.preventDefault(); + const rowId = generateUniqueID(); + const question = ` +
+ + + + `; + survey.append(question); + incrementNumOfQuestions(); +}); + +addRadioChoice.click((e) => { + e.preventDefault(); + const rowId = generateUniqueID(); + const radioQuestionContainer = generateUniqueID(); + const uniqueName = generateUniqueID(); + const question = ` + + + + + `; + survey.append(question); + incrementNumOfQuestions(); +}); + +addNumericRange.click((e) => { + e.preventDefault(); + const rowId = generateUniqueID(); + const question = ` + + + + + `; + survey.append(question); + incrementNumOfQuestions(); +}); + +submitButton.click((e) => { + e.preventDefault(); + const dataDictionary = {}; + dataDictionary["radioQuestions"] = {}; + dataDictionary["numericRanges"] = {}; + dataDictionary["title"] = formTitle.text(); + // Iterate over table rows with the class 'text-questions' + const textQuestions = [] + $('.text-questions label').each(function () { + textQuestions.push($(this).text()) + }).get(); + dataDictionary["textQuestions"] = textQuestions; + + $('.radio-questions').each(function() { + const title = $(this).find('.title').text(); + const radioQuestions = []; + const radioQuestionContainer = $(this).find('div'); + $(radioQuestionContainer).find('label:not(.title)').each(function () { + radioQuestions.push($(this).text()); + }); + dataDictionary["radioQuestions"][title] = radioQuestions; + }); + + $('.numeric-questions').each(function() { + const title = $(this).find(".title").text(); + const ranges = []; + $(this).find("span").each(function() { + ranges.push(parseInt($(this).text())); + }) + dataDictionary["numericRanges"][title] = ranges; + }); + + console.log(dataDictionary); + + // send post using ajax + const dataJson = JSON.stringify(dataDictionary); + $.ajax({ + type: $("#survey-container").attr("method"), + url: '/api/v1/createSurvey', + data: dataJson, + contentType: 'application/json', + success: function(res) { + // Handle the success response (optional) + console.log('Survey created successfully'); + if (res === 200) window.location.href = "/"; + // You can add code to redirect to the home page if needed + }, + error: function(xhr, status, error) { + // Handle the error response (optional) + console.error('Error creating survey:', error); + } + }); +}); \ No newline at end of file From 1462fda0adf725299b6d99cd9f99428f2d12fc5e Mon Sep 17 00:00:00 2001 From: anthony-massaad Date: Sat, 4 Nov 2023 20:09:30 -0400 Subject: [PATCH 10/10] ditto --- .../controllers/PageController.java | 6 +- .../resources/static/scripts/createSurvey.js | 5 +- .../resources/templates/createSurvey.html | 171 +----------------- 3 files changed, 6 insertions(+), 176 deletions(-) diff --git a/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java b/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java index ca77778..dec9210 100644 --- a/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java +++ b/src/main/java/com/opinionowl/opinionowl/controllers/PageController.java @@ -72,11 +72,11 @@ public String getAnswerSurveyPage(@RequestParam(value = "surveyId") Long surveyI for (int i = 0; i { data: dataJson, contentType: 'application/json', success: function(res) { - // Handle the success response (optional) + // success handling console.log('Survey created successfully'); if (res === 200) window.location.href = "/"; - // You can add code to redirect to the home page if needed }, error: function(xhr, status, error) { - // Handle the error response (optional) + // error handling console.error('Error creating survey:', error); } }); diff --git a/src/main/resources/templates/createSurvey.html b/src/main/resources/templates/createSurvey.html index 6716ef3..ff98185 100644 --- a/src/main/resources/templates/createSurvey.html +++ b/src/main/resources/templates/createSurvey.html @@ -24,175 +24,6 @@

Form Title

- +
+ + + +
+ +
+ + +
+ +
+ + +
+ +
+ + + +
+ 0 + + 11 +