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

Commit 9d9f9ab

Browse files
committed
switched in our code
1 parent 32091a2 commit 9d9f9ab

File tree

124 files changed

+793
-8321
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+793
-8321
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.greglturnquist;
2+
3+
import com.greglturnquist.model.form.Form;
4+
import com.greglturnquist.model.form.Question;
5+
import com.greglturnquist.repository.FormRepository;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.boot.CommandLineRunner;
9+
import org.springframework.boot.SpringApplication;
10+
import org.springframework.boot.autoconfigure.SpringBootApplication;
11+
import org.springframework.context.annotation.Bean;
12+
13+
@SpringBootApplication
14+
public class FormApplication {
15+
16+
private static final Logger log = LoggerFactory.getLogger(FormApplication.class);
17+
18+
public static void main(String[] args) {
19+
SpringApplication.run(FormApplication.class, args);
20+
}
21+
22+
@Bean
23+
public CommandLineRunner run(FormRepository repository) {
24+
return (args) -> {
25+
Form form = new Form();
26+
27+
form.addQuestion(new Question());
28+
form.addQuestion(new Question("what day is it?"));
29+
form.addQuestion(new Question("Do you enjoy sports?"));
30+
31+
repository.save(form);
32+
33+
log.info("Created new Form");
34+
35+
36+
for (Form f : repository.findAll()) {
37+
log.info(f.getId().toString());
38+
log.info(f.toString());
39+
}
40+
};
41+
}
42+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.greglturnquist.controller;
2+
3+
4+
import com.greglturnquist.repository.AnswerRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Controller;
7+
8+
@Controller
9+
public class AnswerController {
10+
11+
@Autowired
12+
private AnswerRepository answerRepository;
13+
14+
public AnswerController(AnswerRepository answerRepository){
15+
this.answerRepository = answerRepository;
16+
}
17+
18+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.greglturnquist.controller;
2+
3+
import com.greglturnquist.model.form.Form;
4+
import com.greglturnquist.model.form.Question;
5+
import com.greglturnquist.repository.FormRepository;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
import java.util.*;
9+
10+
@RestController
11+
public class WebController {
12+
13+
private final FormRepository repository;
14+
15+
public WebController(FormRepository repository) {
16+
this.repository = repository;
17+
}
18+
19+
@PostMapping("/form")
20+
public Form createForm(@RequestBody(required = false) List<Question> questionList) {
21+
Form a = new Form();
22+
if(questionList != null) {
23+
for(Question b : questionList) {
24+
a.addQuestion(b);
25+
}
26+
}
27+
repository.save(a);
28+
29+
return a;
30+
}
31+
32+
@GetMapping("/form/{id}")
33+
public Form getForm(@PathVariable String id) {
34+
Optional<Form> response = repository.findById(UUID.fromString(id));
35+
return response.orElse(null);
36+
}
37+
38+
@GetMapping("/form")
39+
public Form getAllForms() {
40+
Iterable<Form> response = repository.findAll();
41+
List<Form> temp = new ArrayList<Form>();
42+
for (Form f : response) {
43+
temp.add(f);
44+
}
45+
//returning first index just for the first milestone
46+
return temp.get(0);
47+
}
48+
49+
@PostMapping("/form/{id}")
50+
public PrimitiveResponse<Boolean> createQuestion(@PathVariable String id, @RequestBody Question question) {
51+
Optional<Form> response = repository.findById(UUID.fromString(id));
52+
if(response.isEmpty()) {
53+
return new PrimitiveResponse<>("success", false);
54+
}
55+
Form book = response.get();
56+
book.addQuestion(question);
57+
repository.save(book);
58+
return new PrimitiveResponse<>("success", true);
59+
}
60+
61+
@DeleteMapping("/form/{id}/question/{questionId}")
62+
public PrimitiveResponse<Boolean> deleteQuestion(@PathVariable String id, @PathVariable String questionId) {
63+
Optional<Form> response = repository.findById(UUID.fromString(id));
64+
if(response.isEmpty()) {
65+
return new PrimitiveResponse<>("success", false);
66+
}
67+
Form book = response.get();
68+
if(book.removeQuestion(UUID.fromString(questionId))) {
69+
repository.save(book);
70+
return new PrimitiveResponse<>("success", true);
71+
}
72+
return new PrimitiveResponse<>("success", false);
73+
}
74+
75+
}
76+
77+
78+
class PrimitiveResponse<T> {
79+
80+
private final Map<String, T> body;
81+
82+
public PrimitiveResponse(String key, T value) {
83+
this.body = new HashMap<>(1);
84+
this.body.put(key, value);
85+
}
86+
87+
public Map<String, T> getBody() {
88+
return this.body;
89+
}
90+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
package com.greglturnquist.controller;
2+
3+
import com.greglturnquist.model.Answer;
4+
import com.greglturnquist.model.form.Form;
5+
import com.greglturnquist.model.form.Question;
6+
import com.greglturnquist.model.form.data.AnswerAndQuestion;
7+
import com.greglturnquist.model.form.data.DataForm;
8+
import com.greglturnquist.model.form.data.QuestionForm;
9+
import com.greglturnquist.repository.FormRepository;
10+
import com.fasterxml.jackson.core.JsonProcessingException;
11+
import org.springframework.stereotype.Controller;
12+
import org.springframework.ui.Model;
13+
import org.springframework.web.bind.annotation.GetMapping;
14+
import org.springframework.web.bind.annotation.ModelAttribute;
15+
import org.springframework.web.bind.annotation.PostMapping;
16+
import org.springframework.web.bind.annotation.RequestBody;
17+
18+
import java.io.UnsupportedEncodingException;
19+
import java.net.URLDecoder;
20+
import java.nio.charset.StandardCharsets;
21+
import java.util.*;
22+
23+
@Controller
24+
public class WebUIController {
25+
26+
private final FormRepository repository;
27+
private static boolean closed = true;
28+
29+
public static String decodeValue(String value) {
30+
try {
31+
return URLDecoder.decode(value, StandardCharsets.UTF_8.toString());
32+
} catch (UnsupportedEncodingException ex) {
33+
throw new RuntimeException(ex.getCause());
34+
}
35+
}
36+
37+
public WebUIController(FormRepository repository) {
38+
this.repository = repository;
39+
}
40+
41+
@GetMapping("/ui")
42+
public String selectForm(Model model) {
43+
model.addAttribute("dataForm", new DataForm());
44+
return "dataForm";
45+
}
46+
47+
@PostMapping("/ui")
48+
public String getForm(@ModelAttribute DataForm dataForm, Model model) {
49+
Optional<Form> result = repository.findById(UUID.fromString(dataForm.getContent()));
50+
51+
model.addAttribute("Form", result.orElse(null));
52+
return "result";
53+
}
54+
55+
@GetMapping("/create")
56+
public String createForm(Model model) {
57+
Form form = new Form();
58+
repository.save(form);
59+
60+
model.addAttribute("Form", form);
61+
return "result";
62+
}
63+
@PostMapping(value = "/submission")
64+
public String submitForm(@RequestBody String body) throws JsonProcessingException {
65+
System.out.println(body);
66+
String decodedValue = decodeValue(body);
67+
decodedValue = decodedValue.substring(0, decodedValue.length() - 1);
68+
System.out.println(decodedValue);
69+
List<String> answerList = Arrays.asList(decodedValue.split(","));
70+
for(String s : answerList){
71+
System.out.println(s);
72+
}
73+
System.out.println(answerList);
74+
Iterable<Form> response = repository.findAll();
75+
List<Form> temp = new ArrayList<Form>();
76+
for (Form f : response) {
77+
temp.add(f);
78+
}
79+
Form f = temp.get(0);
80+
//
81+
82+
List<Question> questionList = f.getQuestions();
83+
// for(Question q: questionList){
84+
// System.out.println(q);
85+
// Answer answer = new Answer("hey");
86+
// q.addAnswerList(answer);
87+
// answer.setQuestion(q);
88+
// }
89+
for(int i = 0; i < questionList.size(); i++){
90+
Answer answer = new Answer(answerList.get(i));
91+
questionList.get(i).addAnswerList(answer);
92+
answer.setQuestion(questionList.get(i));
93+
}
94+
repository.save(f);
95+
return "Thanks";
96+
}
97+
98+
@GetMapping("/question")
99+
public String inputQuestionData(Model model) {
100+
model.addAttribute("questionForm", new QuestionForm());
101+
return "question";
102+
}
103+
104+
@PostMapping("/question")
105+
public String createQuestionInfo(@ModelAttribute QuestionForm questionForm, Model model) {
106+
Optional<Form> result = repository.findById(UUID.fromString(questionForm.getId()));
107+
108+
result.ifPresent(form -> {
109+
form.addQuestion(new Question(questionForm.getValue()));
110+
repository.save(form);
111+
});
112+
113+
model.addAttribute("questionForm", result.get());
114+
115+
return "result";
116+
}
117+
118+
@GetMapping("/admin")
119+
public String displayAdmin() {
120+
return "admin";
121+
}
122+
123+
124+
@GetMapping("/survey")
125+
public String displaySurvey(Model model) {
126+
if (closed != true){
127+
return closeForm(model);
128+
}
129+
model.addAttribute("closed", closed);
130+
return "survey";
131+
}
132+
133+
@GetMapping("/survey2")
134+
public String displaySurvey2(Model model) {
135+
// AnswerList answerList = new AnswerList();
136+
// answerList.setQuestion("hello");
137+
List<AnswerAndQuestion> answerList = new ArrayList<>();
138+
Form form = new Form();
139+
form.addQuestion(new Question());
140+
form.addQuestion(new Question("what day is it?"));
141+
form.addQuestion(new Question("Do you enjoy sports?"));
142+
List<Question> questionList = form.getQuestions();
143+
for(Question question : questionList){
144+
AnswerAndQuestion answer = new AnswerAndQuestion();
145+
answer.setQuestion(question.getValue());
146+
answerList.add(answer);
147+
}
148+
// model.addAttribute("Form", form);
149+
model.addAttribute("answerList", answerList);
150+
151+
return "survey2";
152+
}
153+
154+
155+
@GetMapping("/controlPanel")
156+
public String displayControlPanel() {
157+
return "controlPanel";
158+
}
159+
160+
@GetMapping("/closeForm")
161+
public String closeForm(Model model) {
162+
this.closed = false;
163+
// Iterable<Form> response = repository.findAll();
164+
// List<Form> temp = new ArrayList<Form>();
165+
// for (Form f : response) {
166+
// temp.add(f);
167+
// }
168+
// Form form = temp.get(0);
169+
// List<Question> questionList = form.getQuestions();
170+
// model.addAttribute("questions", questionList);
171+
return "answerResults";
172+
}
173+
174+
}

0 commit comments

Comments
 (0)