Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/com/example/init/controllers/Home.java
Original file line number Diff line number Diff line change
@@ -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";
// }
//}
104 changes: 104 additions & 0 deletions src/main/java/com/example/init/controllers/QuizCont.java
Original file line number Diff line number Diff line change
@@ -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<List<QuizDto>>() {
//// }.getType();
//// List<QuizDto> 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<ResultsQuiz> sList = qService.getTopScore();
m.addAttribute("sList", sList);

return "scoreboard.html";
}


}
171 changes: 130 additions & 41 deletions src/main/java/com/example/init/controllers/QuizController.java
Original file line number Diff line number Diff line change
@@ -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<List<Quiz>>(){}.getType();
List<Quiz> 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<List<Quiz>>(){}.getType();
// List<Quiz> 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");
//// }
//
//}
50 changes: 50 additions & 0 deletions src/main/java/com/example/init/controllers/quiz.json
Original file line number Diff line number Diff line change
@@ -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"
}

]
4 changes: 4 additions & 0 deletions src/main/java/com/example/init/models/ApplicationUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class ApplicationUser implements UserDetails {

@OneToMany(mappedBy = "author" , fetch = FetchType.EAGER)
private List<Code> codes ;

@OneToOne
private ResultsQuiz quiz;

public ApplicationUser() {
}

Expand Down
Loading