Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Krishan #138

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
},
"homepage": "https://github.com/spring-guides/tut-react-and-spring-data-rest",
"dependencies": {
"@material-ui/core": "^4.12.3",
"eslint": "^8.11.0",
"react": "^16.5.2",
"react-dom": "^16.5.2",
"rest": "^1.3.1"
Expand Down
42 changes: 42 additions & 0 deletions basic/src/main/java/com/greglturnquist/FormApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.greglturnquist;

import com.greglturnquist.model.form.Form;
import com.greglturnquist.model.form.Question;
import com.greglturnquist.repository.FormRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class FormApplication {

private static final Logger log = LoggerFactory.getLogger(FormApplication.class);

public static void main(String[] args) {
SpringApplication.run(FormApplication.class, args);
}

@Bean
public CommandLineRunner run(FormRepository repository) {
return (args) -> {
Form form = new Form();

form.addQuestion(new Question());
form.addQuestion(new Question("what day is it?"));
form.addQuestion(new Question("Do you enjoy sports?"));

repository.save(form);

log.info("Created new Form");


for (Form f : repository.findAll()) {
log.info(f.getId().toString());
log.info(f.toString());
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.greglturnquist.controller;


import com.greglturnquist.repository.AnswerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class AnswerController {

@Autowired
private AnswerRepository answerRepository;

public AnswerController(AnswerRepository answerRepository){
this.answerRepository = answerRepository;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.greglturnquist.controller;

import com.greglturnquist.model.form.Form;
import com.greglturnquist.model.form.Question;
import com.greglturnquist.repository.FormRepository;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
public class WebController {

private final FormRepository repository;

public WebController(FormRepository repository) {
this.repository = repository;
}

@PostMapping("/form")
public Form createForm(@RequestBody(required = false) List<Question> questionList) {
Form a = new Form();
if(questionList != null) {
for(Question b : questionList) {
a.addQuestion(b);
}
}
repository.save(a);

return a;
}

@GetMapping("/form/{id}")
public Form getForm(@PathVariable String id) {
Optional<Form> response = repository.findById(UUID.fromString(id));
return response.orElse(null);
}

@GetMapping("/form")
public Form getAllForms() {
Iterable<Form> response = repository.findAll();
List<Form> temp = new ArrayList<Form>();
for (Form f : response) {
temp.add(f);
}
//returning first index just for the first milestone
return temp.get(0);
}

@PostMapping("/form/{id}")
public PrimitiveResponse<Boolean> createQuestion(@PathVariable String id, @RequestBody Question question) {
Optional<Form> response = repository.findById(UUID.fromString(id));
if(response.isEmpty()) {
return new PrimitiveResponse<>("success", false);
}
Form book = response.get();
book.addQuestion(question);
repository.save(book);
return new PrimitiveResponse<>("success", true);
}

@DeleteMapping("/form/{id}/question/{questionId}")
public PrimitiveResponse<Boolean> deleteQuestion(@PathVariable String id, @PathVariable String questionId) {
Optional<Form> response = repository.findById(UUID.fromString(id));
if(response.isEmpty()) {
return new PrimitiveResponse<>("success", false);
}
Form book = response.get();
if(book.removeQuestion(UUID.fromString(questionId))) {
repository.save(book);
return new PrimitiveResponse<>("success", true);
}
return new PrimitiveResponse<>("success", false);
}

}


class PrimitiveResponse<T> {

private final Map<String, T> body;

public PrimitiveResponse(String key, T value) {
this.body = new HashMap<>(1);
this.body.put(key, value);
}

public Map<String, T> getBody() {
return this.body;
}
}
174 changes: 174 additions & 0 deletions basic/src/main/java/com/greglturnquist/controller/WebUIController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package com.greglturnquist.controller;

import com.greglturnquist.model.Answer;
import com.greglturnquist.model.form.Form;
import com.greglturnquist.model.form.Question;
import com.greglturnquist.model.form.data.AnswerAndQuestion;
import com.greglturnquist.model.form.data.DataForm;
import com.greglturnquist.model.form.data.QuestionForm;
import com.greglturnquist.repository.FormRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
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.RequestBody;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.*;

@Controller
public class WebUIController {

private final FormRepository repository;
private static boolean closed = true;

public static String decodeValue(String value) {
try {
return URLDecoder.decode(value, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex.getCause());
}
}

public WebUIController(FormRepository repository) {
this.repository = repository;
}

@GetMapping("/ui")
public String selectForm(Model model) {
model.addAttribute("dataForm", new DataForm());
return "dataForm";
}

@PostMapping("/ui")
public String getForm(@ModelAttribute DataForm dataForm, Model model) {
Optional<Form> result = repository.findById(UUID.fromString(dataForm.getContent()));

model.addAttribute("Form", result.orElse(null));
return "result";
}

@GetMapping("/create")
public String createForm(Model model) {
Form form = new Form();
repository.save(form);

model.addAttribute("Form", form);
return "result";
}
@PostMapping(value = "/submission")
public String submitForm(@RequestBody String body) throws JsonProcessingException {
System.out.println(body);
String decodedValue = decodeValue(body);
decodedValue = decodedValue.substring(0, decodedValue.length() - 1);
System.out.println(decodedValue);
List<String> answerList = Arrays.asList(decodedValue.split(","));
for(String s : answerList){
System.out.println(s);
}
System.out.println(answerList);
Iterable<Form> response = repository.findAll();
List<Form> temp = new ArrayList<Form>();
for (Form f : response) {
temp.add(f);
}
Form f = temp.get(0);
//

List<Question> questionList = f.getQuestions();
// for(Question q: questionList){
// System.out.println(q);
// Answer answer = new Answer("hey");
// q.addAnswerList(answer);
// answer.setQuestion(q);
// }
for(int i = 0; i < questionList.size(); i++){
Answer answer = new Answer(answerList.get(i));
questionList.get(i).addAnswerList(answer);
answer.setQuestion(questionList.get(i));
}
repository.save(f);
return "Thanks";
}

@GetMapping("/question")
public String inputQuestionData(Model model) {
model.addAttribute("questionForm", new QuestionForm());
return "question";
}

@PostMapping("/question")
public String createQuestionInfo(@ModelAttribute QuestionForm questionForm, Model model) {
Optional<Form> result = repository.findById(UUID.fromString(questionForm.getId()));

result.ifPresent(form -> {
form.addQuestion(new Question(questionForm.getValue()));
repository.save(form);
});

model.addAttribute("questionForm", result.get());

return "result";
}

@GetMapping("/admin")
public String displayAdmin() {
return "admin";
}


@GetMapping("/survey")
public String displaySurvey(Model model) {
if (closed != true){
return closeForm(model);
}
model.addAttribute("closed", closed);
return "survey";
}

@GetMapping("/survey2")
public String displaySurvey2(Model model) {
// AnswerList answerList = new AnswerList();
// answerList.setQuestion("hello");
List<AnswerAndQuestion> answerList = new ArrayList<>();
Form form = new Form();
form.addQuestion(new Question());
form.addQuestion(new Question("what day is it?"));
form.addQuestion(new Question("Do you enjoy sports?"));
List<Question> questionList = form.getQuestions();
for(Question question : questionList){
AnswerAndQuestion answer = new AnswerAndQuestion();
answer.setQuestion(question.getValue());
answerList.add(answer);
}
// model.addAttribute("Form", form);
model.addAttribute("answerList", answerList);

return "survey2";
}


@GetMapping("/controlPanel")
public String displayControlPanel() {
return "controlPanel";
}

@GetMapping("/closeForm")
public String closeForm(Model model) {
this.closed = false;
// Iterable<Form> response = repository.findAll();
// List<Form> temp = new ArrayList<Form>();
// for (Form f : response) {
// temp.add(f);
// }
// Form form = temp.get(0);
// List<Question> questionList = form.getQuestions();
// model.addAttribute("questions", questionList);
return "answerResults";
}

}
Loading