-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathItemClient.java
More file actions
52 lines (42 loc) · 1.88 KB
/
Copy pathItemClient.java
File metadata and controls
52 lines (42 loc) · 1.88 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
package ru.practicum.shareit.item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.util.DefaultUriBuilderFactory;
import ru.practicum.shareit.client.BaseClient;
import ru.practicum.shareit.item.dto.ItemDto;
import ru.practicum.shareit.item.dto.RequestCommentDto;
import java.util.Map;
@Service
public class ItemClient extends BaseClient {
private static final String API_PREFIX = "/items";
@Autowired
public ItemClient(@Value("${shareit-server.url}") String serverUrl, RestTemplateBuilder builder) {
super(builder
.uriTemplateHandler(new DefaultUriBuilderFactory(serverUrl + API_PREFIX))
.requestFactory(() -> new HttpComponentsClientHttpRequestFactory())
.build());
}
public ResponseEntity<Object> createItem(Long userId, ItemDto item) {
return post("", userId, item);
}
public ResponseEntity<Object> getItemById(Long itemId) {
return get("/" + itemId);
}
public ResponseEntity<Object> getUserItems(Long userId) {
return get("", userId);
}
public ResponseEntity<Object> updateItem(Long userId, Long itemId, ItemDto item) {
return put("/" + itemId, userId, item);
}
public ResponseEntity<Object> createdComment(RequestCommentDto comment, long userId, long itemId) {
return post("/" + itemId + "/comment", userId, comment);
}
public ResponseEntity<Object> searchItems(Long userId, String text) {
Map<String, Object> params = Map.of("text", text);
return get("/search?text={text}", userId, params);
}
}