108 lines
4.1 KiB
Java
108 lines
4.1 KiB
Java
package eu.dnetlib.uoaadmintools.controllers;
|
|
|
|
import eu.dnetlib.uoaadmintools.entities.Question;
|
|
import eu.dnetlib.uoaadmintools.entities.QuestionResponse;
|
|
import eu.dnetlib.uoaadmintools.dao.QuestionDAO;
|
|
import eu.dnetlib.uoaadmintools.entities.Topic;
|
|
import eu.dnetlib.uoaadmintools.dao.TopicDAO;
|
|
|
|
import org.apache.log4j.Logger;
|
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import java.util.List;
|
|
import java.util.ArrayList;
|
|
|
|
import javax.validation.Valid;
|
|
|
|
@RestController
|
|
@CrossOrigin(origins = "*")
|
|
public class QuestionController {
|
|
private final Logger log = Logger.getLogger(this.getClass());
|
|
|
|
@Autowired
|
|
private QuestionDAO questionDAO;
|
|
|
|
@Autowired
|
|
private TopicDAO topicDAO;
|
|
|
|
@RequestMapping(value = "/question", method = RequestMethod.GET)
|
|
public List<QuestionResponse> getAllQuestions() {
|
|
List<Question> questions = questionDAO.findAll();
|
|
List<QuestionResponse> questionResponses = new ArrayList<>();
|
|
for (Question question : questions) {
|
|
QuestionResponse questionResponse = new QuestionResponse(question);
|
|
List<Topic> topics = new ArrayList<>();
|
|
for (String topic : question.getTopics()) {
|
|
topics.add(topicDAO.findById(topic));
|
|
}
|
|
questionResponse.setTopics(topics);
|
|
questionResponses.add(questionResponse);
|
|
}
|
|
return questionResponses;
|
|
}
|
|
|
|
@RequestMapping(value = "/question", method = RequestMethod.DELETE)
|
|
public void deleteAllQuestions() {
|
|
questionDAO.deleteAll();
|
|
}
|
|
|
|
@RequestMapping(value = "/question", method = RequestMethod.POST)
|
|
public QuestionResponse insertOrUpdateQuestion(@RequestBody Question question) {
|
|
QuestionResponse questionResponse = new QuestionResponse(questionDAO.save(question));
|
|
List<Topic> topics = new ArrayList<>();
|
|
for (String topic : question.getTopics()) {
|
|
topics.add(topicDAO.findById(topic));
|
|
}
|
|
questionResponse.setTopics(topics);
|
|
return questionResponse;
|
|
}
|
|
|
|
@RequestMapping(value = "/question/{id}", method = RequestMethod.GET)
|
|
public QuestionResponse getQuestion(@PathVariable(value = "id") String id) {
|
|
Question question = questionDAO.findById(id);
|
|
QuestionResponse questionResponse = new QuestionResponse(question);
|
|
List<Topic> topics = new ArrayList<>();
|
|
for (String topic : question.getTopics()) {
|
|
topics.add(topicDAO.findById(topic));
|
|
}
|
|
questionResponse.setTopics(topics);
|
|
return questionResponse;
|
|
}
|
|
|
|
@RequestMapping(value = "/question/{id}", method = RequestMethod.DELETE)
|
|
public void deleteQuestion(@PathVariable(value = "id") String id) {
|
|
questionDAO.delete(id);
|
|
}
|
|
|
|
@RequestMapping(value = "/question/inc/{id}", method = RequestMethod.PUT)
|
|
public Question incQuestion(@PathVariable(value = "id") String id) {
|
|
Question question = questionDAO.findById(id);
|
|
question.increment();
|
|
questionDAO.save(question);
|
|
return question;
|
|
}
|
|
|
|
@RequestMapping(value = "/question/delete", method = RequestMethod.POST)
|
|
public void deleteQuestions(@RequestBody List<String> questions) {
|
|
for (String question : questions) {
|
|
deleteQuestion(question);
|
|
}
|
|
}
|
|
|
|
@RequestMapping(value = "/question/toggle", method = RequestMethod.POST)
|
|
public void toggleQuestions(@RequestBody List<String> questions, @RequestParam String status) throws Exception {
|
|
for (String id: questions) {
|
|
Question question = questionDAO.findById(id);
|
|
question.setIsActive(Boolean.parseBoolean(status));
|
|
questionDAO.save(question);
|
|
}
|
|
}
|
|
}
|