-
Notifications
You must be signed in to change notification settings - Fork 599
Expand file tree
/
Copy pathCustomerController.java
More file actions
76 lines (56 loc) · 2.53 KB
/
Copy pathCustomerController.java
File metadata and controls
76 lines (56 loc) · 2.53 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package guru.springframework.spring6restmvc.controller;
import guru.springframework.spring6restmvc.model.CustomerDTO;
import guru.springframework.spring6restmvc.services.CustomerService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.UUID;
/**
* Created by jt, Spring Framework Guru.
*/
@RequiredArgsConstructor
@RestController
public class CustomerController {
public static final String CUSTOMER_PATH = "/api/v1/customer";
public static final String CUSTOMER_PATH_ID = CUSTOMER_PATH + "/{customerId}";
private final CustomerService customerService;
@PatchMapping(CUSTOMER_PATH_ID)
public ResponseEntity patchCustomerById(@PathVariable("customerId") UUID customerId,
@RequestBody CustomerDTO customer){
customerService.patchCustomerById(customerId, customer);
return new ResponseEntity(HttpStatus.NO_CONTENT);
}
@DeleteMapping(CUSTOMER_PATH_ID)
public ResponseEntity deleteCustomerById(@PathVariable("customerId") UUID customerId){
if (!customerService.deleteCustomerById(customerId)){
throw new NotFoundException();
}
return new ResponseEntity(HttpStatus.NO_CONTENT);
}
@PutMapping(CUSTOMER_PATH_ID)
public ResponseEntity updateCustomerByID(@PathVariable("customerId") UUID customerId,
@RequestBody CustomerDTO customer){
if (customerService.updateCustomerById(customerId, customer).isEmpty()){
throw new NotFoundException();
}
return new ResponseEntity(HttpStatus.NO_CONTENT);
}
@PostMapping(CUSTOMER_PATH)
public ResponseEntity handlePost(@RequestBody CustomerDTO customer){
CustomerDTO savedCustomer = customerService.saveNewCustomer(customer);
HttpHeaders headers = new HttpHeaders();
headers.add("Location", CUSTOMER_PATH + "/" + savedCustomer.getId().toString());
return new ResponseEntity(headers, HttpStatus.CREATED);
}
@GetMapping(CUSTOMER_PATH)
public List<CustomerDTO> listAllCustomers(){
return customerService.getAllCustomers();
}
@GetMapping(value = CUSTOMER_PATH_ID)
public CustomerDTO getCustomerById(@PathVariable("customerId") UUID id){
return customerService.getCustomerById(id).orElseThrow(NotFoundException::new);
}
}