-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPersonController.java
More file actions
46 lines (39 loc) · 1.48 KB
/
PersonController.java
File metadata and controls
46 lines (39 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package de.conciso.starter;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@CrossOrigin("http://localhost:4200")
@RequestMapping("/api/person")
public class PersonController {
private final Personen personen;
public PersonController(Personen personen) {
this.personen = personen;
}
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Person> create(@RequestParam("vorname") String vorname,
@RequestParam("name") String name) {
try {
var person = personen.create(vorname, name);
return ResponseEntity.ok(person);
} catch (IllegalArgumentException exception) {
return ResponseEntity.badRequest().build();
} catch (Exception exception) {
return ResponseEntity.internalServerError().build();
}
}
@GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Person> findById(@PathVariable("id") int id) {
try {
var found = personen.findById(id);
return found.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build());
} catch (Exception exception) {
return ResponseEntity.internalServerError().build();
}
}
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Person> findByRequestParam(@RequestParam("id") int id) {
return findById(id);
}
}