diff --git a/build.gradle b/build.gradle index 8d03cce..41d4cf6 100644 --- a/build.gradle +++ b/build.gradle @@ -19,11 +19,16 @@ 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' + 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' + implementation 'com.googlecode.json-simple:json-simple:1.1.1' } test { 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 new file mode 100644 index 0000000..87998b3 --- /dev/null +++ b/src/main/java/com/example/init/controllers/QuizCont.java @@ -0,0 +1,104 @@ +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; +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.FileNotFoundException; +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; + @Autowired + QuestionRepo questionRepo; + 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"; +// } +@ModelAttribute("result") +public ResultsQuiz getResult() { + return result; +} + @GetMapping("/") + public String home() { + return "quizmenu.html"; + } + + + @PostMapping("/quiz/v2") + 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.setUser(user); + QuestionForm qForm = qService.getQuestions(); +// questionRepo.save(qForm); + m.addAttribute("qForm", qForm); + System.out.println(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 "resultsv2.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 794de6b..ecbc8b0 100644 --- a/src/main/java/com/example/init/controllers/QuizController.java +++ b/src/main/java/com/example/init/controllers/QuizController.java @@ -1,41 +1,130 @@ -package com.example.init.controllers; - -import com.example.init.models.Quiz; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; - -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.util.ArrayList; -import java.util.List; - -@Controller -public class QuizController { - - - - - @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); -// model.addAttribute("answers",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(); +// +// model.addAttribute("quiz" , quiz); +// return "quiz"; +// } +// +// +// @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/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/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 bf4692f..d67af19 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,95 @@ 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 int chose = -1; - 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 , int chose) { + 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; + this.chose = chose; } - public Long getId() { - return id; + public Quiz() { + } -// public String url = "https://quizapi.io/api/v1/questions?apiKey=phkL2Z69NDCrImLlfnLoElQkoF3StoJlQrAeKvkf&limit=5&css"; + public static class Answers{ + 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 Quiz() { } -// public String getAnswers() { -// return answers; -// } - -// public void setAnswers(String answers) { -// this.answers = answers; -// } - public String getQuestion() { - return question; + public 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 void setQuestion(String question) { - this.question = question; + + private static class Tag{ + public String name; } + } + + + + + +// 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..51bfbac --- /dev/null +++ b/src/main/java/com/example/init/models/QuizDto.java @@ -0,0 +1,152 @@ +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 Long 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; + + @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(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; + 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 Long getQuesId() { + return quesId; + } + + public void setQuesId(Long 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{" + + "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 new file mode 100644 index 0000000..de4cfd8 --- /dev/null +++ b/src/main/java/com/example/init/models/ResultsQuiz.java @@ -0,0 +1,54 @@ +package com.example.init.models; + + +import org.springframework.stereotype.Component; + +import javax.persistence.*; +@Component +@Entity +@Table(name = "results") +public class ResultsQuiz { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private int id; + + @OneToOne + @JoinColumn(name = "username_id") + public ApplicationUser user; + public int totalCorrect = 0; + + public ResultsQuiz() { + super(); + } + + public ResultsQuiz(int id, ApplicationUser username, int totalCorrect) { + super(); + this.id = id; + this.user = user; + this.totalCorrect = totalCorrect; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public ApplicationUser getUser() { + return user; + } + + public void setUser(ApplicationUser user) { + this.user = user; + } + + public int getTotalCorrect() { + return totalCorrect; + } + + public void setTotalCorrect(int totalCorrect) { + this.totalCorrect = totalCorrect; + } +} 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 new file mode 100644 index 0000000..89e4f89 --- /dev/null +++ b/src/main/java/com/example/init/repositories/ResultsRepo.java @@ -0,0 +1,10 @@ +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/security/WebSecurityConfig.java b/src/main/java/com/example/init/security/WebSecurityConfig.java index 3b7cd93..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") + .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 new file mode 100644 index 0000000..2ab564a --- /dev/null +++ b/src/main/java/com/example/init/serviceQuiz/ServiceQuiz.java @@ -0,0 +1,84 @@ +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 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; + +@Service +public class ServiceQuiz { + + @Autowired + QuizDto quizDto; + + @Autowired + QuestionForm qForm; + @Autowired + QuestionRepo qRepo; + @Autowired + ResultsQuiz resultsQuiz; + @Autowired + ResultsRepo rRepo; + + + 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(); + + 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.setUser(result.getUser()); + 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/application.properties b/src/main/resources/application.properties index ae3973a..1d00730 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-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.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/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/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/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/navbar.html b/src/main/resources/templates/navbar.html new file mode 100644 index 0000000..66ae50b --- /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/quiz.html b/src/main/resources/templates/quiz.html index 5e39f28..8ed5345 100644 --- a/src/main/resources/templates/quiz.html +++ b/src/main/resources/templates/quiz.html @@ -2,30 +2,88 @@ + + + Quiz Page - - - - - - -
- - - - - - - - - - - -
+
+

Only Select The Correct Answers

+ +
+
+ + + + +

+ +
+ + +

+
+ +
+ + +
+ +
+ +

+
+ +
+ +

+
+ +
+ +

+
+ +
+ +

+ +

+
+ + + + + +
+ + +
+
+
+
+ + + +
+ +
+ diff --git a/src/main/resources/templates/quiz2.html b/src/main/resources/templates/quiz2.html new file mode 100644 index 0000000..1b59e9a --- /dev/null +++ b/src/main/resources/templates/quiz2.html @@ -0,0 +1,42 @@ + + + + + + + Quiz Page + + + +
+
+

+
+ +

+
+
+ +

+
+
+ +

+
+
+ +

+
+
+ +

+
+
+ +

+
+
+ +
+ + \ No newline at end of file diff --git a/src/main/resources/templates/quizmenu.html b/src/main/resources/templates/quizmenu.html new file mode 100644 index 0000000..6d27d78 --- /dev/null +++ b/src/main/resources/templates/quizmenu.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/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 diff --git a/src/main/resources/templates/resultsv2.html b/src/main/resources/templates/resultsv2.html new file mode 100644 index 0000000..155303c --- /dev/null +++ b/src/main/resources/templates/resultsv2.html @@ -0,0 +1,48 @@ + + + + + + + + + + Result Page + + + + +
+ + +
+
+ image + +

Your Score

+ +
+ +
+

Total correct 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..250eeac --- /dev/null +++ b/src/main/resources/templates/scoreboard.html @@ -0,0 +1,35 @@ + + + + + + + + + Score Board + + + +
+ +
+

Top Scores Of All Time

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