diff --git a/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_databind_2_11_4.xml b/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_databind_2_11_1.xml similarity index 66% rename from .idea/libraries/Maven__com_fasterxml_jackson_core_jackson_databind_2_11_4.xml rename to .idea/libraries/Maven__com_fasterxml_jackson_core_jackson_databind_2_11_1.xml index 247505a..08c0010 100644 --- a/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_databind_2_11_4.xml +++ b/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_databind_2_11_1.xml @@ -1,13 +1,13 @@ - + - + - + - + \ No newline at end of file diff --git a/mcitmocks.iml b/mcitmocks.iml index c7fba72..e21b055 100644 --- a/mcitmocks.iml +++ b/mcitmocks.iml @@ -39,7 +39,6 @@ - @@ -58,7 +57,6 @@ - @@ -80,9 +78,6 @@ - - - @@ -99,9 +94,9 @@ - - - + + + @@ -124,5 +119,24 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index a9e8f3a..b56983a 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,11 @@ org.springframework.boot spring-boot-starter-oauth2-client + + com.fasterxml.jackson.core + jackson-databind + 2.11.1 + diff --git a/src/main/java/com/mcitmocks/mcitmocks/InterviewQuestion/InterviewQuestion.java b/src/main/java/com/mcitmocks/mcitmocks/InterviewQuestion/InterviewQuestion.java index 768a132..16ded75 100644 --- a/src/main/java/com/mcitmocks/mcitmocks/InterviewQuestion/InterviewQuestion.java +++ b/src/main/java/com/mcitmocks/mcitmocks/InterviewQuestion/InterviewQuestion.java @@ -3,11 +3,12 @@ import org.hibernate.annotations.GenericGenerator; import javax.persistence.*; +import java.beans.ConstructorProperties; import java.util.HashSet; import java.util.Set; @Entity -@Table(name="interview_questions") +@Table(name = "interview_questions") public class InterviewQuestion { @Id @@ -23,10 +24,14 @@ public class InterviewQuestion { @ElementCollection private Set questionTypes; - protected InterviewQuestion() {} + protected InterviewQuestion() { + } - public InterviewQuestion(String questionName, QuestionDifficulty questionDifficulty, HashSet questionTypes) { + @ConstructorProperties({"questionName", "questionText", "answerText", "questionDifficulty", "questionTypes"}) + public InterviewQuestion(String questionName, String questionText, String answerText, QuestionDifficulty questionDifficulty, Set questionTypes) { this.questionName = questionName; + this.questionText = questionText; + this.answerText = answerText; this.questionDifficulty = questionDifficulty; this.questionTypes = questionTypes; } @@ -43,4 +48,15 @@ public Set getQuestionTypes() { return this.questionTypes; } + @Override + public String toString() { + return "InterviewQuestion{" + + "id='" + id + '\'' + + ", questionName='" + questionName + '\'' + + ", questionText='" + questionText + '\'' + + ", answerText='" + answerText + '\'' + + ", questionDifficulty=" + questionDifficulty + + ", questionTypes=" + questionTypes + + '}'; + } } diff --git a/src/main/java/com/mcitmocks/mcitmocks/InterviewQuestion/InterviewQuestionController.java b/src/main/java/com/mcitmocks/mcitmocks/InterviewQuestion/InterviewQuestionController.java index a66336f..28c42f5 100644 --- a/src/main/java/com/mcitmocks/mcitmocks/InterviewQuestion/InterviewQuestionController.java +++ b/src/main/java/com/mcitmocks/mcitmocks/InterviewQuestion/InterviewQuestionController.java @@ -10,7 +10,7 @@ public class InterviewQuestionController { @Autowired private InterviewQuestionRepository interviewQuestionRepository; - @GetMapping(value="/api/questions/random", produces = "application/json") + @GetMapping(value = "/api/questions/random", produces = "application/json") public InterviewQuestion getRandom() { return interviewQuestionRepository.getRandom(); } diff --git a/src/main/java/com/mcitmocks/mcitmocks/InterviewQuestion/InterviewQuestionLoader.java b/src/main/java/com/mcitmocks/mcitmocks/InterviewQuestion/InterviewQuestionLoader.java index a5b56ba..22b67dc 100644 --- a/src/main/java/com/mcitmocks/mcitmocks/InterviewQuestion/InterviewQuestionLoader.java +++ b/src/main/java/com/mcitmocks/mcitmocks/InterviewQuestion/InterviewQuestionLoader.java @@ -1,10 +1,14 @@ package com.mcitmocks.mcitmocks.InterviewQuestion; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; -import java.util.HashSet; +import java.io.File; +import java.io.IOException; +import java.util.List; @Component public class InterviewQuestionLoader implements CommandLineRunner { @@ -18,22 +22,17 @@ public void run(String... args) throws Exception { seedInterviewQuestions(); } - private void seedInterviewQuestions() { - HashSet questionTypes1 = new HashSet<>(); - questionTypes1.add(QuestionType.ARRAY); - InterviewQuestion question1 = new InterviewQuestion("Two Sum", QuestionDifficulty.EASY, questionTypes1); - interviewQuestionRepository.save(question1); - - HashSet questionTypes2 = new HashSet<>(); - questionTypes2.add(QuestionType.LINKED_LIST); - questionTypes2.add(QuestionType.RECURSION); - InterviewQuestion question2 = new InterviewQuestion("Reverse Linked List", QuestionDifficulty.EASY, questionTypes2); - interviewQuestionRepository.save(question2); - - HashSet questionTypes3 = new HashSet<>(); - questionTypes3.add(QuestionType.TREE); - questionTypes3.add(QuestionType.RECURSION); - InterviewQuestion question3 = new InterviewQuestion("Binary Tree Inorder Traversal", QuestionDifficulty.EASY, questionTypes3); - interviewQuestionRepository.save(question3); + private static List readJsonQuestions() throws IOException { + ObjectMapper mapper = new ObjectMapper(); + return mapper.readValue(new File("src/main/java/com/mcitmocks/mcitmocks/InterviewQuestion/mock-questions.json"), new TypeReference>() { + }); + } + + private void seedInterviewQuestions() throws IOException { + readJsonQuestions().forEach(interviewQuestionRepository::save); + } + + public static void main(String[] args) throws IOException { + readJsonQuestions().forEach(System.out::println); } } diff --git a/src/main/java/com/mcitmocks/mcitmocks/InterviewQuestion/mock-questions.json b/src/main/java/com/mcitmocks/mcitmocks/InterviewQuestion/mock-questions.json new file mode 100644 index 0000000..acff1dc --- /dev/null +++ b/src/main/java/com/mcitmocks/mcitmocks/InterviewQuestion/mock-questions.json @@ -0,0 +1,31 @@ +[ + { + "questionName": "Two Sum", + "questionText": "Hello world", + "answerText": "Hello to you too", + "questionDifficulty": "EASY", + "questionTypes": [ + "ARRAY" + ] + }, + { + "questionName": "Reverse Linked List", + "questionText": "Hello world", + "answerText": "Hello to you too", + "questionDifficulty": "EASY", + "questionTypes": [ + "LINKED_LIST", + "RECURSION" + ] + }, + { + "questionName": "Binary Tree Inorder Traversal", + "questionText": "Hello world", + "answerText": "Hello to you too", + "questionDifficulty": "EASY", + "questionTypes": [ + "TREE", + "RECURSION" + ] + } +] \ No newline at end of file