|
| 1 | +package pl.codeleak.samples.springbootjunit5.todo; |
| 2 | + |
| 3 | +import org.springframework.core.ParameterizedTypeReference; |
| 4 | +import org.springframework.http.HttpMethod; |
| 5 | +import org.springframework.web.client.RestTemplate; |
| 6 | +import pl.codeleak.samples.springbootjunit5.config.JsonPlaceholderApiConfigProperties; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +public class JsonPlaceholderTaskRepository implements TaskRepository { |
| 11 | + |
| 12 | + private final RestTemplate restTemplate; |
| 13 | + private final JsonPlaceholderApiConfigProperties properties; |
| 14 | + |
| 15 | + public JsonPlaceholderTaskRepository(RestTemplate restTemplate, JsonPlaceholderApiConfigProperties properties) { |
| 16 | + this.restTemplate = restTemplate; |
| 17 | + this.properties = properties; |
| 18 | + } |
| 19 | + |
| 20 | + @Override |
| 21 | + public Task findOne(Integer id) { |
| 22 | + return restTemplate |
| 23 | + .getForObject("/todos/{id}", Task.class, id); |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public List<Task> findAll() { |
| 28 | + return restTemplate |
| 29 | + .exchange("/todos?_sort={sort}&_order={order}&_limit={limit}", |
| 30 | + HttpMethod.GET, |
| 31 | + null, |
| 32 | + new ParameterizedTypeReference<List<Task>>() {}, |
| 33 | + properties.getTodoFindAll().getSort(), |
| 34 | + properties.getTodoFindAll().getOrder(), |
| 35 | + properties.getTodoFindAll().getLimit()) |
| 36 | + .getBody(); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public List<Task> findByTitle(String title) { |
| 41 | + return restTemplate |
| 42 | + .exchange("/todos?title_like={title}", HttpMethod.GET, null, new ParameterizedTypeReference<List<Task>>() {}, title) |
| 43 | + .getBody(); |
| 44 | + } |
| 45 | +} |
0 commit comments