generated from yandex-praktikum/java-shareit
-
Notifications
You must be signed in to change notification settings - Fork 0
Add item requests and gateway #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aKorishev
wants to merge
11
commits into
main
Choose a base branch
from
add-item-requests-and-gateway
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ec39ed9
[add-item-requests-and-gateway] Добавил логику Request...
aKorishev f7eea75
[add-item-requests-and-gateway] Добавил поле requestId в item
aKorishev 6c40b88
[add-item-requests-and-gateway] Отладка по итогам тестов postman
aKorishev 168be08
[add-item-requests-and-gateway] Добавил модули server и gateway. Доба…
aKorishev 02ef3cd
[add-item-requests-and-gateway] Исправил checkStyle
aKorishev 1b3abdf
[add-item-requests-and-gateway] Удалил файлы java из общего проекта
aKorishev 0f9dc7e
[add-item-requests-and-gateway] Добавил файлы, которые не попали в ки…
aKorishev 7433bdd
[add-item-requests-and-gateway] Добавил тесты
aKorishev a91c6b4
[add-item-requests-and-gateway] checkstyle
aKorishev 08a7307
[add-item-requests-and-gateway] Добавил тесты для server
aKorishev 48ded6e
[add-item-requests-and-gateway] Исправил замечания ревью
aKorishev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,39 @@ | ||
| services: | ||
| gateway: | ||
| build: gateway | ||
| image: shareit-gateway | ||
| container_name: shareit-gateway | ||
| ports: | ||
| - "8080:8080" | ||
| depends_on: | ||
| - server | ||
| environment: | ||
| - SHAREIT_SERVER_URL=http://server:9090 | ||
|
|
||
| server: | ||
| build: server | ||
| image: shareit-server | ||
| container_name: shareit-server | ||
| ports: | ||
| - "9090:9090" | ||
| depends_on: | ||
| - db | ||
| environment: | ||
| - SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/shareit | ||
| - SPRING_DATASOURCE_USERNAME=shareit | ||
| - SPRING_DATASOURCE_PASSWORD=shareit | ||
|
|
||
| db: | ||
| image: postgres:16.1 | ||
| container_name: postgres | ||
| ports: | ||
| - "6541:5432" | ||
| environment: | ||
| - POSTGRES_PASSWORD=shareit | ||
| - POSTGRES_USER=shareit | ||
| - POSTGRES_DB=shareit | ||
| healthcheck: | ||
| test: pg_isready -q -d $$POSTGRES_DB -U $$POSTGRES_USER | ||
| timeout: 5s | ||
| interval: 5s | ||
| retries: 10 |
This file contains hidden or 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,5 @@ | ||
| FROM eclipse-temurin:21-jre-jammy | ||
| VOLUME /tmp | ||
| ARG JAR_FILE=target/*.jar | ||
| COPY ${JAR_FILE} app.jar | ||
| ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} -jar /app.jar"] |
This file contains hidden or 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,124 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>ru.practicum</groupId> | ||
| <artifactId>shareit</artifactId> | ||
| <version>0.0.1-SNAPSHOT</version> | ||
| </parent> | ||
|
|
||
| <artifactId>shareit-gateway</artifactId> | ||
| <version>0.0.1-SNAPSHOT</version> | ||
|
|
||
| <name>ShareIt Gateway</name> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-web</artifactId> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-validation</artifactId> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-actuator</artifactId> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.hibernate.validator</groupId> | ||
| <artifactId>hibernate-validator</artifactId> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.apache.httpcomponents.client5</groupId> | ||
| <artifactId>httpclient5</artifactId> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-configuration-processor</artifactId> | ||
| <optional>true</optional> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.projectlombok</groupId> | ||
| <artifactId>lombok</artifactId> | ||
| <optional>true</optional> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-test</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-maven-plugin</artifactId> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.jacoco</groupId> | ||
| <artifactId>jacoco-maven-plugin</artifactId> | ||
| <configuration> | ||
| <excludes> | ||
| <exclude>ru/practicum/shareit/**/dto/*</exclude> | ||
| <exclude>ru/practicum/shareit/ShareItGateway.class</exclude> | ||
| <exclude>ru/practicum/shareit/rest/*.class</exclude> | ||
| </excludes> | ||
| </configuration> | ||
| <executions> | ||
| <execution> | ||
| <id>default-prepare-agent</id> | ||
| <goals> | ||
| <goal>prepare-agent</goal> | ||
| </goals> | ||
| <configuration> | ||
| <destFile>${project.build.directory}/coverage-reports/jacoco.exec</destFile> | ||
| <propertyName>surefireArgLine</propertyName> | ||
| </configuration> | ||
| </execution> | ||
| <execution> | ||
| <id>default-report</id> | ||
| <phase>test</phase> | ||
| <goals> | ||
| <goal>report</goal> | ||
| </goals> | ||
| <configuration> | ||
| <dataFile>${project.build.directory}/coverage-reports/jacoco.exec</dataFile> | ||
| <outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory> | ||
| </configuration> | ||
| </execution> | ||
| <execution> | ||
| <id>default-check</id> | ||
| <goals> | ||
| <goal>check</goal> | ||
| </goals> | ||
| <configuration> | ||
| <rules> | ||
| <rule> | ||
| <element>BUNDLE</element> | ||
| <limits> | ||
| <limit> | ||
| <counter>COMPLEXITY</counter> | ||
| <value>COVEREDRATIO</value> | ||
| <minimum>0.70</minimum> | ||
| </limit> | ||
| </limits> | ||
| </rule> | ||
| </rules> | ||
| </configuration> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| </project> |
12 changes: 12 additions & 0 deletions
12
gateway/src/main/java/ru/practicum/shareit/ShareItGateway.java
This file contains hidden or 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,12 @@ | ||
| package ru.practicum.shareit; | ||
|
|
||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
|
||
| @SpringBootApplication | ||
| public class ShareItGateway { | ||
| public static void main(String[] args) { | ||
| SpringApplication.run(ShareItGateway.class, args); | ||
| } | ||
|
|
||
| } |
79 changes: 79 additions & 0 deletions
79
gateway/src/main/java/ru/practicum/shareit/booking/BookingController.java
This file contains hidden or 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,79 @@ | ||
| package ru.practicum.shareit.booking; | ||
|
|
||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpMethod; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import ru.practicum.shareit.rest.RestQueryBuilder; | ||
| import ru.practicum.shareit.booking.dto.BookingDto; | ||
|
|
||
| @RestController | ||
| @RequestMapping(path = "/bookings") | ||
| @RequiredArgsConstructor | ||
| public class BookingController { | ||
| private final BookingService bookingService; | ||
|
|
||
| @GetMapping("/{id}") | ||
| public ResponseEntity<Object> getBooking( | ||
| @PathVariable long id, | ||
| @RequestHeader("X-Sharer-User-Id") long userId) { | ||
| var request = RestQueryBuilder.builder() | ||
| .method(HttpMethod.GET) | ||
| .path("/" + id) | ||
| .requestHead("X-Sharer-User-Id", String.valueOf(userId)) | ||
| .build(); | ||
|
|
||
| return bookingService.executeQuery(request); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public ResponseEntity<Object> getBookingForUserId( | ||
| @RequestHeader("X-Sharer-User-Id") long userId) { | ||
| var request = RestQueryBuilder.builder() | ||
| .method(HttpMethod.GET) | ||
| .requestHead("X-Sharer-User-Id", String.valueOf(userId)) | ||
| .build(); | ||
|
|
||
| return bookingService.executeQuery(request); | ||
| } | ||
|
|
||
| @GetMapping("/owner") | ||
| public ResponseEntity<Object> getBookingForItemOwnerId( | ||
| @RequestHeader("X-Sharer-User-Id") long ownerId) { | ||
| var request = RestQueryBuilder.builder() | ||
| .method(HttpMethod.GET) | ||
| .path("/owner") | ||
| .requestHead("X-Sharer-User-Id", String.valueOf(ownerId)) | ||
| .build(); | ||
|
|
||
| return bookingService.executeQuery(request); | ||
| } | ||
|
|
||
| @PostMapping | ||
| public ResponseEntity<Object> postBooking( | ||
| @RequestHeader("X-Sharer-User-Id") long userId, | ||
| @Valid @RequestBody BookingDto bookingDto) { | ||
| var request = RestQueryBuilder.builder() | ||
| .method(HttpMethod.POST) | ||
| .requestHead("X-Sharer-User-Id", String.valueOf(userId)) | ||
| .body(bookingDto) | ||
| .build(); | ||
|
|
||
| return bookingService.executeQuery(request); | ||
| } | ||
|
|
||
| @PatchMapping("/{id}") | ||
| public ResponseEntity<Object> patchBooking( | ||
| @PathVariable long id, | ||
| @RequestParam(name = "approved") boolean approved, | ||
| @RequestHeader("X-Sharer-User-Id") long userId) { | ||
| var request = RestQueryBuilder.builder() | ||
| .method(HttpMethod.PATCH) | ||
| .path("/" + id + "?approved=" + (approved ? "true" : "false")) | ||
| .requestHead("X-Sharer-User-Id", String.valueOf(userId)) | ||
| .build(); | ||
|
|
||
| return bookingService.executeQuery(request); | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
gateway/src/main/java/ru/practicum/shareit/booking/BookingService.java
This file contains hidden or 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,26 @@ | ||
| package ru.practicum.shareit.booking; | ||
|
|
||
| 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.client.HttpComponentsClientHttpRequestFactory; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.web.util.DefaultUriBuilderFactory; | ||
| import ru.practicum.shareit.rest.RestService; | ||
|
|
||
| @Service | ||
| public class BookingService extends RestService { | ||
| private static final String API_PREFIX = "/bookings"; | ||
|
|
||
| @Autowired | ||
| public BookingService( | ||
| @Value("${shareit-server.url}") String serverUrl, | ||
| RestTemplateBuilder builder) { | ||
| super( | ||
| builder | ||
| .uriTemplateHandler(new DefaultUriBuilderFactory(serverUrl + API_PREFIX)) | ||
| .requestFactory(() -> new HttpComponentsClientHttpRequestFactory()) | ||
| .build() | ||
| ); | ||
| } | ||
| } | ||
17 changes: 17 additions & 0 deletions
17
gateway/src/main/java/ru/practicum/shareit/booking/dto/BookingDto.java
This file contains hidden or 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,17 @@ | ||
| package ru.practicum.shareit.booking.dto; | ||
|
|
||
|
|
||
| import lombok.Builder; | ||
| import ru.practicum.shareit.item.dto.ItemDto; | ||
| import ru.practicum.shareit.user.dto.UserDto; | ||
|
|
||
| @Builder(toBuilder = true) | ||
| public record BookingDto( | ||
| Long id, | ||
| Long itemId, | ||
| BookingStatus status, | ||
| String start, | ||
| String end, | ||
| UserDto booker, | ||
| ItemDto item | ||
| ){ } |
8 changes: 8 additions & 0 deletions
8
gateway/src/main/java/ru/practicum/shareit/booking/dto/BookingStatus.java
This file contains hidden or 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.booking.dto; | ||
|
|
||
| public enum BookingStatus { | ||
| WAITING, | ||
| APPROVED, | ||
| REJECTED, | ||
| CANCELED | ||
| } |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Эти классы не логично называть Service, т.к. в них нет никакой бизнес-логики по работе с сущностями. Лучше назвать RestClient и BookingClient и пометить аннотацией
@ComponentThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Сервис, потому что стоят за контроллером. А клиентом не хочу называть, т.к. клиент внутри используется.
И бизнес-логика подразумевалась в задании, в описании маршрутизации рест-запросов
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Классы получают аннотацию
@Serviceпотому что они принадлежат сервисному слою приложения, а не потому что стоят за контроллером. Маршрутизация -это не бизнес-логика. В задании как раз говорится, что "shareIt-server будет содержать всю основную логику ", а в shareIt-gateway нужно вынести "всю валидацию входных данных".PS
Если хочешь подчеркнуть, что основная задача класса маршрутизация, то можно назвать Router