Skip to content
Snippets Groups Projects
Commit a84a5b55 authored by Quentin Barlet's avatar Quentin Barlet :spades:
Browse files

Exercie 02 Done

parent de1ed5c8
Branches dev
No related tags found
No related merge requests found
meta {
name: DELETE One Student
type: http
seq: 5
}
delete {
url: localhost:8080/etudiant/10
body: json
auth: none
}
body:json {
{
"id": 10,
"prenom": "Bruce",
"nom": "Banner",
"age": 100,
"groupe": "S"
}
}
meta {
name: GET All Student By Group
type: http
seq: 3
seq: 7
}
get {
......
meta {
name: GET One Student
type: http
seq: 2
seq: 6
}
get {
url: localhost:8080/etudiant/2
url: localhost:8080/etudiant/10
body: none
auth: none
}
meta {
name: PATCH One Student
type: http
seq: 4
}
patch {
url: localhost:8080/etudiant/10
body: json
auth: none
}
body:json {
{
"prenom": "B",
"nom": "NAME",
"age": 50,
"groupe": "Z"
}
}
meta {
name: POST One Student
type: http
seq: 4
seq: 3
}
post {
......
meta {
name: PUT One Student
type: http
seq: 2
}
put {
url: localhost:8080/etudiant/10
body: json
auth: none
}
body:json {
{
"prenom": "B",
"nom": "B",
"age": 200,
"groupe": "H"
}
}
package fr.but3.tp508;
import java.util.List;
import java.net.http.HttpResponse;
import java.util.Map;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Optional;
......@@ -10,9 +12,13 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
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.http.HttpStatus;
import org.springframework.http.MediaType;
......@@ -25,19 +31,21 @@ public class ControleurEtudiantRest {
@Autowired
private EtudiantRepository etudiantRepository;
@GetMapping
public List<Etudiant> getAllStudent() {
@GetMapping(produces={MediaType.APPLICATION_JSON_VALUE})
List<Etudiant> getAllStudent() {
return (List<Etudiant>) this.etudiantRepository.findAll();
}
@PostMapping
public Etudiant saveStudent(@RequestBody Etudiant student) {
Etudiant query_result = this.etudiantRepository.save(student);
return query_result;
@PostMapping(produces={MediaType.APPLICATION_JSON_VALUE})
Etudiant saveStudent(@RequestBody Etudiant student) {
return this.etudiantRepository.save(student);
}
@GetMapping(value = "/{id}")
/* #########################################################################
* # /{id} #
* #########################################################################
*/
@GetMapping(value = "/{id}", produces={MediaType.APPLICATION_JSON_VALUE})
ResponseEntity<Etudiant> getOneStudent(@PathVariable int id) {
Optional<Etudiant> query_result = this.etudiantRepository.findById(id);
......@@ -46,7 +54,61 @@ public class ControleurEtudiantRest {
return ResponseEntity.notFound().build();
}
@GetMapping(value = "/select")
@PutMapping(value="/{id}", produces={MediaType.APPLICATION_JSON_VALUE})
ResponseEntity<Etudiant> updateCompleteStudent(@PathVariable int id, @RequestBody Etudiant newStudent) {
Optional<Etudiant> queryResult = this.etudiantRepository.findById(id);
if(queryResult.isPresent()) {
Etudiant student = queryResult.get();
student = newStudent;
student.setId(id);
this.etudiantRepository.save(student);
return ResponseEntity.ok(student);
} else {
return ResponseEntity.notFound().build();
}
}
@PatchMapping(value="/{id}", produces={MediaType.APPLICATION_JSON_VALUE})
ResponseEntity<Etudiant> updatePartiallyStudent(@PathVariable int id, @RequestBody Map<String, Object> updates) {
Optional<Etudiant> queryResult = this.etudiantRepository.findById(id);
if(queryResult.isPresent()) {
Etudiant student = queryResult.get();
for(Map.Entry<String, Object> entry: updates.entrySet()) {
try {
Method setter = new PropertyDescriptor(entry.getKey(), Etudiant.class).getWriteMethod();
setter.invoke(student, entry.getValue());
}catch(Exception e) {
System.err.println(e);
}
}
this.etudiantRepository.save(student);
return ResponseEntity.ok(student);
} else {
return ResponseEntity.notFound().build();
}
}
@DeleteMapping(value = "/{id}", produces={MediaType.APPLICATION_JSON_VALUE})
void deleteStudent(@PathVariable int id) {
this.etudiantRepository.deleteById(id);
}
/* #########################################################################
* # /select #
* #########################################################################
*/
@GetMapping(value = "/select", produces={MediaType.APPLICATION_JSON_VALUE})
List<Etudiant> select(@RequestParam String groupe) {
List<Etudiant> students = new ArrayList<>();
......
......@@ -10,7 +10,6 @@ import jakarta.persistence.GeneratedValue;
@ToString
@Data
public class Etudiant {
@GeneratedValue
@Id
int id;
String prenom;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment