| package pl.hackerspace.controller; |
| |
| import pl.hackerspace.domain.Client; |
| import pl.hackerspace.repository.ClientRepository; |
| import org.springframework.http.ResponseEntity; |
| import org.springframework.web.bind.annotation.*; |
| |
| import java.net.URI; |
| import java.net.URISyntaxException; |
| import java.util.List; |
| |
| @RestController |
| @RequestMapping("/clients") |
| public class ClientsController { |
| |
| private final ClientRepository clientRepository; |
| |
| public ClientsController(ClientRepository clientRepository) { |
| this.clientRepository = clientRepository; |
| } |
| |
| @GetMapping |
| public List<Client> getClients() { |
| return clientRepository.findAll(); |
| } |
| |
| @GetMapping("/{nip}") |
| public Client getClient(@PathVariable String nip) { |
| return clientRepository.findById(nip).orElseThrow(RuntimeException::new); |
| } |
| |
| @PostMapping |
| public ResponseEntity<Client> createClient(@RequestBody Client client) throws URISyntaxException { |
| Client savedClient = clientRepository.save(client); |
| return ResponseEntity.created(new URI("/clients/" + savedClient.getNip())).body(savedClient); |
| } |
| |
| @PutMapping("/{nip}") |
| public ResponseEntity<Client> updateClient(@PathVariable String nip, @RequestBody Client client) { |
| Client currentClient = clientRepository.findById(nip).orElseThrow(RuntimeException::new); |
| currentClient.setName(client.getName()); |
| currentClient.setEmail(client.getEmail()); |
| currentClient = clientRepository.save(client); |
| |
| return ResponseEntity.ok(currentClient); |
| } |
| |
| @DeleteMapping("/{nip}") |
| public ResponseEntity<Void> deleteClient(@PathVariable String nip) { |
| clientRepository.deleteById(nip); |
| return ResponseEntity.ok().build(); |
| } |
| |
| } |