-
Notifications
You must be signed in to change notification settings - Fork 287
Expand file tree
/
Copy pathBeerClientImpl.java
More file actions
95 lines (73 loc) · 3.1 KB
/
BeerClientImpl.java
File metadata and controls
95 lines (73 loc) · 3.1 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package guru.springframework.spring6resttemplate.client;
import guru.springframework.spring6resttemplate.model.BeerDTO;
import guru.springframework.spring6resttemplate.model.BeerDTOPageImpl;
import guru.springframework.spring6resttemplate.model.BeerStyle;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.util.UUID;
/**
* Created by jt, Spring Framework Guru.
*/
@RequiredArgsConstructor
@Service
public class BeerClientImpl implements BeerClient {
private final RestTemplateBuilder restTemplateBuilder;
public static final String GET_BEER_PATH = "/api/v1/beer";
public static final String GET_BEER_BY_ID_PATH = "/api/v1/beer/{beerId}";
@Override
public void deleteBeer(UUID beerId) {
RestTemplate restTemplate = restTemplateBuilder.build();
restTemplate.delete(GET_BEER_BY_ID_PATH, beerId);
}
@Override
public BeerDTO updateBeer(BeerDTO beerDto) {
RestTemplate restTemplate = restTemplateBuilder.build();
restTemplate.put(GET_BEER_BY_ID_PATH, beerDto, beerDto.getId());
return getBeerById(beerDto.getId());
}
@Override
public BeerDTO createBeer(BeerDTO newDto) {
RestTemplate restTemplate = restTemplateBuilder.build();
URI uri = restTemplate.postForLocation(GET_BEER_PATH, newDto);
return restTemplate.getForObject(uri.getPath(), BeerDTO.class);
}
@Override
public BeerDTO getBeerById(UUID beerId) {
RestTemplate restTemplate = restTemplateBuilder.build();
return restTemplate.getForObject(GET_BEER_BY_ID_PATH, BeerDTO.class, beerId);
}
@Override
public Page<BeerDTO> listBeers() {
return this.listBeers(null, null, null, null, null);
}
@Override
public Page<BeerDTO> listBeers(String beerName,BeerStyle beerStyle, Boolean showInventory, Integer pageNumber,
Integer pageSize) {
RestTemplate restTemplate = restTemplateBuilder.build();
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromPath(GET_BEER_PATH);
if (beerName != null) {
uriComponentsBuilder.queryParam("beerName", beerName);
}
if (beerStyle != null) {
uriComponentsBuilder.queryParam("beerStyle", beerStyle);
}
if (showInventory != null) {
uriComponentsBuilder.queryParam("showInventory", showInventory);
}
if (pageNumber != null) {
uriComponentsBuilder.queryParam("pageNumber", pageNumber);
}
if (pageSize != null) {
uriComponentsBuilder.queryParam("pageSize", pageSize);
}
ResponseEntity<BeerDTOPageImpl> response =
restTemplate.getForEntity(uriComponentsBuilder.toUriString() , BeerDTOPageImpl.class);
return response.getBody();
}
}