QuestionController, creat et update fonctionent
This commit is contained in:
21
src/main/java/sample/data/jpa/SecurityConfig.java
Normal file
21
src/main/java/sample/data/jpa/SecurityConfig.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package sample.data.jpa;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
@Configuration
|
||||
public class SecurityConfig {
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http
|
||||
.csrf(csrf -> csrf.disable())
|
||||
.authorizeHttpRequests(auth -> auth
|
||||
.anyRequest().permitAll()
|
||||
);
|
||||
|
||||
return http.build();
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ public class Question implements Serializable {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private int id;
|
||||
private String question;
|
||||
private String enonce;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
@JoinColumn(name ="bonne_reponse", referencedColumnName = "id")
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package sample.data.jpa.web;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@@ -18,37 +22,49 @@ import sample.data.jpa.service.QuestionDao;
|
||||
@Controller
|
||||
@RequestMapping("/question")
|
||||
public class QuestionController {
|
||||
@Autowired
|
||||
|
||||
@Autowired
|
||||
private QuestionDao qDao;
|
||||
|
||||
|
||||
@RequestMapping("/create")
|
||||
@ResponseBody
|
||||
public String create(String email, String name) {
|
||||
String qId = "";
|
||||
try {
|
||||
Question question = new Question();
|
||||
qDao.save(question);
|
||||
qId = String.valueOf(question.getId());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return "Error creating the question : " + ex.toString();
|
||||
}
|
||||
return "Question succesfully created with id = " + qId;
|
||||
}
|
||||
|
||||
@RequestMapping("/update")
|
||||
/*
|
||||
* Utiliser un Json pour mettre : String enonce
|
||||
*/
|
||||
@PostMapping("/create")
|
||||
@ResponseBody
|
||||
public String updateQuestion(int id, String newQ) {
|
||||
public String create(@RequestBody Map<String, String> body) {
|
||||
String qId = "";
|
||||
Question q = new Question();
|
||||
try {
|
||||
Question q = qDao.findById(id).get();
|
||||
q.setQuestion(newQ);
|
||||
qDao.save(q);
|
||||
if(body.containsKey("enonce")){
|
||||
q.setEnonce(body.get("enonce"));
|
||||
}
|
||||
else q.setEnonce("");
|
||||
qDao.save(q);
|
||||
qId = String.valueOf(q.getId());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return "Error updating the question: " + ex.toString();
|
||||
return "Error creating the question : " + ex.toString();
|
||||
}
|
||||
return "Question succesfully updated!";
|
||||
return "Question \""+q.getEnonce()+"\" succesfully created with id = " + qId;
|
||||
}
|
||||
|
||||
/*
|
||||
* Utiliser un Json pour mettre : String enonce
|
||||
* l'id se met dans l'url
|
||||
*/
|
||||
@PutMapping("/update/{id}")
|
||||
@ResponseBody
|
||||
public String updateQuestion(@PathVariable("id") int id, @RequestBody Map<String, String> body) {
|
||||
Question q;
|
||||
try {
|
||||
q = qDao.findById(id).get();
|
||||
q.setEnonce(body.get("enonce"));
|
||||
qDao.save(q);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return "Error updating the question: " + ex.toString();
|
||||
}
|
||||
return "Question "+id+" succesfully updated! : " + q.getEnonce();
|
||||
}
|
||||
|
||||
@RequestMapping("/getReponse")
|
||||
|
||||
Reference in New Issue
Block a user