-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98dcb01
commit d7d92ec
Showing
92 changed files
with
3,561 additions
and
185 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
gateway/src/main/java/ru/practicum/shareit/api/RequestHttpHeaders.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package ru.practicum.shareit.api; | ||
|
||
import lombok.experimental.UtilityClass; | ||
|
||
@UtilityClass | ||
public class RequestHttpHeaders { | ||
public static final String USER_ID = "X-Sharer-User-Id"; | ||
} |
4 changes: 4 additions & 0 deletions
4
gateway/src/main/java/ru/practicum/shareit/api/ValidateCreateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package ru.practicum.shareit.api; | ||
|
||
public interface ValidateCreateRequest { | ||
} |
4 changes: 4 additions & 0 deletions
4
gateway/src/main/java/ru/practicum/shareit/api/ValidateUpdateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package ru.practicum.shareit.api; | ||
|
||
public interface ValidateUpdateRequest { | ||
} |
48 changes: 0 additions & 48 deletions
48
gateway/src/main/java/ru/practicum/shareit/booking/BookingClient.java
This file was deleted.
Oops, something went wrong.
55 changes: 0 additions & 55 deletions
55
gateway/src/main/java/ru/practicum/shareit/booking/BookingController.java
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
gateway/src/main/java/ru/practicum/shareit/booking/client/BookingClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package ru.practicum.shareit.booking.client; | ||
|
||
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.util.DefaultUriBuilderFactory; | ||
|
||
import ru.practicum.shareit.booking.dto.BookingSaveDto; | ||
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"; | ||
private static final String BOOKING_ID_PATH = "/{bookingId}"; | ||
private static final String BOOKING_PATH = BOOKING_ID_PATH + "?approved={approved}"; | ||
private static final String ALL_USER_BOOKINGS_PATH = "?state={state}&from={from}&size={size}"; | ||
private static final String ALL_USER_ITEMS_BOOKINGS_PATH = "/owner/?state={state}"; | ||
|
||
@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> addBooking(Integer userId, BookingSaveDto bookingSaveDto) { | ||
return post("", userId, bookingSaveDto); | ||
} | ||
|
||
public ResponseEntity<Object> manageBooking(Integer userId, Integer bookingId, Boolean approved) { | ||
Map<String, Object> uriVariables = Map.of("bookingId",bookingId,"approved", approved); | ||
return patch(BOOKING_PATH, userId, uriVariables); | ||
} | ||
|
||
public ResponseEntity<Object> getBooking(Integer userId, Integer bookingId) { | ||
Map<String, Object> uriVariables = Map.of("bookingId", bookingId); | ||
return get(BOOKING_ID_PATH, userId, uriVariables); | ||
} | ||
|
||
public ResponseEntity<Object> getAllUserBookings(Integer userId, BookingState state, Integer from, Integer size) { | ||
Map<String, Object> uriVariables = Map.of("state", state.name(), "from", from, "size", size); | ||
return get(ALL_USER_BOOKINGS_PATH, userId, uriVariables); | ||
} | ||
|
||
public ResponseEntity<Object> getAllUserItemsBookings(Integer userId, BookingState state) { | ||
Map<String, Object> uriVariables = Map.of("state", state.name()); | ||
return get(ALL_USER_ITEMS_BOOKINGS_PATH, userId, uriVariables); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
gateway/src/main/java/ru/practicum/shareit/booking/controller/BookingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package ru.practicum.shareit.booking.controller; | ||
|
||
import jakarta.validation.Valid; | ||
import jakarta.validation.constraints.Positive; | ||
import jakarta.validation.constraints.PositiveOrZero; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.*; | ||
import ru.practicum.shareit.api.RequestHttpHeaders; | ||
import ru.practicum.shareit.booking.client.BookingClient; | ||
import ru.practicum.shareit.booking.dto.BookingSaveDto; | ||
import ru.practicum.shareit.booking.dto.BookingState; | ||
import ru.practicum.shareit.exceptions.NotValidException; | ||
|
||
|
||
@Controller | ||
@RequestMapping(path = "/bookings") | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
@Validated | ||
public class BookingController { | ||
private final BookingClient bookingClient; | ||
|
||
@PostMapping | ||
public ResponseEntity<Object> addBooking(@RequestHeader(RequestHttpHeaders.USER_ID) Integer userId, | ||
@RequestBody @Valid BookingSaveDto bookingSaveDto) { | ||
return bookingClient.addBooking(userId, bookingSaveDto); | ||
} | ||
|
||
@PatchMapping("/{bookingId}") | ||
public ResponseEntity<Object> manageBooking(@RequestHeader(RequestHttpHeaders.USER_ID) Integer userId, | ||
@PathVariable Integer bookingId, | ||
@RequestParam Boolean approved) { | ||
return bookingClient.manageBooking(userId, bookingId, approved); | ||
} | ||
|
||
@GetMapping("/{bookingId}") | ||
public ResponseEntity<Object> getBooking(@RequestHeader(RequestHttpHeaders.USER_ID) Integer userId, | ||
@PathVariable Integer bookingId) { | ||
return bookingClient.getBooking(userId, bookingId); | ||
} | ||
|
||
@GetMapping | ||
public ResponseEntity<Object> getAllUserBookings(@RequestHeader(RequestHttpHeaders.USER_ID) Integer userId, | ||
@RequestParam(defaultValue = "all") String state, | ||
@RequestParam(defaultValue = "0") @PositiveOrZero Integer from, | ||
@RequestParam(defaultValue = "10") @Positive Integer size) { | ||
BookingState bookingState = getBookingState(state); | ||
return bookingClient.getAllUserBookings(userId, bookingState, from, size); | ||
} | ||
|
||
@GetMapping("/owner") | ||
public ResponseEntity<Object> getAllUserItemsBookings(@RequestHeader(RequestHttpHeaders.USER_ID) Integer userId, | ||
@RequestParam(defaultValue = "all") String state) { | ||
BookingState bookingState = getBookingState(state); | ||
|
||
return bookingClient.getAllUserItemsBookings(userId, bookingState); | ||
} | ||
|
||
private BookingState getBookingState(String state) { | ||
return BookingState.from(state) | ||
.orElseThrow(() -> new NotValidException(BookingState.class, state + " not valid")); | ||
} | ||
|
||
} |
20 changes: 0 additions & 20 deletions
20
gateway/src/main/java/ru/practicum/shareit/booking/dto/BookItemRequestDto.java
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
gateway/src/main/java/ru/practicum/shareit/booking/dto/BookingSaveDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ru.practicum.shareit.booking.dto; | ||
|
||
import jakarta.validation.constraints.FutureOrPresent; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.AccessLevel; | ||
import lombok.Data; | ||
import lombok.experimental.FieldDefaults; | ||
import ru.practicum.shareit.validation.DateTimeStartBeforeEnd; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Data | ||
@FieldDefaults(level = AccessLevel.PRIVATE) | ||
@DateTimeStartBeforeEnd | ||
public class BookingSaveDto { | ||
@NotNull | ||
Integer itemId; | ||
@FutureOrPresent | ||
LocalDateTime start; | ||
LocalDateTime end; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.