-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathBookingClient.java
More file actions
71 lines (59 loc) · 2.66 KB
/
Copy pathBookingClient.java
File metadata and controls
71 lines (59 loc) · 2.66 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
package ru.practicum.shareit.booking;
import java.util.Map;
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.bind.annotation.*;
import org.springframework.web.util.DefaultUriBuilderFactory;
import ru.practicum.shareit.booking.dto.BookItemRequestDto;
import ru.practicum.shareit.booking.dto.BookingState;
import ru.practicum.shareit.client.BaseClient;
@Service
public class BookingClient extends BaseClient {
private static final String API_PREFIX = "/bookings";
@Autowired
public BookingClient(@Value("${shareit-server.url}") String serverUrl, RestTemplateBuilder builder) {
super(
builder
.uriTemplateHandler(new DefaultUriBuilderFactory(serverUrl + API_PREFIX))
.requestFactory(() -> new HttpComponentsClientHttpRequestFactory())
.build()
);
}
public ResponseEntity<Object> getBookings(long userId, BookingState state, Integer from, Integer size) {
Map<String, Object> parameters = Map.of(
"state", state.name(),
"from", from,
"size", size
);
return get("?state={state}&from={from}&size={size}", userId, parameters);
}
@GetMapping("/owner")
public ResponseEntity<Object> getBookingsByState(BookingState state, Integer from, Integer size, Long userId) {
Map<String, Object> parameters = Map.of(
"state", state.name(),
"from", from,
"size", size
);
return get("/owner?state={state}&from={from}&size={size}", userId, parameters);
}
public ResponseEntity<Object> bookItem(long userId, BookItemRequestDto requestDto) {
return post("", userId, requestDto);
}
public ResponseEntity<Object> getBooking(long userId, Long bookingId) {
return get("/" + bookingId, userId);
}
//Patch /bookings/bookingId?approved
public ResponseEntity<Object> approveBooking(Long bookingId, Boolean approved, Long userId) {
Map<String, Object> parameters = Map.of(
"approved", approved
);
return patch("/" + bookingId + "?approved={approved}", userId, parameters, null);
}
public ResponseEntity<Object> canceledBooking(Long bookingId, Long userId) {
return patch("/" + bookingId + "/canceled", userId);
}
}