-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathBookingController.java
More file actions
80 lines (69 loc) · 4.09 KB
/
Copy pathBookingController.java
File metadata and controls
80 lines (69 loc) · 4.09 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
package ru.practicum.shareit.booking;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import ru.practicum.shareit.booking.dto.BookItemRequestDto;
import ru.practicum.shareit.booking.dto.BookingState;
import java.util.List;
@Controller
@RequestMapping(path = "/bookings")
@RequiredArgsConstructor
@Slf4j
@Validated
public class BookingController {
private final BookingClient bookingClient;
@GetMapping
public ResponseEntity<Object> getBookings(@RequestHeader("X-Sharer-User-Id") @Positive long userId,
@RequestParam(name = "state", defaultValue = "all") String stateParam,
@PositiveOrZero @RequestParam(name = "from", defaultValue = "0") Integer from,
@Positive @RequestParam(name = "size", defaultValue = "10") Integer size) {
BookingState state = BookingState.from(stateParam)
.orElseThrow(() -> new IllegalArgumentException("Unknown state: " + stateParam));
log.info("Get booking with state {}, userId={}, from={}, size={}", stateParam, userId, from, size);
return bookingClient.getBookings(userId, state, from, size);
}
@GetMapping("/owner")
public ResponseEntity<Object> getBookingsByState(@RequestParam(name = "state", defaultValue = "all")
String stateParam,
@PositiveOrZero @RequestParam(name = "from", defaultValue = "0")
Integer from,
@Positive @RequestParam(name = "size", defaultValue = "10")
Integer size,
@RequestHeader("X-Sharer-User-Id") @Positive Long userId) {
BookingState state = BookingState.from(stateParam)
.orElseThrow(() -> new IllegalArgumentException("Unknown state: " + stateParam));
log.info("Get booking owner with state {}, userId={}, from={}, size={}", stateParam, userId, from, size);
return bookingClient.getBookingsByState(state, from, size, userId);
}
@GetMapping("/{bookingId}")
public ResponseEntity<Object> getBooking(@RequestHeader("X-Sharer-User-Id") long userId,
@PathVariable Long bookingId) {
log.info("Get booking {}, userId={}", bookingId, userId);
return bookingClient.getBooking(userId, bookingId);
}
@PostMapping
public ResponseEntity<Object> createBooking(@RequestHeader("X-Sharer-User-Id") long userId,
@RequestBody @Valid BookItemRequestDto requestDto) {
log.info("Creating booking {}, userId={}", requestDto, userId);
return bookingClient.bookItem(userId, requestDto);
}
@PatchMapping("/{bookingId}")
public ResponseEntity<Object> approveBooking(@PathVariable("bookingId") Long bookingId,
@RequestParam("approved") Boolean approved,
@RequestHeader("X-Sharer-User-Id") Long userId) {
log.info("Update booking {}, userId={} on approve = {}", bookingId, userId, approved);
return bookingClient.approveBooking(bookingId, approved, userId);
}
@PatchMapping("/{bookingId}/canceled")
public ResponseEntity<Object> canceledBooking(@PathVariable("bookingId") Long bookingId,
@RequestHeader("X-Sharer-User-Id") Long userId) {
log.info("Cancel booking {}, userId={}", bookingId, userId);
return bookingClient.canceledBooking(bookingId, userId);
}
}