From 5a9a096fb4f74598a47ce056dd856eafb3ba2876 Mon Sep 17 00:00:00 2001 From: khair eldeen zakaria shkokany Date: Mon, 25 Oct 2021 01:29:35 +0300 Subject: [PATCH 1/8] 60% quiz --- build.gradle | 5 +- .../init/controllers/QuizController.java | 10 ++- .../java/com/example/init/models/Answers.java | 71 +++++++++++++++++++ .../java/com/example/init/models/Quiz.java | 66 ++++++++++------- src/main/resources/application.properties | 18 +++-- src/main/resources/templates/quiz.html | 53 ++++++++++---- 6 files changed, 177 insertions(+), 46 deletions(-) create mode 100644 src/main/java/com/example/init/models/Answers.java diff --git a/build.gradle b/build.gradle index 8d03cce..992f7b1 100644 --- a/build.gradle +++ b/build.gradle @@ -19,11 +19,14 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-websocket' implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5' + implementation 'org.projectlombok:lombok:1.18.20' developmentOnly 'org.springframework.boot:spring-boot-devtools' - runtimeOnly 'org.postgresql:postgresql' +// runtimeOnly 'org.postgresql:postgresql' + runtimeOnly 'com.h2database:h2' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.security:spring-security-test' implementation 'com.google.code.gson:gson:2.8.8' + implementation 'com.googlecode.json-simple:json-simple:1.1.1' } test { diff --git a/src/main/java/com/example/init/controllers/QuizController.java b/src/main/java/com/example/init/controllers/QuizController.java index 794de6b..fd0b954 100644 --- a/src/main/java/com/example/init/controllers/QuizController.java +++ b/src/main/java/com/example/init/controllers/QuizController.java @@ -3,6 +3,7 @@ import com.example.init.models.Quiz; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; +import org.json.simple.JSONObject; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -32,9 +33,16 @@ public String getQuiz(Model model) throws IOException { BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String data = bufferedReader.readLine(); Type jsonCasting = new TypeToken>(){}.getType(); +// JSONObject jsonObject = new JSONObject(); +// jsonObject.put("answers" , data); + + + + +// List jsonList = gson.fromJson(data,jsonCasting); List jsonList = gson.fromJson(data,jsonCasting); model.addAttribute("quiz",jsonList); -// model.addAttribute("answers",jsonList); +// model.addAttribute("answers",jsonObject); bufferedReader.close(); return "quiz"; } diff --git a/src/main/java/com/example/init/models/Answers.java b/src/main/java/com/example/init/models/Answers.java new file mode 100644 index 0000000..204ea15 --- /dev/null +++ b/src/main/java/com/example/init/models/Answers.java @@ -0,0 +1,71 @@ +package com.example.init.models; + +import lombok.Data; + +@Data +public class Answers { + + public String answear_a; + public String answear_b; + public String answear_c; + public String answear_d; + public String answear_e; + public String answear_f; + + public Answers(String answear_a, String answear_b, String answear_c, String answear_d, String answear_e, String answear_f) { + this.answear_a = answear_a; + this.answear_b = answear_b; + this.answear_c = answear_c; + this.answear_d = answear_d; + this.answear_e = answear_e; + this.answear_f = answear_f; + } + + public String getAnswear_a() { + return answear_a; + } + + public void setAnswear_a(String answear_a) { + this.answear_a = answear_a; + } + + public String getAnswear_b() { + return answear_b; + } + + public void setAnswear_b(String answear_b) { + this.answear_b = answear_b; + } + + public String getAnswear_c() { + return answear_c; + } + + public void setAnswear_c(String answear_c) { + this.answear_c = answear_c; + } + + public String getAnswear_d() { + return answear_d; + } + + public void setAnswear_d(String answear_d) { + this.answear_d = answear_d; + } + + public String getAnswear_e() { + return answear_e; + } + + public void setAnswear_e(String answear_e) { + this.answear_e = answear_e; + } + + public String getAnswear_f() { + return answear_f; + } + + public void setAnswear_f(String answear_f) { + this.answear_f = answear_f; + } +} diff --git a/src/main/java/com/example/init/models/Quiz.java b/src/main/java/com/example/init/models/Quiz.java index bf4692f..ce69a9e 100644 --- a/src/main/java/com/example/init/models/Quiz.java +++ b/src/main/java/com/example/init/models/Quiz.java @@ -1,6 +1,7 @@ package com.example.init.models; import com.google.gson.Gson; +import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; import javax.persistence.*; @@ -8,46 +9,63 @@ import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; -@Entity public class Quiz { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "id", nullable = false) - private Long id; + public int id; public String question; -// public String answers; + public Object description; + public Answers answers; + public String multiple_correct_answers; + public CorrectAnswers correct_answers; + public String correct_answer; + public Object explanation; + public Object tip; + public List tags; + public String category; + public String difficulty; - public Quiz(String question) { + public Quiz(int id, String question, Object description, Answers answers, String multiple_correct_answers, CorrectAnswers correct_answers, String correct_answer, Object explanation, Object tip, List tags, String category, String difficulty) { + this.id = id; this.question = question; -// this.answers = answers; + this.description = description; + this.answers = answers; + this.multiple_correct_answers = multiple_correct_answers; + this.correct_answers = correct_answers; + this.correct_answer = correct_answer; + this.explanation = explanation; + this.tip = tip; + this.tags = tags; + this.category = category; + this.difficulty = difficulty; } - public Long getId() { - return id; + private static class Answers{ + public String answer_a; + public String answer_b; + public String answer_c; + public String answer_d; + public Object answer_e; + public Object answer_f; } -// public String url = "https://quizapi.io/api/v1/questions?apiKey=phkL2Z69NDCrImLlfnLoElQkoF3StoJlQrAeKvkf&limit=5&css"; + private static class CorrectAnswers{ + public String answer_a_correct; + public String answer_b_correct; + public String answer_c_correct; + public String answer_d_correct; + public String answer_e_correct; + public String answer_f_correct; + } - public Quiz() { + private static class Tag{ + public String name; } -// public String getAnswers() { -// return answers; -// } -// public void setAnswers(String answers) { -// this.answers = answers; -// } - public String getQuestion() { - return question; - } - public void setQuestion(String question) { - this.question = question; - } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index ae3973a..b1d92b6 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,18 +1,22 @@ -spring.datasource.hikari.connectionTimeout=20000 -spring.datasource.hikari.maximumPoolSize=5 spring.sql.init.platform=postgres -spring.datasource.url=jdbc:postgresql://localhost:5432/init -spring.datasource.username=mohammad -spring.datasource.password=1234 +#spring.datasource.url=jdbc:postgresql://localhost:5432/coders +#spring.datasource.username=postgres +#spring.datasource.password=12345 -spring.jpa.hibernate.ddl-auto=update +spring.jpa.hibernate.ddl-auto=create spring.datasource.initialization-mode=always +spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password=password +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect + +#spring.jpa.database-platform = org.hibernate.dialect.PostgreSQL94Dialect spring.jpa.show-sql=true spring.jpa.generate-ddl=false spring.jpa.open-in-view=false spring.jpa.properties.hibernate.show_sql=true spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.type=trace -spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect diff --git a/src/main/resources/templates/quiz.html b/src/main/resources/templates/quiz.html index 5e39f28..541a39c 100644 --- a/src/main/resources/templates/quiz.html +++ b/src/main/resources/templates/quiz.html @@ -10,23 +10,50 @@ - - + +
- - - - - - - - - - - +
+ +

+

+

+

+

+

+ + + +

+

+

+

+

+

+ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 1787d6735ae4328255c4364e1535030d5d75b66c Mon Sep 17 00:00:00 2001 From: khair eldeen zakaria shkokany Date: Mon, 25 Oct 2021 17:38:01 +0300 Subject: [PATCH 2/8] quiz form --- build.gradle | 6 +- .../init/controllers/QuizController.java | 63 +++++++++++--- .../java/com/example/init/models/Quiz.java | 20 +++-- .../com/example/init/models/ResultsQuiz.java | 53 ++++++++++++ .../init/repositories/ResultsRepo.java | 9 ++ .../example/init/serviceQuiz/ServiceQuiz.java | 46 ++++++++++ src/main/resources/application.properties | 22 ++--- src/main/resources/statics/quiz.css | 48 +++++++++++ src/main/resources/templates/quiz.html | 86 ++++++++++++------- src/main/resources/templates/result.html | 43 ++++++++++ 10 files changed, 337 insertions(+), 59 deletions(-) create mode 100644 src/main/java/com/example/init/models/ResultsQuiz.java create mode 100644 src/main/java/com/example/init/repositories/ResultsRepo.java create mode 100644 src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java create mode 100644 src/main/resources/statics/quiz.css create mode 100644 src/main/resources/templates/result.html diff --git a/build.gradle b/build.gradle index 992f7b1..41d4cf6 100644 --- a/build.gradle +++ b/build.gradle @@ -21,8 +21,10 @@ dependencies { implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5' implementation 'org.projectlombok:lombok:1.18.20' developmentOnly 'org.springframework.boot:spring-boot-devtools' -// runtimeOnly 'org.postgresql:postgresql' - runtimeOnly 'com.h2database:h2' + implementation 'org.webjars:bootstrap:4.1.3' + + runtimeOnly 'org.postgresql:postgresql' +// runtimeOnly 'com.h2database:h2' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.security:spring-security-test' implementation 'com.google.code.gson:gson:2.8.8' diff --git a/src/main/java/com/example/init/controllers/QuizController.java b/src/main/java/com/example/init/controllers/QuizController.java index fd0b954..be2cac5 100644 --- a/src/main/java/com/example/init/controllers/QuizController.java +++ b/src/main/java/com/example/init/controllers/QuizController.java @@ -1,12 +1,20 @@ package com.example.init.controllers; +import com.example.init.models.ApplicationUser; import com.example.init.models.Quiz; +import com.example.init.models.ResultsQuiz; +import com.example.init.repositories.ResultsRepo; +import com.example.init.serviceQuiz.ServiceQuiz; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; +import org.dom4j.rule.Mode; import org.json.simple.JSONObject; +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 org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; import java.io.BufferedReader; import java.io.FileReader; @@ -21,9 +29,10 @@ @Controller public class QuizController { - - - + @Autowired + ServiceQuiz qService; + @Autowired + ResultsRepo resultsRepo; @GetMapping("/quiz") public String getQuiz(Model model) throws IOException { Gson gson = new Gson(); @@ -33,17 +42,49 @@ public String getQuiz(Model model) throws IOException { BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String data = bufferedReader.readLine(); Type jsonCasting = new TypeToken>(){}.getType(); -// JSONObject jsonObject = new JSONObject(); -// jsonObject.put("answers" , data); - - - - -// List jsonList = gson.fromJson(data,jsonCasting); List jsonList = gson.fromJson(data,jsonCasting); model.addAttribute("quiz",jsonList); -// model.addAttribute("answers",jsonObject); bufferedReader.close(); return "quiz"; } +// @PostMapping("/quiz") +// public String postQuiz(Model model){ +//// Quiz quiz = new Quiz(); +// Quiz quiz = (Quiz) qService.getQustion(); +// model.addAttribute("quiz" , quiz); +// +// return "quiz"; +// } + + + @PostMapping("/submit") + public String submit(@ModelAttribute Quiz quiz,Model model ){ + ResultsQuiz resultsQuiz = new ResultsQuiz(); + ApplicationUser applicationUser = new ApplicationUser(); + resultsQuiz.setTotalCorrect(qService.getAnswersTest(quiz)); + resultsQuiz.setUsername(applicationUser.getUsername()); + resultsQuiz.setTotalCorrect(resultsQuiz.getTotalCorrect()); + model.addAttribute("resultQuiz", resultsRepo.save(resultsQuiz)); + return "result"; + } + + +// +// @GetMapping("/posts") +// public String getPostForUsername(Model model , Principal principal) { +// ApplicationUser applicationUser = repositeryData.findApplicationUserByUsername(principal.getName()); +// model.addAttribute("username" , applicationUser); +// return "posts"; +// } +// +// @PostMapping("/posts") +// public RedirectView createPostUsername(Model model , Principal principal , String body) +// { +// ApplicationUser applicationUser = repositeryData.findApplicationUserByUsername(principal.getName()); +// Post post = new Post(applicationUser , body); +// post = repositeryPost.save(post); +// model.addAttribute("username" , applicationUser.getWrittenPost()); +// return new RedirectView("/profile"); +// } + } diff --git a/src/main/java/com/example/init/models/Quiz.java b/src/main/java/com/example/init/models/Quiz.java index ce69a9e..b2e2e36 100644 --- a/src/main/java/com/example/init/models/Quiz.java +++ b/src/main/java/com/example/init/models/Quiz.java @@ -26,8 +26,9 @@ public class Quiz { public List tags; public String category; public String difficulty; + public int chose = -1; - public Quiz(int id, String question, Object description, Answers answers, String multiple_correct_answers, CorrectAnswers correct_answers, String correct_answer, Object explanation, Object tip, List tags, String category, String difficulty) { + public Quiz(int id, String question, Object description, Answers answers, String multiple_correct_answers, CorrectAnswers correct_answers, String correct_answer, Object explanation, Object tip, List tags, String category, String difficulty , int chose) { this.id = id; this.question = question; this.description = description; @@ -40,18 +41,23 @@ public Quiz(int id, String question, Object description, Answers answers, String this.tags = tags; this.category = category; this.difficulty = difficulty; + this.chose = chose; } - private static class Answers{ + public Quiz() { + + } + + public static class Answers{ public String answer_a; public String answer_b; public String answer_c; public String answer_d; - public Object answer_e; - public Object answer_f; + public String answer_e; + public String answer_f; } - private static class CorrectAnswers{ + public static class CorrectAnswers{ public String answer_a_correct; public String answer_b_correct; public String answer_c_correct; @@ -68,4 +74,8 @@ private static class Tag{ + + + + } diff --git a/src/main/java/com/example/init/models/ResultsQuiz.java b/src/main/java/com/example/init/models/ResultsQuiz.java new file mode 100644 index 0000000..8c9934d --- /dev/null +++ b/src/main/java/com/example/init/models/ResultsQuiz.java @@ -0,0 +1,53 @@ +package com.example.init.models; + + +import javax.persistence.*; + +@Entity +@Table(name = "results") +public class ResultsQuiz { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) +// @Column(name = "id", nullable = false) + private int id; + private String username; + private int totalCorrect; + + public ResultsQuiz() { + } + + public ResultsQuiz(int id ,String username, int totalCorrect) { + this.id = id; + this.username = username; + this.totalCorrect = totalCorrect; + } + + public int getId() { + return id; + } + + + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public int getTotalCorrect() { + return totalCorrect; + } + + public void setTotalCorrect(int totalCorrect) { + this.totalCorrect = totalCorrect; + } + // public Long getId() { +// return id; +// } +// +// public void setId(Long id) { +// this.id = id; +// } +} diff --git a/src/main/java/com/example/init/repositories/ResultsRepo.java b/src/main/java/com/example/init/repositories/ResultsRepo.java new file mode 100644 index 0000000..3f54bb9 --- /dev/null +++ b/src/main/java/com/example/init/repositories/ResultsRepo.java @@ -0,0 +1,9 @@ +package com.example.init.repositories; + +import com.example.init.models.ResultsQuiz; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ResultsRepo extends JpaRepository { +} diff --git a/src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java b/src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java new file mode 100644 index 0000000..95da3f0 --- /dev/null +++ b/src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java @@ -0,0 +1,46 @@ +package com.example.init.serviceQuiz; + +import com.example.init.models.Quiz; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +@Service +public class ServiceQuiz { + + public List getQustion() { + + List allQues = new ArrayList(); + return allQues; + } + +public int counter = 0; + + public int getAnswersTest(Quiz quiz) { + + int correct = 0; + + for(Quiz q: getQustion()) { + if (q.correct_answer == q.answers.answer_a){ + correct++; + }else if (q.correct_answer == q.answers.answer_b) { + correct++; + }else if (q.correct_answer == q.answers.answer_c) { + correct++; + }else if (q.correct_answer == q.answers.answer_d) { + correct++; + }else if (q.correct_answer == q.answers.answer_e) { + correct++; + }else if (q.correct_answer == q.answers.answer_f) { + correct++; + } + return correct; + } + + + + return correct; + } + + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index b1d92b6..1d00730 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,19 +1,19 @@ spring.sql.init.platform=postgres -#spring.datasource.url=jdbc:postgresql://localhost:5432/coders -#spring.datasource.username=postgres -#spring.datasource.password=12345 +spring.datasource.url=jdbc:postgresql://localhost:5432/coders +spring.datasource.username=postgres +spring.datasource.password=12345 -spring.jpa.hibernate.ddl-auto=create +spring.jpa.hibernate.ddl-auto=create-drop spring.datasource.initialization-mode=always +# +#spring.datasource.url=jdbc:h2:mem:testdb +#spring.datasource.driverClassName=org.h2.Driver +#spring.datasource.username=sa +#spring.datasource.password=password +#spring.jpa.database-platform=org.hibernate.dialect.H2Dialect -spring.datasource.url=jdbc:h2:mem:testdb -spring.datasource.driverClassName=org.h2.Driver -spring.datasource.username=sa -spring.datasource.password=password -spring.jpa.database-platform=org.hibernate.dialect.H2Dialect - -#spring.jpa.database-platform = org.hibernate.dialect.PostgreSQL94Dialect +spring.jpa.database-platform = org.hibernate.dialect.PostgreSQL94Dialect spring.jpa.show-sql=true spring.jpa.generate-ddl=false spring.jpa.open-in-view=false diff --git a/src/main/resources/statics/quiz.css b/src/main/resources/statics/quiz.css new file mode 100644 index 0000000..2ff53bb --- /dev/null +++ b/src/main/resources/statics/quiz.css @@ -0,0 +1,48 @@ +* { + margin: 0; + padding: 0; + box-sizing: border-box; +} + +body { + width: 100%; + overflow-x: hidden; +} + +h1 { + font-size: 22px; + text-align: center; + padding: 15px 25px; + border-radius: 4px; +} + +.ques-box { + width: 100%; + max-width: 800px; + padding: 15px 25px; + margin: 50px auto 0 auto; + border-radius: 4px; +} + +.option { + display: flex; + flex-direction: row; +} + +.title { + font-size: 18px; + font-weight: 500; + color: red; +} + +.option p { + margin-left: 15px; + position: relative; + margin-top: -6px; +} + +footer { + height: 80px; + text-align: center; + line-height: 80px; +} \ No newline at end of file diff --git a/src/main/resources/templates/quiz.html b/src/main/resources/templates/quiz.html index 541a39c..f5c41c6 100644 --- a/src/main/resources/templates/quiz.html +++ b/src/main/resources/templates/quiz.html @@ -2,6 +2,9 @@ + + + Quiz Page @@ -10,49 +13,72 @@ - - -
- -
+ + + + + + + + + + + + + + + + + +
+

Only Select The Correct Answers

+
+
+ +
+

-

-

-

-

-

+
+
+ +

+
+
+ +

+
-

-

-

-

-

-

- - + + + + + + + + + + + - - - - - - - - - - - - + +
+ + + +
+ + + diff --git a/src/main/resources/templates/result.html b/src/main/resources/templates/result.html new file mode 100644 index 0000000..9543103 --- /dev/null +++ b/src/main/resources/templates/result.html @@ -0,0 +1,43 @@ + + + + + Result Page + + + + +
+
+ + +

Your Score

+ +
+ +

+ +
+ +
+

Total correct answer:  

+

+
+ +
+

Total false answer:  

+

+
+ +
+ + +
+
+
+ + + \ No newline at end of file From 7598ba27fbf0966873ee2838bf692d8b3cc8b859 Mon Sep 17 00:00:00 2001 From: khair eldeen zakaria shkokany Date: Mon, 25 Oct 2021 23:47:25 +0300 Subject: [PATCH 3/8] khair quiz to jamal --- .../init/controllers/QuizController.java | 21 ++++-- .../java/com/example/init/models/Quiz.java | 25 +++++-- .../com/example/init/models/ResultsQuiz.java | 13 ++-- .../init/security/WebSecurityConfig.java | 2 +- .../example/init/serviceQuiz/ServiceQuiz.java | 74 +++++++------------ src/main/resources/application.properties | 2 +- src/main/resources/templates/quiz.html | 8 +- src/main/resources/templates/result.html | 6 +- 8 files changed, 76 insertions(+), 75 deletions(-) diff --git a/src/main/java/com/example/init/controllers/QuizController.java b/src/main/java/com/example/init/controllers/QuizController.java index be2cac5..51e7cad 100644 --- a/src/main/java/com/example/init/controllers/QuizController.java +++ b/src/main/java/com/example/init/controllers/QuizController.java @@ -3,8 +3,9 @@ import com.example.init.models.ApplicationUser; import com.example.init.models.Quiz; import com.example.init.models.ResultsQuiz; +import com.example.init.repositories.ApplicationUserRepository; import com.example.init.repositories.ResultsRepo; -import com.example.init.serviceQuiz.ServiceQuiz; + import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import org.dom4j.rule.Mode; @@ -23,16 +24,18 @@ import java.lang.reflect.Type; import java.net.HttpURLConnection; import java.net.URL; +import java.security.Principal; import java.util.ArrayList; import java.util.List; @Controller public class QuizController { - @Autowired - ServiceQuiz qService; + @Autowired ResultsRepo resultsRepo; + @Autowired + ApplicationUserRepository applicationUserRepository; @GetMapping("/quiz") public String getQuiz(Model model) throws IOException { Gson gson = new Gson(); @@ -58,14 +61,16 @@ public String getQuiz(Model model) throws IOException { @PostMapping("/submit") - public String submit(@ModelAttribute Quiz quiz,Model model ){ + public String submit( Model model , Principal principal){ ResultsQuiz resultsQuiz = new ResultsQuiz(); - ApplicationUser applicationUser = new ApplicationUser(); - resultsQuiz.setTotalCorrect(qService.getAnswersTest(quiz)); - resultsQuiz.setUsername(applicationUser.getUsername()); +// ApplicationUser applicationUser = new ApplicationUser(); + Quiz quiz = new Quiz(); +// resultsQuiz.setTotalCorrect(quiz.getAnswers()); + resultsQuiz.setUsername(applicationUserRepository.findByUsername(principal.getName())); resultsQuiz.setTotalCorrect(resultsQuiz.getTotalCorrect()); - model.addAttribute("resultQuiz", resultsRepo.save(resultsQuiz)); + model.addAttribute("quiz", resultsRepo.save(resultsQuiz)); return "result"; + } diff --git a/src/main/java/com/example/init/models/Quiz.java b/src/main/java/com/example/init/models/Quiz.java index b2e2e36..b26c376 100644 --- a/src/main/java/com/example/init/models/Quiz.java +++ b/src/main/java/com/example/init/models/Quiz.java @@ -70,12 +70,25 @@ private static class Tag{ public String name; } + public int getAnswers() { + int correct = 0; + if (answers.answer_a == (correct_answer)){ + correct++; +} + else if (correct_answer.equals(answers.answer_b) ){ + correct++; + }else if (correct_answer.equals(answers.answer_c)) { + correct++; + }else if (correct_answer .equals(answers.answer_d)) { + correct++; + }else if (correct_answer.equals(answers.answer_e)) { + correct++; + }else if (correct_answer.equals(answers.answer_f)) { + correct++; + } + return correct; + } + } - - - - - -} diff --git a/src/main/java/com/example/init/models/ResultsQuiz.java b/src/main/java/com/example/init/models/ResultsQuiz.java index 8c9934d..d6e6c94 100644 --- a/src/main/java/com/example/init/models/ResultsQuiz.java +++ b/src/main/java/com/example/init/models/ResultsQuiz.java @@ -10,13 +10,15 @@ public class ResultsQuiz { @GeneratedValue(strategy = GenerationType.IDENTITY) // @Column(name = "id", nullable = false) private int id; - private String username; - private int totalCorrect; + @ManyToOne + @JoinColumn(name = "username_id") + public ApplicationUser username; + public int totalCorrect; public ResultsQuiz() { } - public ResultsQuiz(int id ,String username, int totalCorrect) { + public ResultsQuiz(int id ,ApplicationUser username, int totalCorrect) { this.id = id; this.username = username; this.totalCorrect = totalCorrect; @@ -27,12 +29,11 @@ public int getId() { } - - public String getUsername() { + public ApplicationUser getUsername() { return username; } - public void setUsername(String username) { + public void setUsername(ApplicationUser username) { this.username = username; } diff --git a/src/main/java/com/example/init/security/WebSecurityConfig.java b/src/main/java/com/example/init/security/WebSecurityConfig.java index 3b7cd93..8b0220b 100644 --- a/src/main/java/com/example/init/security/WebSecurityConfig.java +++ b/src/main/java/com/example/init/security/WebSecurityConfig.java @@ -33,7 +33,7 @@ protected void configure(HttpSecurity http) throws Exception { csrf() .disable() .authorizeRequests() - .antMatchers( "/login", "/signup","/","/quiz") + .antMatchers( "/login", "/signup","/","/quiz","/submit") .permitAll().antMatchers("/adminpanel") .hasAuthority("ROLE_ADMIN") .anyRequest().authenticated() diff --git a/src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java b/src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java index 95da3f0..b3130dd 100644 --- a/src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java +++ b/src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java @@ -1,46 +1,28 @@ -package com.example.init.serviceQuiz; - -import com.example.init.models.Quiz; -import org.springframework.stereotype.Service; - -import java.util.ArrayList; -import java.util.List; -@Service -public class ServiceQuiz { - - public List getQustion() { - - List allQues = new ArrayList(); - return allQues; - } - -public int counter = 0; - - public int getAnswersTest(Quiz quiz) { - - int correct = 0; - - for(Quiz q: getQustion()) { - if (q.correct_answer == q.answers.answer_a){ - correct++; - }else if (q.correct_answer == q.answers.answer_b) { - correct++; - }else if (q.correct_answer == q.answers.answer_c) { - correct++; - }else if (q.correct_answer == q.answers.answer_d) { - correct++; - }else if (q.correct_answer == q.answers.answer_e) { - correct++; - }else if (q.correct_answer == q.answers.answer_f) { - correct++; - } - return correct; - } - - - - return correct; - } - - -} +//package com.example.init.serviceQuiz; +// +//import com.example.init.models.Quiz; +//import org.springframework.stereotype.Service; +// +//import java.util.ArrayList; +//import java.util.List; +//@Service +//public class ServiceQuiz { +// +// public List getQustion() { +// +// return new ArrayList(); +// } +// +// public int counter = 0; +// +// public void getAnswersTest() { +// +// int correct = 10; +// +// Quiz quiz = new Quiz(); +// +// +// } +//} +// +// diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 1d00730..c701635 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -4,7 +4,7 @@ spring.datasource.url=jdbc:postgresql://localhost:5432/coders spring.datasource.username=postgres spring.datasource.password=12345 -spring.jpa.hibernate.ddl-auto=create-drop +spring.jpa.hibernate.ddl-auto=update spring.datasource.initialization-mode=always # #spring.datasource.url=jdbc:h2:mem:testdb diff --git a/src/main/resources/templates/quiz.html b/src/main/resources/templates/quiz.html index f5c41c6..6729680 100644 --- a/src/main/resources/templates/quiz.html +++ b/src/main/resources/templates/quiz.html @@ -36,20 +36,20 @@

Only Select The Correct Answers

- +
- +

- +

- +

diff --git a/src/main/resources/templates/result.html b/src/main/resources/templates/result.html index 9543103..84c1bf6 100644 --- a/src/main/resources/templates/result.html +++ b/src/main/resources/templates/result.html @@ -16,18 +16,18 @@

Your Score

-

+

Total correct answer:  

-

+

Total false answer:  

-

+

From 8dcf358b8acf303aac6cd224e6a1ed1f1c881424 Mon Sep 17 00:00:00 2001 From: khair eldeen zakaria shkokany Date: Wed, 27 Oct 2021 01:13:03 +0300 Subject: [PATCH 4/8] hope we are done --- .../example/init/controllers/QuizCont.java | 90 ++++++++ .../init/controllers/QuizController.java | 211 ++++++++++-------- .../com/example/init/controllers}/json.json | 0 .../com/example/init/controllers/quiz.json | 50 +++++ .../java/com/example/init/models/Answers.java | 71 ------ .../com/example/init/models/QuestionForm.java | 19 ++ .../java/com/example/init/models/Quiz.java | 47 ++-- .../java/com/example/init/models/QuizDto.java | 145 ++++++++++++ .../com/example/init/models/ResultsQuiz.java | 32 ++- .../init/repositories/QuestionRepo.java | 10 + .../init/repositories/ResultsRepo.java | 1 + .../init/security/WebSecurityConfig.java | 2 +- .../example/init/serviceQuiz/ServiceQuiz.java | 96 +++++--- src/main/resources/templates/quiz.html | 180 ++++++++------- src/main/resources/templates/quiz2.html | 58 +++++ src/main/resources/templates/result.html | 6 +- src/main/resources/templates/resultsv2.html | 55 +++++ src/main/resources/templates/scoreboard.html | 40 ++++ 18 files changed, 810 insertions(+), 303 deletions(-) create mode 100644 src/main/java/com/example/init/controllers/QuizCont.java rename src/main/{resources => java/com/example/init/controllers}/json.json (100%) create mode 100644 src/main/java/com/example/init/controllers/quiz.json delete mode 100644 src/main/java/com/example/init/models/Answers.java create mode 100644 src/main/java/com/example/init/models/QuestionForm.java create mode 100644 src/main/java/com/example/init/models/QuizDto.java create mode 100644 src/main/java/com/example/init/repositories/QuestionRepo.java create mode 100644 src/main/resources/templates/quiz2.html create mode 100644 src/main/resources/templates/resultsv2.html create mode 100644 src/main/resources/templates/scoreboard.html diff --git a/src/main/java/com/example/init/controllers/QuizCont.java b/src/main/java/com/example/init/controllers/QuizCont.java new file mode 100644 index 0000000..9caeec4 --- /dev/null +++ b/src/main/java/com/example/init/controllers/QuizCont.java @@ -0,0 +1,90 @@ +package com.example.init.controllers; + +import com.example.init.models.QuestionForm; +import com.example.init.models.QuizDto; +import com.example.init.models.ResultsQuiz; +import com.example.init.serviceQuiz.ServiceQuiz; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import com.google.gson.stream.JsonReader; +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 org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; +import org.springframework.web.servlet.view.RedirectView; + +import java.io.FileReader; +import java.lang.reflect.Type; +import java.security.Principal; +import java.util.List; + +@Controller +public class QuizCont { + + @Autowired + ResultsQuiz result; + @Autowired + ServiceQuiz qService; + + Boolean submitted = false; + + @GetMapping("/quiz/v2") + public String getQuiz(Model model) throws Exception { + + Gson gson = new Gson(); + + FileReader data = new FileReader("C:\\Users\\STUDENT\\Desktop\\Spring\\Project\\init\\src\\main\\java\\com\\example\\init\\controllers\\quiz.json"); + Type jsonCasting = new TypeToken>() { + }.getType(); + List jsonList = gson.fromJson(data, jsonCasting); + model.addAttribute("quiz", jsonList); + System.out.println(jsonList); + System.out.println("----------------------"); + String numbersJson = gson.toJson(jsonList); + System.out.println(numbersJson); + return "quiz2"; + } + + @PostMapping("/quiz") + public String quiz(@RequestParam String username, Model m, RedirectAttributes ra) { + if(username.equals("")) { + ra.addFlashAttribute("warning", "You must enter your name"); + return "redirect:/"; + } + + submitted = false; + result.setUsername(username); + + QuestionForm qForm = qService.getQuestions(); + m.addAttribute("qForm", qForm); + + return "quiz2.html"; + } + @PostMapping("/submit") + public String submit(@ModelAttribute QuestionForm qForm, Model m) { + if(!submitted) { + result.setTotalCorrect(qService.getResult(qForm)); + qService.saveScore(result); + submitted = true; + } + + + + return "result.html"; + } + + + @GetMapping("/score") + public String score(Model m) { + List sList = qService.getTopScore(); + m.addAttribute("sList", sList); + + return "scoreboard.html"; + } + + +} diff --git a/src/main/java/com/example/init/controllers/QuizController.java b/src/main/java/com/example/init/controllers/QuizController.java index 51e7cad..ecbc8b0 100644 --- a/src/main/java/com/example/init/controllers/QuizController.java +++ b/src/main/java/com/example/init/controllers/QuizController.java @@ -1,95 +1,130 @@ -package com.example.init.controllers; - -import com.example.init.models.ApplicationUser; -import com.example.init.models.Quiz; -import com.example.init.models.ResultsQuiz; -import com.example.init.repositories.ApplicationUserRepository; -import com.example.init.repositories.ResultsRepo; - -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; -import org.dom4j.rule.Mode; -import org.json.simple.JSONObject; -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 org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.PostMapping; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.lang.reflect.Type; -import java.net.HttpURLConnection; -import java.net.URL; -import java.security.Principal; -import java.util.ArrayList; -import java.util.List; - -@Controller -public class QuizController { - - - @Autowired - ResultsRepo resultsRepo; - @Autowired - ApplicationUserRepository applicationUserRepository; - @GetMapping("/quiz") - public String getQuiz(Model model) throws IOException { - Gson gson = new Gson(); - String urlApi = "https://quizapi.io/api/v1/questions?apiKey=phkL2Z69NDCrImLlfnLoElQkoF3StoJlQrAeKvkf&limit=10&css"; - HttpURLConnection connection = (HttpURLConnection) new URL(urlApi).openConnection(); - InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream()); - BufferedReader bufferedReader = new BufferedReader(inputStreamReader); - String data = bufferedReader.readLine(); - Type jsonCasting = new TypeToken>(){}.getType(); - List jsonList = gson.fromJson(data,jsonCasting); - model.addAttribute("quiz",jsonList); - bufferedReader.close(); - return "quiz"; - } +//package com.example.init.controllers; +// +//import com.example.init.models.ApplicationUser; +//import com.example.init.models.Quiz; +//import com.example.init.models.ResultsQuiz; +//import com.example.init.repositories.ApplicationUserRepository; +//import com.example.init.repositories.ResultsRepo; +// +//import com.google.gson.Gson; +//import com.google.gson.reflect.TypeToken; +//import org.dom4j.rule.Mode; +//import org.json.simple.JSONObject; +//import org.springframework.beans.factory.annotation.Autowired; +//import org.springframework.stereotype.Controller; +//import org.springframework.ui.Model; +//import org.springframework.web.bind.annotation.*; +//import org.springframework.web.servlet.view.RedirectView; +// +//import java.io.BufferedReader; +//import java.io.FileReader; +//import java.io.IOException; +//import java.io.InputStreamReader; +//import java.lang.reflect.Type; +//import java.net.HttpURLConnection; +//import java.net.URL; +//import java.security.Principal; +//import java.util.ArrayList; +//import java.util.List; +// +//@Controller +//public class QuizController { +// +// +// @Autowired +// ResultsRepo resultsRepo; +// @Autowired +// ApplicationUserRepository applicationUserRepository; +// @GetMapping("/quiz") +// public String getQuiz(Model model) throws IOException { +// Gson gson = new Gson(); +// String urlApi = "https://quizapi.io/api/v1/questions?apiKey=phkL2Z69NDCrImLlfnLoElQkoF3StoJlQrAeKvkf&limit=10&css"; +// HttpURLConnection connection = (HttpURLConnection) new URL(urlApi).openConnection(); +// InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream()); +// BufferedReader bufferedReader = new BufferedReader(inputStreamReader); +// String data = bufferedReader.readLine(); +// Type jsonCasting = new TypeToken>(){}.getType(); +// List jsonList = gson.fromJson(data,jsonCasting); +// model.addAttribute("quiz",jsonList); +// String numbersJson = gson.toJson(jsonList); +// +// System.out.println(numbersJson); +// bufferedReader.close(); +// return "quiz"; +// } // @PostMapping("/quiz") // public String postQuiz(Model model){ -//// Quiz quiz = new Quiz(); -// Quiz quiz = (Quiz) qService.getQustion(); -// model.addAttribute("quiz" , quiz); +// Quiz quiz = new Quiz(); // +// model.addAttribute("quiz" , quiz); // return "quiz"; // } - - - @PostMapping("/submit") - public String submit( Model model , Principal principal){ - ResultsQuiz resultsQuiz = new ResultsQuiz(); -// ApplicationUser applicationUser = new ApplicationUser(); - Quiz quiz = new Quiz(); -// resultsQuiz.setTotalCorrect(quiz.getAnswers()); - resultsQuiz.setUsername(applicationUserRepository.findByUsername(principal.getName())); - resultsQuiz.setTotalCorrect(resultsQuiz.getTotalCorrect()); - model.addAttribute("quiz", resultsRepo.save(resultsQuiz)); - return "result"; - - } - - -// -// @GetMapping("/posts") -// public String getPostForUsername(Model model , Principal principal) { -// ApplicationUser applicationUser = repositeryData.findApplicationUserByUsername(principal.getName()); -// model.addAttribute("username" , applicationUser); -// return "posts"; -// } // -// @PostMapping("/posts") -// public RedirectView createPostUsername(Model model , Principal principal , String body) -// { -// ApplicationUser applicationUser = repositeryData.findApplicationUserByUsername(principal.getName()); -// Post post = new Post(applicationUser , body); -// post = repositeryPost.save(post); -// model.addAttribute("username" , applicationUser.getWrittenPost()); -// return new RedirectView("/profile"); +// +// @PostMapping("/submit") +// public String submit(@RequestBody Quiz quiz , Model model , Principal principal){ +// Gson gson = new Gson(); +// Quiz quiz1 = new Quiz(); +// ResultsQuiz resultsQuiz = new ResultsQuiz(); +//// System.out.println(quiz.toString()); +// String numbersJson = gson.toJson(quiz); +// System.out.println(numbersJson); +// +// +//// ApplicationUser applicationUser = new ApplicationUser(); +// +// int correct = 0; +// +//// if (quiz[0].correct_answers.answer_a_correct.equals("true")) { +//// correct++; +//// +//// } else if (quiz.correct_answers.answer_b_correct.equals("true") && quiz.correct) { +//// correct++; +//// } else if (quiz.correct_answers.answer_c_correct.equals("true")) { +//// correct++; +//// } else if (quiz.correct_answers.answer_d_correct.equals("true")) { +//// correct++; +//// } else if (quiz.correct_answers.answer_e_correct.equals("true")) { +//// correct++; +//// } else if (quiz.correct_answers.answer_f_correct.equals("true")) { +//// correct++; +//// +//// } +// +// +// resultsQuiz.setTotalCorrect(correct); +//// resultsQuiz.setUsername(applicationUserRepository.findByUsername(principal.getName())); +// resultsQuiz.setTotalCorrect(resultsQuiz.getTotalCorrect()); +// model.addAttribute("resultsData", resultsRepo.save(resultsQuiz)); +// return "result"; // } - -} +//// @PostMapping("/submit/v2") +//// public RedirectView attemptSignUp( +//// @RequestParam String answers_a) +//// +//// { +//// System.out.println(answers_a); +//// +//// return new RedirectView("/quiz"); +//// } +// +// +//// +//// @GetMapping("/posts") +//// public String getPostForUsername(Model model , Principal principal) { +//// ApplicationUser applicationUser = repositeryData.findApplicationUserByUsername(principal.getName()); +//// model.addAttribute("username" , applicationUser); +//// return "posts"; +//// } +//// +//// @PostMapping("/posts") +//// public RedirectView createPostUsername(Model model , Principal principal , String body) +//// { +//// ApplicationUser applicationUser = repositeryData.findApplicationUserByUsername(principal.getName()); +//// Post post = new Post(applicationUser , body); +//// post = repositeryPost.save(post); +//// model.addAttribute("username" , applicationUser.getWrittenPost()); +//// return new RedirectView("/profile"); +//// } +// +//} diff --git a/src/main/resources/json.json b/src/main/java/com/example/init/controllers/json.json similarity index 100% rename from src/main/resources/json.json rename to src/main/java/com/example/init/controllers/json.json diff --git a/src/main/java/com/example/init/controllers/quiz.json b/src/main/java/com/example/init/controllers/quiz.json new file mode 100644 index 0000000..1aa136b --- /dev/null +++ b/src/main/java/com/example/init/controllers/quiz.json @@ -0,0 +1,50 @@ +[ + { + "quesId": 1, + "question": "Which are the three looping constructs provided by shell", + "answer_a": "while", + "answer_b": "for", + "answer_c": "until", + "answer_d": "foreach", + "answer_e": "done", + "answer_f": "each", + "correct_answer":"while" +}, + { + "quesId": 2, + "question": "What are the functions of Replication controller?", + "answer_a": "It is responsible to control and administer the lifecycle of the pod.", + "answer_b": "It helps the user to check the running status of the pod", + "answer_c": "It is responsible to monitor and verify whether the allowed number of pod replicas were running", + "answer_d": "It is responsible for routing the ingress traffic", + "correct_answer": "It is responsible to control and administer the lifecycle of the pod." + }, + { + "quesId": 3, + "question": "Which command can be used by the administrator to bring the system into single user mode?", + "answer_a": "init s", + "answer_b": "single", + "answer_c": "init", + "answer_d": "single s", + "correct_answer": "init s" + + }, + { + "quesId": 4, + "question": "What is the Container Runtime?", + "answer_a": "Software that is responsible for running containers", + "answer_b": "An API object that manages a replicated application", + "answer_c": "Stored instance of a Container that holds a set of software needed to run an application", + "answer_d": "A command line tool for communicating with a Kubernetes API server", + "correct_answer": "Software that is responsible for running containers" + + }, + { + "quesId": 5, + "question": "Is the NULL value treated as 0?", + "answer_a": "True", + "answer_b": "False", + "correct_answer": "False" + } + +] diff --git a/src/main/java/com/example/init/models/Answers.java b/src/main/java/com/example/init/models/Answers.java deleted file mode 100644 index 204ea15..0000000 --- a/src/main/java/com/example/init/models/Answers.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.example.init.models; - -import lombok.Data; - -@Data -public class Answers { - - public String answear_a; - public String answear_b; - public String answear_c; - public String answear_d; - public String answear_e; - public String answear_f; - - public Answers(String answear_a, String answear_b, String answear_c, String answear_d, String answear_e, String answear_f) { - this.answear_a = answear_a; - this.answear_b = answear_b; - this.answear_c = answear_c; - this.answear_d = answear_d; - this.answear_e = answear_e; - this.answear_f = answear_f; - } - - public String getAnswear_a() { - return answear_a; - } - - public void setAnswear_a(String answear_a) { - this.answear_a = answear_a; - } - - public String getAnswear_b() { - return answear_b; - } - - public void setAnswear_b(String answear_b) { - this.answear_b = answear_b; - } - - public String getAnswear_c() { - return answear_c; - } - - public void setAnswear_c(String answear_c) { - this.answear_c = answear_c; - } - - public String getAnswear_d() { - return answear_d; - } - - public void setAnswear_d(String answear_d) { - this.answear_d = answear_d; - } - - public String getAnswear_e() { - return answear_e; - } - - public void setAnswear_e(String answear_e) { - this.answear_e = answear_e; - } - - public String getAnswear_f() { - return answear_f; - } - - public void setAnswear_f(String answear_f) { - this.answear_f = answear_f; - } -} diff --git a/src/main/java/com/example/init/models/QuestionForm.java b/src/main/java/com/example/init/models/QuestionForm.java new file mode 100644 index 0000000..33624c7 --- /dev/null +++ b/src/main/java/com/example/init/models/QuestionForm.java @@ -0,0 +1,19 @@ +package com.example.init.models; + +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public class QuestionForm { + + private List questions; + + public List getQuestions() { + return questions; + } + + public void setQuestions(List questions) { + this.questions = questions; + } +} diff --git a/src/main/java/com/example/init/models/Quiz.java b/src/main/java/com/example/init/models/Quiz.java index b26c376..d67af19 100644 --- a/src/main/java/com/example/init/models/Quiz.java +++ b/src/main/java/com/example/init/models/Quiz.java @@ -55,8 +55,10 @@ public static class Answers{ public String answer_d; public String answer_e; public String answer_f; + } + public static class CorrectAnswers{ public String answer_a_correct; public String answer_b_correct; @@ -66,29 +68,36 @@ public static class CorrectAnswers{ public String answer_f_correct; } + private static class Tag{ public String name; } - - public int getAnswers() { - int correct = 0; - if (answers.answer_a == (correct_answer)){ - correct++; -} - else if (correct_answer.equals(answers.answer_b) ){ - correct++; - }else if (correct_answer.equals(answers.answer_c)) { - correct++; - }else if (correct_answer .equals(answers.answer_d)) { - correct++; - }else if (correct_answer.equals(answers.answer_e)) { - correct++; - }else if (correct_answer.equals(answers.answer_f)) { - correct++; - } - return correct; - } } + + +// String convertedToString = String.valueOf(answers.answer_a); +//// if (convertedToString.equals(correct_answer)) +// { +//// correct++; +// }else if (correct_answer.equals(answers.answer_b) ){ +// correct++; +// }else if (correct_answer.equals(answers.answer_c)) { +// correct++; +// }else if (correct_answer .equals(answers.answer_d)) { +// correct++; +// }else if (correct_answer.equals(answers.answer_e)) { +// correct++; +// }else if (correct_answer.equals(answers.answer_f)) { +// correct++; +// } +// return correct; +// } + + + + + + diff --git a/src/main/java/com/example/init/models/QuizDto.java b/src/main/java/com/example/init/models/QuizDto.java new file mode 100644 index 0000000..c2b47cd --- /dev/null +++ b/src/main/java/com/example/init/models/QuizDto.java @@ -0,0 +1,145 @@ +package com.example.init.models; + +import org.springframework.stereotype.Component; + +import javax.persistence.*; + +@Component +@Entity +@Table(name = "questions") +public class QuizDto { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + public int quesId; + public String question; + public String answer_a; + public String answer_b; + public String answer_c; + public String answer_d; + public String answer_e; + public String answer_f; + public String correct_answer; + public int chose = -1; + + public Long getId() { + return id; + } + + + public QuizDto(int quesId, String question, String answer_a, String answer_b, String answer_c, String answer_d, String answer_e, String answer_f, String correct_answer, int chose) { + super(); + this.quesId = quesId; + this.question = question; + this.answer_a = answer_a; + this.answer_b = answer_b; + this.answer_c = answer_c; + this.answer_d = answer_d; + this.answer_e = answer_e; + this.answer_f = answer_f; + this.correct_answer = correct_answer; + this.chose = chose; + } + + public QuizDto() { + super(); + } + + public int getQuesId() { + return quesId; + } + + public void setQuesId(int quesId) { + this.quesId = quesId; + } + + public String getQuestion() { + return question; + } + + public void setQuestion(String question) { + this.question = question; + } + + public String getAnswer_a() { + return answer_a; + } + + public void setAnswer_a(String answer_a) { + this.answer_a = answer_a; + } + + public String getAnswer_b() { + return answer_b; + } + + public void setAnswer_b(String answer_b) { + this.answer_b = answer_b; + } + + public String getAnswer_c() { + return answer_c; + } + + public void setAnswer_c(String answer_c) { + this.answer_c = answer_c; + } + + public String getAnswer_d() { + return answer_d; + } + + public void setAnswer_d(String answer_d) { + this.answer_d = answer_d; + } + + public String getAnswer_e() { + return answer_e; + } + + public void setAnswer_e(String answer_e) { + this.answer_e = answer_e; + } + + public String getAnswer_f() { + return answer_f; + } + + public void setAnswer_f(String answer_f) { + this.answer_f = answer_f; + } + + public String getCorrect_answer() { + return correct_answer; + } + + public void setCorrect_answer(String correct_answer) { + this.correct_answer = correct_answer; + } + + public int getChose() { + return chose; + } + + public void setChose(int chose) { + this.chose = chose; + } + + @Override + public String toString() { + return "QuizDto{" + + "id=" + id + + ", quesId=" + quesId + + ", question='" + question + '\'' + + ", answer_a='" + answer_a + '\'' + + ", answer_b='" + answer_b + '\'' + + ", answer_c='" + answer_c + '\'' + + ", answer_d='" + answer_d + '\'' + + ", answer_e='" + answer_e + '\'' + + ", answer_f='" + answer_f + '\'' + + ", correct_answer='" + correct_answer + '\'' + + ", chose=" + chose + + '}'; + } +} diff --git a/src/main/java/com/example/init/models/ResultsQuiz.java b/src/main/java/com/example/init/models/ResultsQuiz.java index d6e6c94..7f89c18 100644 --- a/src/main/java/com/example/init/models/ResultsQuiz.java +++ b/src/main/java/com/example/init/models/ResultsQuiz.java @@ -1,39 +1,44 @@ package com.example.init.models; -import javax.persistence.*; +import org.springframework.stereotype.Component; +import javax.persistence.*; +@Component @Entity @Table(name = "results") public class ResultsQuiz { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) -// @Column(name = "id", nullable = false) private int id; - @ManyToOne - @JoinColumn(name = "username_id") - public ApplicationUser username; - public int totalCorrect; + + public String username; + public int totalCorrect = 0; public ResultsQuiz() { + super(); } - public ResultsQuiz(int id ,ApplicationUser username, int totalCorrect) { + public ResultsQuiz(int id, String username, int totalCorrect) { + super(); this.id = id; this.username = username; - this.totalCorrect = totalCorrect; + this.totalCorrect = totalCorrect; } public int getId() { return id; } + public void setId(int id) { + this.id = id; + } - public ApplicationUser getUsername() { + public String getUsername() { return username; } - public void setUsername(ApplicationUser username) { + public void setUsername(String username) { this.username = username; } @@ -44,11 +49,4 @@ public int getTotalCorrect() { public void setTotalCorrect(int totalCorrect) { this.totalCorrect = totalCorrect; } - // public Long getId() { -// return id; -// } -// -// public void setId(Long id) { -// this.id = id; -// } } diff --git a/src/main/java/com/example/init/repositories/QuestionRepo.java b/src/main/java/com/example/init/repositories/QuestionRepo.java new file mode 100644 index 0000000..26faa13 --- /dev/null +++ b/src/main/java/com/example/init/repositories/QuestionRepo.java @@ -0,0 +1,10 @@ +package com.example.init.repositories; + +import com.example.init.models.QuizDto; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface QuestionRepo extends JpaRepository { + +} \ No newline at end of file diff --git a/src/main/java/com/example/init/repositories/ResultsRepo.java b/src/main/java/com/example/init/repositories/ResultsRepo.java index 3f54bb9..89e4f89 100644 --- a/src/main/java/com/example/init/repositories/ResultsRepo.java +++ b/src/main/java/com/example/init/repositories/ResultsRepo.java @@ -6,4 +6,5 @@ @Repository public interface ResultsRepo extends JpaRepository { + } diff --git a/src/main/java/com/example/init/security/WebSecurityConfig.java b/src/main/java/com/example/init/security/WebSecurityConfig.java index 8b0220b..2ec8dfe 100644 --- a/src/main/java/com/example/init/security/WebSecurityConfig.java +++ b/src/main/java/com/example/init/security/WebSecurityConfig.java @@ -33,7 +33,7 @@ protected void configure(HttpSecurity http) throws Exception { csrf() .disable() .authorizeRequests() - .antMatchers( "/login", "/signup","/","/quiz","/submit") + .antMatchers( "/login", "/signup","/","/quiz","/submit" , "/quiz/v2" , "/courses") .permitAll().antMatchers("/adminpanel") .hasAuthority("ROLE_ADMIN") .anyRequest().authenticated() diff --git a/src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java b/src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java index b3130dd..cc8f784 100644 --- a/src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java +++ b/src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java @@ -1,28 +1,68 @@ -//package com.example.init.serviceQuiz; -// -//import com.example.init.models.Quiz; -//import org.springframework.stereotype.Service; -// -//import java.util.ArrayList; -//import java.util.List; -//@Service -//public class ServiceQuiz { -// -// public List getQustion() { -// -// return new ArrayList(); -// } -// -// public int counter = 0; -// -// public void getAnswersTest() { -// -// int correct = 10; -// -// Quiz quiz = new Quiz(); -// -// -// } -//} -// -// +package com.example.init.serviceQuiz; + +import com.example.init.models.QuestionForm; +import com.example.init.models.Quiz; +import com.example.init.models.QuizDto; +import com.example.init.models.ResultsQuiz; +import com.example.init.repositories.QuestionRepo; +import com.example.init.repositories.ResultsRepo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Sort; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +@Service +public class ServiceQuiz { + + @Autowired + QuizDto quizDto; + + @Autowired + QuestionForm qForm; + @Autowired + QuestionRepo qRepo; + @Autowired + ResultsQuiz resultsQuiz; + @Autowired + ResultsRepo rRepo; + + + public QuestionForm getQuestions() { + List allQues = qRepo.findAll(); + List qList = new ArrayList(); + + Random random = new Random(); + + for (int i = 0; i < 5; i++) { + int rand = random.nextInt(allQues.size()); + qList.add(allQues.get(rand)); + allQues.remove(rand); + } + + qForm.setQuestions(qList); + return qForm; + } + public int getResult(QuestionForm qForm) { + int correct = 0 ; + for (QuizDto q: qForm.getQuestions()) + if (q.getQuesId() == q.getChose()) + correct++; + + return correct; + } + public void saveScore(ResultsQuiz result) { + ResultsQuiz saveResult = new ResultsQuiz(); + saveResult.setUsername(result.getUsername()); + saveResult.setTotalCorrect(result.getTotalCorrect()); + rRepo.save(saveResult); + } + public List getTopScore() { + List sList = rRepo.findAll(Sort.by(Sort.Direction.DESC, "totalCorrect")); + + return sList; + } +} + diff --git a/src/main/resources/templates/quiz.html b/src/main/resources/templates/quiz.html index 6729680..c9a81da 100644 --- a/src/main/resources/templates/quiz.html +++ b/src/main/resources/templates/quiz.html @@ -1,85 +1,113 @@ - - - - - - - - Quiz Page - - - - - - - + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - -
-

Only Select The Correct Answers

- - -
- - -
- -

-
- -
- -

-
- -
- -

-
- - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + + + + + + + + + + + + + + + + + + + + + + + + - - - - -
+ + + + + + + + + + + + + + + + + + + - - - -
+ + + + + - - -
+ + - - - \ No newline at end of file + + + \ No newline at end of file diff --git a/src/main/resources/templates/quiz2.html b/src/main/resources/templates/quiz2.html new file mode 100644 index 0000000..857bfe8 --- /dev/null +++ b/src/main/resources/templates/quiz2.html @@ -0,0 +1,58 @@ + + + + + + + + + + Quiz Page + + + + +
+ +
+

Only Select The Correct Answers

+ +
+
+

+ +
+ +

+
+ +
+ +

+
+ +
+ +

+
+
+ +

+
+ + + + +
+ + +
+
+ + + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/result.html b/src/main/resources/templates/result.html index 84c1bf6..9543103 100644 --- a/src/main/resources/templates/result.html +++ b/src/main/resources/templates/result.html @@ -16,18 +16,18 @@

Your Score

-

+

Total correct answer:  

-

+

Total false answer:  

-

+

diff --git a/src/main/resources/templates/resultsv2.html b/src/main/resources/templates/resultsv2.html new file mode 100644 index 0000000..9073042 --- /dev/null +++ b/src/main/resources/templates/resultsv2.html @@ -0,0 +1,55 @@ + + + + + + + + + + Result Page + + + + +
+ + +
+
+ image + +

Your Score

+ +
+

Participant name:  

+

+
+ +
+

Total correct answer:  

+

+
+ +
+

Total false answer:  

+

+
+ +
+ + +
+
+
+ + +
+ + + + + \ No newline at end of file diff --git a/src/main/resources/templates/scoreboard.html b/src/main/resources/templates/scoreboard.html new file mode 100644 index 0000000..cf161e6 --- /dev/null +++ b/src/main/resources/templates/scoreboard.html @@ -0,0 +1,40 @@ + + + + + + + + + + Score Board + + + + +
+ + +
+

Top Scores Of All Time

+ + + + + + + + + + +
NameScore
+
+ + +
+ + + + + \ No newline at end of file From ddb8ef97b9f05ba36d5e5223bfdcb3c73f1213e3 Mon Sep 17 00:00:00 2001 From: khair eldeen zakaria shkokany Date: Wed, 27 Oct 2021 01:50:13 +0300 Subject: [PATCH 5/8] hope we are done 1 --- .../example/init/controllers/QuizCont.java | 20 ++--- src/main/resources/application.properties | 2 +- src/main/resources/templates/quiz2.html | 76 ++++++++----------- src/main/resources/templates/scoreboard.html | 5 -- 4 files changed, 41 insertions(+), 62 deletions(-) diff --git a/src/main/java/com/example/init/controllers/QuizCont.java b/src/main/java/com/example/init/controllers/QuizCont.java index 9caeec4..e5b92b2 100644 --- a/src/main/java/com/example/init/controllers/QuizCont.java +++ b/src/main/java/com/example/init/controllers/QuizCont.java @@ -41,7 +41,7 @@ public String getQuiz(Model model) throws Exception { Type jsonCasting = new TypeToken>() { }.getType(); List jsonList = gson.fromJson(data, jsonCasting); - model.addAttribute("quiz", jsonList); + model.addAttribute("qForm", jsonList); System.out.println(jsonList); System.out.println("----------------------"); String numbersJson = gson.toJson(jsonList); @@ -49,15 +49,15 @@ public String getQuiz(Model model) throws Exception { return "quiz2"; } - @PostMapping("/quiz") - public String quiz(@RequestParam String username, Model m, RedirectAttributes ra) { - if(username.equals("")) { - ra.addFlashAttribute("warning", "You must enter your name"); - return "redirect:/"; - } - - submitted = false; - result.setUsername(username); + @PostMapping("/quiz/v2") + public String quiz( Model m, RedirectAttributes ra) { +// if(username.equals("")) { +// ra.addFlashAttribute("warning", "You must enter your name"); +// return "redirect:/"; +// } +// +// submitted = false; +// result.setUsername(username); QuestionForm qForm = qService.getQuestions(); m.addAttribute("qForm", qForm); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index c701635..1d00730 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -4,7 +4,7 @@ spring.datasource.url=jdbc:postgresql://localhost:5432/coders spring.datasource.username=postgres spring.datasource.password=12345 -spring.jpa.hibernate.ddl-auto=update +spring.jpa.hibernate.ddl-auto=create-drop spring.datasource.initialization-mode=always # #spring.datasource.url=jdbc:h2:mem:testdb diff --git a/src/main/resources/templates/quiz2.html b/src/main/resources/templates/quiz2.html index 857bfe8..10c76e6 100644 --- a/src/main/resources/templates/quiz2.html +++ b/src/main/resources/templates/quiz2.html @@ -1,58 +1,42 @@ - + - - - Quiz Page - -
- -
-

Only Select The Correct Answers

- -
-
-

- -
- -

-
- -
- -

-
- -
- -

-
-
- -

-
- - - - + +
+

+
+ +

- - - -
- - - - - +
+ +

+
+
+ +

+
+
+ +

+
+
+ +

+
+
+ +

+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/scoreboard.html b/src/main/resources/templates/scoreboard.html index cf161e6..250eeac 100644 --- a/src/main/resources/templates/scoreboard.html +++ b/src/main/resources/templates/scoreboard.html @@ -5,17 +5,14 @@ - Score Board -
-

Top Scores Of All Time

@@ -32,9 +29,7 @@

Top Scores Of All Time

-
- \ No newline at end of file From 9043ab8fcb1d4253818e1841955014655599bdd1 Mon Sep 17 00:00:00 2001 From: khair eldeen zakaria shkokany Date: Wed, 27 Oct 2021 02:04:38 +0300 Subject: [PATCH 6/8] hope we are done 2 --- .../example/init/controllers/QuizCont.java | 37 ++++++++++--------- .../example/init/serviceQuiz/ServiceQuiz.java | 19 +++++++++- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/example/init/controllers/QuizCont.java b/src/main/java/com/example/init/controllers/QuizCont.java index e5b92b2..a97c2c3 100644 --- a/src/main/java/com/example/init/controllers/QuizCont.java +++ b/src/main/java/com/example/init/controllers/QuizCont.java @@ -17,6 +17,7 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes; import org.springframework.web.servlet.view.RedirectView; +import java.io.FileNotFoundException; import java.io.FileReader; import java.lang.reflect.Type; import java.security.Principal; @@ -32,25 +33,25 @@ public class QuizCont { Boolean submitted = false; - @GetMapping("/quiz/v2") - public String getQuiz(Model model) throws Exception { - - Gson gson = new Gson(); - - FileReader data = new FileReader("C:\\Users\\STUDENT\\Desktop\\Spring\\Project\\init\\src\\main\\java\\com\\example\\init\\controllers\\quiz.json"); - Type jsonCasting = new TypeToken>() { - }.getType(); - List jsonList = gson.fromJson(data, jsonCasting); - model.addAttribute("qForm", jsonList); - System.out.println(jsonList); - System.out.println("----------------------"); - String numbersJson = gson.toJson(jsonList); - System.out.println(numbersJson); - return "quiz2"; - } +// @GetMapping("/quiz/v2") +// public String getQuiz(Model model) throws Exception { +// +//// Gson gson = new Gson(); +//// +//// FileReader data = new FileReader("C:\\Users\\STUDENT\\Desktop\\Spring\\Project\\init\\src\\main\\java\\com\\example\\init\\controllers\\quiz.json"); +//// Type jsonCasting = new TypeToken>() { +//// }.getType(); +//// List jsonList = gson.fromJson(data, jsonCasting); +//// model.addAttribute("qForm", jsonList); +//// System.out.println(jsonList); +//// System.out.println("----------------------"); +//// String numbersJson = gson.toJson(jsonList); +//// System.out.println(numbersJson); +//// return "quiz2"; +// } @PostMapping("/quiz/v2") - public String quiz( Model m, RedirectAttributes ra) { + public String quiz( Model m, RedirectAttributes ra) throws FileNotFoundException { // if(username.equals("")) { // ra.addFlashAttribute("warning", "You must enter your name"); // return "redirect:/"; @@ -61,7 +62,7 @@ public String quiz( Model m, RedirectAttributes ra) { QuestionForm qForm = qService.getQuestions(); m.addAttribute("qForm", qForm); - + System.out.println(qForm); return "quiz2.html"; } @PostMapping("/submit") diff --git a/src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java b/src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java index cc8f784..6582904 100644 --- a/src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java +++ b/src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java @@ -6,10 +6,15 @@ import com.example.init.models.ResultsQuiz; import com.example.init.repositories.QuestionRepo; import com.example.init.repositories.ResultsRepo; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; import java.util.Random; @@ -30,8 +35,18 @@ public class ServiceQuiz { ResultsRepo rRepo; - public QuestionForm getQuestions() { - List allQues = qRepo.findAll(); + public QuestionForm getQuestions() throws FileNotFoundException { +// List allQues = qRepo.findAll(); + Gson gson = new Gson(); + + FileReader data = new FileReader("C:\\Users\\STUDENT\\Desktop\\Spring\\Project\\init\\src\\main\\java\\com\\example\\init\\controllers\\quiz.json"); + Type jsonCasting = new TypeToken>() { + }.getType(); + List allQues = gson.fromJson(data, jsonCasting); + System.out.println(allQues); + System.out.println("----------------------"); + String numbersJson = gson.toJson(allQues); + System.out.println(numbersJson); List qList = new ArrayList(); Random random = new Random(); From 02e8b9efbc1c2de8a231e71a048ede91dc1d9a9c Mon Sep 17 00:00:00 2001 From: khair eldeen zakaria shkokany Date: Wed, 27 Oct 2021 11:05:24 +0300 Subject: [PATCH 7/8] hope we are done 3 --- .../com/example/init/controllers/Home.java | 26 ++++++------ .../example/init/controllers/QuizCont.java | 25 +++++++++--- .../example/init/models/ApplicationUser.java | 4 ++ .../java/com/example/init/models/QuizDto.java | 25 +++++++----- .../com/example/init/models/ResultsQuiz.java | 16 ++++---- .../example/init/serviceQuiz/ServiceQuiz.java | 3 +- src/main/resources/templates/footer.html | 18 +++++++++ src/main/resources/templates/index.html | 40 +++++++++++++++++++ src/main/resources/templates/navbar.html | 35 ++++++++++++++++ src/main/resources/templates/quiz2.html | 4 +- src/main/resources/templates/resultsv2.html | 4 +- 11 files changed, 160 insertions(+), 40 deletions(-) create mode 100644 src/main/resources/templates/footer.html create mode 100644 src/main/resources/templates/index.html create mode 100644 src/main/resources/templates/navbar.html diff --git a/src/main/java/com/example/init/controllers/Home.java b/src/main/java/com/example/init/controllers/Home.java index 6adc840..74be6c9 100644 --- a/src/main/java/com/example/init/controllers/Home.java +++ b/src/main/java/com/example/init/controllers/Home.java @@ -1,13 +1,13 @@ -package com.example.init.controllers; - -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.GetMapping; - -@Controller -public class Home { - - @GetMapping("/") - public String home() { - return "home"; - } -} +//package com.example.init.controllers; +// +//import org.springframework.stereotype.Controller; +//import org.springframework.web.bind.annotation.GetMapping; +// +//@Controller +//public class Home { +// +// @GetMapping("/") +// public String home() { +// return "home"; +// } +//} diff --git a/src/main/java/com/example/init/controllers/QuizCont.java b/src/main/java/com/example/init/controllers/QuizCont.java index a97c2c3..e6848c7 100644 --- a/src/main/java/com/example/init/controllers/QuizCont.java +++ b/src/main/java/com/example/init/controllers/QuizCont.java @@ -1,8 +1,10 @@ package com.example.init.controllers; +import com.example.init.models.ApplicationUser; import com.example.init.models.QuestionForm; import com.example.init.models.QuizDto; import com.example.init.models.ResultsQuiz; +import com.example.init.repositories.QuestionRepo; import com.example.init.serviceQuiz.ServiceQuiz; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; @@ -30,7 +32,8 @@ public class QuizCont { ResultsQuiz result; @Autowired ServiceQuiz qService; - + @Autowired + QuestionRepo questionRepo; Boolean submitted = false; // @GetMapping("/quiz/v2") @@ -49,18 +52,28 @@ public class QuizCont { //// System.out.println(numbersJson); //// return "quiz2"; // } +@ModelAttribute("result") +public ResultsQuiz getResult() { + return result; +} + @GetMapping("/") + public String home() { + return "index.html"; + } + @PostMapping("/quiz/v2") - public String quiz( Model m, RedirectAttributes ra) throws FileNotFoundException { -// if(username.equals("")) { + public String quiz(Model m, RedirectAttributes ra) throws FileNotFoundException { +// if(user.equals("")) { // ra.addFlashAttribute("warning", "You must enter your name"); // return "redirect:/"; // } // // submitted = false; -// result.setUsername(username); - +// +// result.setUser(user); QuestionForm qForm = qService.getQuestions(); +// questionRepo.save(qForm); m.addAttribute("qForm", qForm); System.out.println(qForm); return "quiz2.html"; @@ -75,7 +88,7 @@ public String submit(@ModelAttribute QuestionForm qForm, Model m) { - return "result.html"; + return "resultsv2.html"; } diff --git a/src/main/java/com/example/init/models/ApplicationUser.java b/src/main/java/com/example/init/models/ApplicationUser.java index a26537a..16b9fe4 100644 --- a/src/main/java/com/example/init/models/ApplicationUser.java +++ b/src/main/java/com/example/init/models/ApplicationUser.java @@ -29,6 +29,10 @@ public class ApplicationUser implements UserDetails { @OneToMany(mappedBy = "author" , fetch = FetchType.EAGER) private List codes ; + + @OneToOne + private ResultsQuiz quiz; + public ApplicationUser() { } diff --git a/src/main/java/com/example/init/models/QuizDto.java b/src/main/java/com/example/init/models/QuizDto.java index c2b47cd..51bfbac 100644 --- a/src/main/java/com/example/init/models/QuizDto.java +++ b/src/main/java/com/example/init/models/QuizDto.java @@ -10,9 +10,9 @@ public class QuizDto { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; +// private Long id; - public int quesId; + public Long quesId; public String question; public String answer_a; public String answer_b; @@ -23,12 +23,20 @@ public class QuizDto { public String correct_answer; public int chose = -1; - public Long getId() { - return id; + @OneToOne(orphanRemoval = true) + @JoinColumn + private ApplicationUser application_user_id; + + public ApplicationUser getApplication_user_id() { + return application_user_id; + } + + public void setApplication_user_id(ApplicationUser application_user_id) { + this.application_user_id = application_user_id; } - public QuizDto(int quesId, String question, String answer_a, String answer_b, String answer_c, String answer_d, String answer_e, String answer_f, String correct_answer, int chose) { + public QuizDto(Long quesId, String question, String answer_a, String answer_b, String answer_c, String answer_d, String answer_e, String answer_f, String correct_answer, int chose) { super(); this.quesId = quesId; this.question = question; @@ -46,11 +54,11 @@ public QuizDto() { super(); } - public int getQuesId() { + public Long getQuesId() { return quesId; } - public void setQuesId(int quesId) { + public void setQuesId(Long quesId) { this.quesId = quesId; } @@ -129,8 +137,7 @@ public void setChose(int chose) { @Override public String toString() { return "QuizDto{" + - "id=" + id + - ", quesId=" + quesId + + "quesId=" + quesId + ", question='" + question + '\'' + ", answer_a='" + answer_a + '\'' + ", answer_b='" + answer_b + '\'' + diff --git a/src/main/java/com/example/init/models/ResultsQuiz.java b/src/main/java/com/example/init/models/ResultsQuiz.java index 7f89c18..de4cfd8 100644 --- a/src/main/java/com/example/init/models/ResultsQuiz.java +++ b/src/main/java/com/example/init/models/ResultsQuiz.java @@ -12,17 +12,19 @@ public class ResultsQuiz { @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; - public String username; + @OneToOne + @JoinColumn(name = "username_id") + public ApplicationUser user; public int totalCorrect = 0; public ResultsQuiz() { super(); } - public ResultsQuiz(int id, String username, int totalCorrect) { + public ResultsQuiz(int id, ApplicationUser username, int totalCorrect) { super(); this.id = id; - this.username = username; + this.user = user; this.totalCorrect = totalCorrect; } @@ -34,12 +36,12 @@ public void setId(int id) { this.id = id; } - public String getUsername() { - return username; + public ApplicationUser getUser() { + return user; } - public void setUsername(String username) { - this.username = username; + public void setUser(ApplicationUser user) { + this.user = user; } public int getTotalCorrect() { diff --git a/src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java b/src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java index 6582904..2ab564a 100644 --- a/src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java +++ b/src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java @@ -49,6 +49,7 @@ public QuestionForm getQuestions() throws FileNotFoundException { System.out.println(numbersJson); List qList = new ArrayList(); + Random random = new Random(); for (int i = 0; i < 5; i++) { @@ -70,7 +71,7 @@ public int getResult(QuestionForm qForm) { } public void saveScore(ResultsQuiz result) { ResultsQuiz saveResult = new ResultsQuiz(); - saveResult.setUsername(result.getUsername()); + saveResult.setUser(result.getUser()); saveResult.setTotalCorrect(result.getTotalCorrect()); rRepo.save(saveResult); } diff --git a/src/main/resources/templates/footer.html b/src/main/resources/templates/footer.html new file mode 100644 index 0000000..c4528f5 --- /dev/null +++ b/src/main/resources/templates/footer.html @@ -0,0 +1,18 @@ + + + + + + + + + +
+

© Dev Rezaur

+
+ + + + + \ 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..6d27d78 --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,40 @@ + + + + + + + + + + Home Page + + + + +
+ + +
+
+ image +
+
+

Spring Boot Quiz App

+
+

Enter Your Name

+ +

+ +
+
+
+ + +
+ + + + + \ No newline at end of file diff --git a/src/main/resources/templates/navbar.html b/src/main/resources/templates/navbar.html new file mode 100644 index 0000000..d3dcf3d --- /dev/null +++ b/src/main/resources/templates/navbar.html @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/quiz2.html b/src/main/resources/templates/quiz2.html index 10c76e6..1b59e9a 100644 --- a/src/main/resources/templates/quiz2.html +++ b/src/main/resources/templates/quiz2.html @@ -8,8 +8,8 @@ -
-
+ +

diff --git a/src/main/resources/templates/resultsv2.html b/src/main/resources/templates/resultsv2.html index 9073042..ca38514 100644 --- a/src/main/resources/templates/resultsv2.html +++ b/src/main/resources/templates/resultsv2.html @@ -24,8 +24,8 @@

Your Score

Participant name:  

-

-
+ +

Total correct answer:  

From 91fb716f4ded719b8389c0ab0325a7de7b555977 Mon Sep 17 00:00:00 2001 From: khair eldeen zakaria shkokany Date: Thu, 28 Oct 2021 12:03:46 +0300 Subject: [PATCH 8/8] final steps in quiz finally --- .../example/init/controllers/QuizCont.java | 2 +- src/main/resources/quiz.json | 50 +++++ src/main/resources/templates/navbar.html | 4 +- src/main/resources/templates/quiz.html | 203 ++++++++---------- .../templates/{index.html => quizmenu.html} | 0 src/main/resources/templates/resultsv2.html | 15 +- 6 files changed, 147 insertions(+), 127 deletions(-) create mode 100644 src/main/resources/quiz.json rename src/main/resources/templates/{index.html => quizmenu.html} (100%) diff --git a/src/main/java/com/example/init/controllers/QuizCont.java b/src/main/java/com/example/init/controllers/QuizCont.java index e6848c7..87998b3 100644 --- a/src/main/java/com/example/init/controllers/QuizCont.java +++ b/src/main/java/com/example/init/controllers/QuizCont.java @@ -58,7 +58,7 @@ public ResultsQuiz getResult() { } @GetMapping("/") public String home() { - return "index.html"; + return "quizmenu.html"; } diff --git a/src/main/resources/quiz.json b/src/main/resources/quiz.json new file mode 100644 index 0000000..d45c8bc --- /dev/null +++ b/src/main/resources/quiz.json @@ -0,0 +1,50 @@ +[ + { + "quesId": 1, + "question": "Which are the three looping constructs provided by shell", + "answer_a": "while", + "answer_b": "for", + "answer_c": "until", + "answer_d": "foreach", + "correct_answer":"while" +}, + { + "quesId": 2, + "question": "What are the functions of Replication controller?", + "answer_a": "It is responsible to control and administer the lifecycle of the pod.", + "answer_b": "It helps the user to check the running status of the pod", + "answer_c": "It is responsible to monitor and verify whether the allowed number of pod replicas were running", + "answer_d": "It is responsible for routing the ingress traffic", + "correct_answer": "It is responsible to control and administer the lifecycle of the pod." + }, + { + "quesId": 3, + "question": "Which command can be used by the administrator to bring the system into single user mode?", + "answer_a": "init s", + "answer_b": "single", + "answer_c": "init", + "answer_d": "single s", + "correct_answer": "init s" + + }, + { + "quesId": 4, + "question": "What is the Container Runtime?", + "answer_a": "Software that is responsible for running containers", + "answer_b": "An API object that manages a replicated application", + "answer_c": "Stored instance of a Container that holds a set of software needed to run an application", + "answer_d": "A command line tool for communicating with a Kubernetes API server", + "correct_answer": "Software that is responsible for running containers" + + }, + { + "quesId": 5, + "question": "Which command can be used to find a word from a text file?", + "answer_a": "cut", + "answer_b": "sort", + "answer_c": "find", + "answer_d": "grep", + "correct_answer": "grep" + } + +] diff --git a/src/main/resources/templates/navbar.html b/src/main/resources/templates/navbar.html index d3dcf3d..66ae50b 100644 --- a/src/main/resources/templates/navbar.html +++ b/src/main/resources/templates/navbar.html @@ -11,7 +11,7 @@