|
1 | 1 | package ru.practicum.shareit.booking; |
2 | 2 |
|
3 | | -import org.springframework.web.bind.annotation.RequestMapping; |
4 | | -import org.springframework.web.bind.annotation.RestController; |
| 3 | +import jakarta.validation.Valid; |
| 4 | +import jakarta.validation.constraints.Positive; |
| 5 | +import lombok.RequiredArgsConstructor; |
| 6 | +import org.springframework.web.bind.annotation.*; |
| 7 | +import ru.practicum.shareit.booking.dto.BookingDto; |
| 8 | +import ru.practicum.shareit.booking.enums.BookingState; |
| 9 | +import ru.practicum.shareit.booking.model.Booking; |
| 10 | +import ru.practicum.shareit.booking.service.BookingService; |
| 11 | + |
| 12 | +import java.util.Collection; |
| 13 | + |
| 14 | +import static ru.practicum.shareit.Constants.ALL; |
| 15 | +import static ru.practicum.shareit.Constants.USER_ID_HEADER; |
5 | 16 |
|
6 | 17 | /** |
7 | 18 | * TODO Sprint add-bookings. |
8 | 19 | */ |
9 | 20 | @RestController |
10 | 21 | @RequestMapping(path = "/bookings") |
| 22 | +@RequiredArgsConstructor |
11 | 23 | public class BookingController { |
| 24 | + |
| 25 | + private final BookingService bookingService; |
| 26 | + |
| 27 | + @PostMapping |
| 28 | + public Booking createBooking(@RequestHeader(USER_ID_HEADER) @Positive Long userId, |
| 29 | + @Valid @RequestBody BookingDto bookingDto) { |
| 30 | + |
| 31 | + return bookingService.createBooking(userId, bookingDto); |
| 32 | + } |
| 33 | + |
| 34 | + @PatchMapping("/{bookingId}") |
| 35 | + public Booking updateBookingStatus( |
| 36 | + @RequestHeader(USER_ID_HEADER) @Positive Long userId, |
| 37 | + @PathVariable @Positive Long bookingId, |
| 38 | + @RequestParam Boolean approved) { |
| 39 | + |
| 40 | + return bookingService.updateBookingStatus(userId, bookingId, approved); |
| 41 | + } |
| 42 | + |
| 43 | + @GetMapping("/{bookingId}") |
| 44 | + public Booking getBooking( |
| 45 | + @RequestHeader(USER_ID_HEADER) @Positive Long userId, |
| 46 | + @PathVariable @Positive Long bookingId) { |
| 47 | + |
| 48 | + return bookingService.getBooking(userId, bookingId); |
| 49 | + } |
| 50 | + |
| 51 | + @GetMapping |
| 52 | + public Collection<Booking> findByBookerAndState( |
| 53 | + @RequestHeader(USER_ID_HEADER) @Positive Long bookerId, |
| 54 | + @RequestParam(defaultValue = ALL) BookingState state) { |
| 55 | + |
| 56 | + return bookingService.findByBookerAndState(bookerId, state); |
| 57 | + } |
| 58 | + |
| 59 | + @GetMapping("/owner") |
| 60 | + public Collection<Booking> findByOwnerAndState( |
| 61 | + @RequestHeader(USER_ID_HEADER) @Positive Long ownerId, |
| 62 | + @RequestParam(defaultValue = ALL) BookingState state) { |
| 63 | + |
| 64 | + return bookingService.findByOwnerAndState(ownerId, state); |
| 65 | + } |
12 | 66 | } |
0 commit comments