1
|
package eu.dnetlib.uoaadmintools.controllers;
|
2
|
|
3
|
import eu.dnetlib.uoaadmintools.entities.Question;
|
4
|
import eu.dnetlib.uoaadmintools.entities.QuestionResponse;
|
5
|
import eu.dnetlib.uoaadmintools.dao.QuestionDAO;
|
6
|
import eu.dnetlib.uoaadmintools.entities.Topic;
|
7
|
import eu.dnetlib.uoaadmintools.dao.TopicDAO;
|
8
|
|
9
|
import org.apache.log4j.Logger;
|
10
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
11
|
import org.springframework.web.bind.annotation.RequestMapping;
|
12
|
import org.springframework.web.bind.annotation.RequestMethod;
|
13
|
import org.springframework.web.bind.annotation.RequestBody;
|
14
|
import org.springframework.web.bind.annotation.PathVariable;
|
15
|
import org.springframework.beans.factory.annotation.Autowired;
|
16
|
import org.springframework.web.bind.annotation.RequestParam;
|
17
|
import org.springframework.web.bind.annotation.RestController;
|
18
|
|
19
|
import java.util.List;
|
20
|
import java.util.ArrayList;
|
21
|
|
22
|
import javax.validation.Valid;
|
23
|
|
24
|
@RestController
|
25
|
@CrossOrigin(origins = "*")
|
26
|
public class QuestionController {
|
27
|
private final Logger log = Logger.getLogger(this.getClass());
|
28
|
|
29
|
@Autowired
|
30
|
private QuestionDAO questionDAO;
|
31
|
|
32
|
@Autowired
|
33
|
private TopicDAO topicDAO;
|
34
|
|
35
|
@RequestMapping(value = "/question", method = RequestMethod.GET)
|
36
|
public List<QuestionResponse> getAllQuestions() {
|
37
|
List<Question> questions = questionDAO.findAll();
|
38
|
List<QuestionResponse> questionResponses = new ArrayList<>();
|
39
|
for (Question question : questions) {
|
40
|
QuestionResponse questionResponse = new QuestionResponse(question);
|
41
|
List<Topic> topics = new ArrayList<>();
|
42
|
for (String topic : question.getTopics()) {
|
43
|
topics.add(topicDAO.findById(topic));
|
44
|
}
|
45
|
questionResponse.setTopics(topics);
|
46
|
questionResponses.add(questionResponse);
|
47
|
}
|
48
|
return questionResponses;
|
49
|
}
|
50
|
|
51
|
@RequestMapping(value = "/question", method = RequestMethod.DELETE)
|
52
|
public void deleteAllQuestions() {
|
53
|
questionDAO.deleteAll();
|
54
|
}
|
55
|
|
56
|
@RequestMapping(value = "/question", method = RequestMethod.POST)
|
57
|
public QuestionResponse insertOrUpdateQuestion(@RequestBody Question question) {
|
58
|
QuestionResponse questionResponse = new QuestionResponse(questionDAO.save(question));
|
59
|
List<Topic> topics = new ArrayList<>();
|
60
|
for (String topic : question.getTopics()) {
|
61
|
topics.add(topicDAO.findById(topic));
|
62
|
}
|
63
|
questionResponse.setTopics(topics);
|
64
|
return questionResponse;
|
65
|
}
|
66
|
|
67
|
@RequestMapping(value = "/question/{id}", method = RequestMethod.GET)
|
68
|
public QuestionResponse getQuestion(@PathVariable(value = "id") String id) {
|
69
|
Question question = questionDAO.findById(id);
|
70
|
QuestionResponse questionResponse = new QuestionResponse(question);
|
71
|
List<Topic> topics = new ArrayList<>();
|
72
|
for (String topic : question.getTopics()) {
|
73
|
topics.add(topicDAO.findById(topic));
|
74
|
}
|
75
|
questionResponse.setTopics(topics);
|
76
|
return questionResponse;
|
77
|
}
|
78
|
|
79
|
@RequestMapping(value = "/question/{id}", method = RequestMethod.DELETE)
|
80
|
public void deleteQuestion(@PathVariable(value = "id") String id) {
|
81
|
questionDAO.delete(id);
|
82
|
}
|
83
|
|
84
|
@RequestMapping(value = "/question/inc/{id}", method = RequestMethod.PUT)
|
85
|
public Question incQuestion(@PathVariable(value = "id") String id) {
|
86
|
Question question = questionDAO.findById(id);
|
87
|
question.increment();
|
88
|
questionDAO.save(question);
|
89
|
return question;
|
90
|
}
|
91
|
|
92
|
@RequestMapping(value = "/question/delete", method = RequestMethod.POST)
|
93
|
public void deleteQuestions(@RequestBody List<String> questions) {
|
94
|
for (String question : questions) {
|
95
|
deleteQuestion(question);
|
96
|
}
|
97
|
}
|
98
|
|
99
|
@RequestMapping(value = "/question/toggle", method = RequestMethod.POST)
|
100
|
public void toggleQuestions(@RequestBody List<String> questions, @RequestParam String status) throws Exception {
|
101
|
for (String id: questions) {
|
102
|
Question question = questionDAO.findById(id);
|
103
|
question.setIsActive(Boolean.parseBoolean(status));
|
104
|
questionDAO.save(question);
|
105
|
}
|
106
|
}
|
107
|
}
|