Hendrik Jungnitsch
2022-09-19 a0b53aec76f777d22b035ccfda713725ab0b796e
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
package de.gedoplan.seminar.sbt.sbtrestexercise.rest;
 
import de.gedoplan.seminar.sbt.sbtrestexercise.domain.Person;
import de.gedoplan.seminar.sbt.sbtrestexercise.repository.PersonRepository;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
 
import java.util.List;
 
@RestController
@RequestMapping(path = "/personen", produces = MediaType.APPLICATION_JSON_VALUE)
public class PersonResource {
    private final PersonRepository personRepository;
 
    public PersonResource(PersonRepository personRepository) {
        this.personRepository = personRepository;
    }
 
    @GetMapping
    public List<Person> getPersonen() {
        return personRepository.findAll();
    }
 
    @GetMapping(path = "{id}",produces = MediaType.APPLICATION_JSON_VALUE)
    public Person getTalk(@PathVariable Integer id) {
        return personRepository.findById(id)
                .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
    }
 
}