From af37e52fc0a1122688573cf701f5783582936762 Mon Sep 17 00:00:00 2001 From: Andersen Date: Thu, 7 Dec 2023 22:28:41 +0300 Subject: [PATCH 1/2] refactor: docker-compose.yml --- docker-compose.yml | 33 +++++ gateway/Dockerfile | 3 + gateway/pom.xml | 65 +++++++++ .../ru/practicum/shareit/ShareitGateway.java | 11 ++ .../shareit/booking/BookingClient.java | 61 +++++++++ .../shareit/booking/BookingController.java | 70 ++++++++++ .../shareit/booking/BookingStatus.java | 0 .../booking/dto/BookItemRequestDto.java | 28 ++++ .../shareit/booking/dto/BookingState.java | 21 +++ .../practicum/shareit/client/BaseClient.java | 124 ++++++++++++++++++ .../exception/EmailDuplicateException.java | 0 .../shareit/exception/ErrorHandler.java | 0 .../shareit/exception/ErrorResponse.java | 0 .../exception/NoDataFoundException.java | 0 .../PostWithoutBookingException.java | 0 .../exception/UnavailableItemException.java | 0 .../exception/UnavailableStateException.java | 0 .../ru/practicum/shareit/item/ItemClient.java | 63 +++++++++ .../shareit/item/ItemGatewayController.java | 69 ++++++++++ .../shareit/item/dto/CommentRequestDto.java | 28 ++++ .../shareit/item/dto/ItemRequestDto.java | 30 +++++ .../shareit/request/RequestClient.java | 48 +++++++ .../request/RequestGatewayController.java | 49 +++++++ .../shareit/request/dto/RequestDto.java | 23 ++++ .../ru/practicum/shareit/user/UserClient.java | 49 +++++++ .../shareit/user/UserGatewayController.java | 49 +++++++ .../shareit/user/dto/UserRequestDto.java | 22 ++++ .../src/main/resources/application.properties | 5 + pom.xml | 11 +- server/Dockerfile | 3 + server/pom.xml | 87 ++++++++++++ .../ru/practicum/shareit/ShareitServer.java | 7 +- .../ru/practicum/shareit/booking/Booking.java | 0 .../shareit/booking/BookingController.java | 1 - .../shareit/booking/BookingMapper.java | 1 + .../shareit/booking/BookingStatus.java | 8 ++ .../booking/dao/BookingRepository.java | 0 .../shareit/booking/dto/BookingDto.java | 0 .../booking/service/BookingService.java | 0 .../booking/service/BookingServiceImpl.java | 0 .../exception/EmailDuplicateException.java | 8 ++ .../shareit/exception/ErrorHandler.java | 41 ++++++ .../shareit/exception/ErrorResponse.java | 22 ++++ .../exception/NoDataFoundException.java | 7 + .../PostWithoutBookingException.java | 8 ++ .../exception/UnavailableItemException.java | 7 + .../exception/UnavailableStateException.java | 7 + .../ru/practicum/shareit/item/Comment.java | 0 .../practicum/shareit/item/CommentMapper.java | 1 + .../shareit/item/ItemController.java | 0 .../practicum/shareit/item/ItemDtoMapper.java | 1 + .../ru/practicum/shareit/item/ItemMapper.java | 1 + .../shareit/item/dao/CommentRepository.java | 0 .../shareit/item/dao/ItemRepository.java | 0 .../shareit/item/dto/CommentDto.java | 0 .../practicum/shareit/item/dto/ItemDto.java | 0 .../shareit/item/dto/ItemRequestDto.java | 0 .../item/dto/ItemRequestDtoMapper.java | 0 .../ru/practicum/shareit/item/model/Item.java | 0 .../shareit/item/service/ItemService.java | 0 .../shareit/item/service/ItemServiceImpl.java | 12 +- .../ru/practicum/shareit/request/Request.java | 0 .../shareit/request/RequestController.java | 0 .../shareit/request/RequestDtoMapper.java | 0 .../shareit/request/RequestMapper.java | 1 + .../request/dao/RequestRepository.java | 2 + .../shareit/request/dto/RequestDto.java | 0 .../request/service/RequestService.java | 1 + .../request/service/RequestServiceImpl.java | 0 .../java/ru/practicum/shareit/user/User.java | 0 .../shareit/user/UserController.java | 0 .../practicum/shareit/user/UserDtoMapper.java | 1 + .../ru/practicum/shareit/user/UserMapper.java | 1 + .../shareit/user/dao/UserRepository.java | 0 .../practicum/shareit/user/dto/UserDto.java | 0 .../shareit/user/service/UserService.java | 0 .../shareit/user/service/UserServiceImpl.java | 0 .../main/resources/application.properties | 20 ++- .../src}/main/resources/booking.json | 0 {src => server/src}/main/resources/item.json | 0 .../src}/main/resources/resource.json | 0 {src => server/src}/main/resources/schema.sql | 0 {src => server/src}/main/resources/user.json | 0 .../shareit/LocalDateTimeAdapter.java | 0 .../ru/practicum/shareit/ShareItTests.java | 0 .../booking/BookingControllerTest.java | 0 .../booking/dao/BookingRepositoryTest.java | 0 .../booking/dto/BookingDtoJsonTest.java | 0 .../BookingServiceImpSpringBootlTest.java | 0 .../service/BookingServiceImplTest.java | 0 .../shareit/exception/ErrorHandlerTest.java | 0 .../shareit/exception/ErrorResponseTest.java | 0 .../shareit/item/ItemControllerTest.java | 0 .../shareit/item/dao/ItemRepositoryTest.java | 0 .../shareit/item/dto/ItemDtoJsonTest.java | 0 .../service/ItemServiceImplSpringBooTest.java | 0 .../item/service/ItemServiceImplTest.java | 0 .../request/RequestControllerTest.java | 0 .../request/dao/RequestRepositoryTest.java | 0 .../request/dto/RequestDtoJsonTest.java | 0 .../RequestServiceImplSpringBootTest.java | 0 .../service/RequestServiceImplTest.java | 0 .../shareit/user/UserControllerTest.java | 0 .../shareit/user/dto/UserDtoJsonTest.java | 0 .../UserServiceImplSpringBootTest.java | 0 .../user/service/UserServiceImplTest.java | 0 106 files changed, 1084 insertions(+), 26 deletions(-) create mode 100644 docker-compose.yml create mode 100644 gateway/Dockerfile create mode 100644 gateway/pom.xml create mode 100644 gateway/src/main/java/ru/practicum/shareit/ShareitGateway.java create mode 100644 gateway/src/main/java/ru/practicum/shareit/booking/BookingClient.java create mode 100644 gateway/src/main/java/ru/practicum/shareit/booking/BookingController.java rename {src => gateway/src}/main/java/ru/practicum/shareit/booking/BookingStatus.java (100%) create mode 100644 gateway/src/main/java/ru/practicum/shareit/booking/dto/BookItemRequestDto.java create mode 100644 gateway/src/main/java/ru/practicum/shareit/booking/dto/BookingState.java create mode 100644 gateway/src/main/java/ru/practicum/shareit/client/BaseClient.java rename {src => gateway/src}/main/java/ru/practicum/shareit/exception/EmailDuplicateException.java (100%) rename {src => gateway/src}/main/java/ru/practicum/shareit/exception/ErrorHandler.java (100%) rename {src => gateway/src}/main/java/ru/practicum/shareit/exception/ErrorResponse.java (100%) rename {src => gateway/src}/main/java/ru/practicum/shareit/exception/NoDataFoundException.java (100%) rename {src => gateway/src}/main/java/ru/practicum/shareit/exception/PostWithoutBookingException.java (100%) rename {src => gateway/src}/main/java/ru/practicum/shareit/exception/UnavailableItemException.java (100%) rename {src => gateway/src}/main/java/ru/practicum/shareit/exception/UnavailableStateException.java (100%) create mode 100644 gateway/src/main/java/ru/practicum/shareit/item/ItemClient.java create mode 100644 gateway/src/main/java/ru/practicum/shareit/item/ItemGatewayController.java create mode 100644 gateway/src/main/java/ru/practicum/shareit/item/dto/CommentRequestDto.java create mode 100644 gateway/src/main/java/ru/practicum/shareit/item/dto/ItemRequestDto.java create mode 100644 gateway/src/main/java/ru/practicum/shareit/request/RequestClient.java create mode 100644 gateway/src/main/java/ru/practicum/shareit/request/RequestGatewayController.java create mode 100644 gateway/src/main/java/ru/practicum/shareit/request/dto/RequestDto.java create mode 100644 gateway/src/main/java/ru/practicum/shareit/user/UserClient.java create mode 100644 gateway/src/main/java/ru/practicum/shareit/user/UserGatewayController.java create mode 100644 gateway/src/main/java/ru/practicum/shareit/user/dto/UserRequestDto.java create mode 100644 gateway/src/main/resources/application.properties create mode 100644 server/Dockerfile create mode 100644 server/pom.xml rename src/main/java/ru/practicum/shareit/ShareItApp.java => server/src/main/java/ru/practicum/shareit/ShareitServer.java (71%) rename {src => server/src}/main/java/ru/practicum/shareit/booking/Booking.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/booking/BookingController.java (99%) rename {src => server/src}/main/java/ru/practicum/shareit/booking/BookingMapper.java (99%) create mode 100644 server/src/main/java/ru/practicum/shareit/booking/BookingStatus.java rename {src => server/src}/main/java/ru/practicum/shareit/booking/dao/BookingRepository.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/booking/dto/BookingDto.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/booking/service/BookingService.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/booking/service/BookingServiceImpl.java (100%) create mode 100644 server/src/main/java/ru/practicum/shareit/exception/EmailDuplicateException.java create mode 100644 server/src/main/java/ru/practicum/shareit/exception/ErrorHandler.java create mode 100644 server/src/main/java/ru/practicum/shareit/exception/ErrorResponse.java create mode 100644 server/src/main/java/ru/practicum/shareit/exception/NoDataFoundException.java create mode 100644 server/src/main/java/ru/practicum/shareit/exception/PostWithoutBookingException.java create mode 100644 server/src/main/java/ru/practicum/shareit/exception/UnavailableItemException.java create mode 100644 server/src/main/java/ru/practicum/shareit/exception/UnavailableStateException.java rename {src => server/src}/main/java/ru/practicum/shareit/item/Comment.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/item/CommentMapper.java (99%) rename {src => server/src}/main/java/ru/practicum/shareit/item/ItemController.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/item/ItemDtoMapper.java (99%) rename {src => server/src}/main/java/ru/practicum/shareit/item/ItemMapper.java (99%) rename {src => server/src}/main/java/ru/practicum/shareit/item/dao/CommentRepository.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/item/dao/ItemRepository.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/item/dto/CommentDto.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/item/dto/ItemDto.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/item/dto/ItemRequestDto.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/item/dto/ItemRequestDtoMapper.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/item/model/Item.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/item/service/ItemService.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/item/service/ItemServiceImpl.java (96%) rename {src => server/src}/main/java/ru/practicum/shareit/request/Request.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/request/RequestController.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/request/RequestDtoMapper.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/request/RequestMapper.java (99%) rename {src => server/src}/main/java/ru/practicum/shareit/request/dao/RequestRepository.java (91%) rename {src => server/src}/main/java/ru/practicum/shareit/request/dto/RequestDto.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/request/service/RequestService.java (99%) rename {src => server/src}/main/java/ru/practicum/shareit/request/service/RequestServiceImpl.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/user/User.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/user/UserController.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/user/UserDtoMapper.java (99%) rename {src => server/src}/main/java/ru/practicum/shareit/user/UserMapper.java (99%) rename {src => server/src}/main/java/ru/practicum/shareit/user/dao/UserRepository.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/user/dto/UserDto.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/user/service/UserService.java (100%) rename {src => server/src}/main/java/ru/practicum/shareit/user/service/UserServiceImpl.java (100%) rename {src => server/src}/main/resources/application.properties (57%) rename {src => server/src}/main/resources/booking.json (100%) rename {src => server/src}/main/resources/item.json (100%) rename {src => server/src}/main/resources/resource.json (100%) rename {src => server/src}/main/resources/schema.sql (100%) rename {src => server/src}/main/resources/user.json (100%) rename {src => server/src}/test/java/ru/practicum/shareit/LocalDateTimeAdapter.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/ShareItTests.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/booking/BookingControllerTest.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/booking/dao/BookingRepositoryTest.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/booking/dto/BookingDtoJsonTest.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/booking/service/BookingServiceImpSpringBootlTest.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/booking/service/BookingServiceImplTest.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/exception/ErrorHandlerTest.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/exception/ErrorResponseTest.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/item/ItemControllerTest.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/item/dao/ItemRepositoryTest.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/item/dto/ItemDtoJsonTest.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/item/service/ItemServiceImplSpringBooTest.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/item/service/ItemServiceImplTest.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/request/RequestControllerTest.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/request/dao/RequestRepositoryTest.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/request/dto/RequestDtoJsonTest.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/request/service/RequestServiceImplSpringBootTest.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/request/service/RequestServiceImplTest.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/user/UserControllerTest.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/user/dto/UserDtoJsonTest.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/user/service/UserServiceImplSpringBootTest.java (100%) rename {src => server/src}/test/java/ru/practicum/shareit/user/service/UserServiceImplTest.java (100%) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..a914772 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,33 @@ +version: '3.8' +services: + gateway: + build: ./gateway + container_name: gateway_container + ports: + - "8080:8080" + depends_on: + - server + environment: + - SHAREIT_SERVER_URL=http://server:9090 + + server: + build: ./server + container_name: server_container + ports: + - "9090:9090" + depends_on: + - db + environment: + - SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/shareit + + db: + image: postgres:13.7-alpine + container_name: data_base + ports: + - "6541:5432" + environment: + - POSTGRES_DB=shareit + - POSTGRES_USER=andersen + - POSTGRES_PASSWORD=andersen + - DB_HOST=db + - DB_PORT=5432 \ No newline at end of file diff --git a/gateway/Dockerfile b/gateway/Dockerfile new file mode 100644 index 0000000..0939c6e --- /dev/null +++ b/gateway/Dockerfile @@ -0,0 +1,3 @@ +FROM amazoncorretto:11 +COPY target/*.jar app.jar +ENTRYPOINT ["java","-jar","/app.jar"] \ No newline at end of file diff --git a/gateway/pom.xml b/gateway/pom.xml new file mode 100644 index 0000000..d1e9599 --- /dev/null +++ b/gateway/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + + ru.practicum + shareit + 0.0.1-SNAPSHOT + + + shareit-gateway + 0.0.1-SNAPSHOT + + ShareIt Gateway + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-validation + + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.apache.httpcomponents + httpclient + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + org.projectlombok + lombok + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/gateway/src/main/java/ru/practicum/shareit/ShareitGateway.java b/gateway/src/main/java/ru/practicum/shareit/ShareitGateway.java new file mode 100644 index 0000000..f2fdd71 --- /dev/null +++ b/gateway/src/main/java/ru/practicum/shareit/ShareitGateway.java @@ -0,0 +1,11 @@ +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); + } +} \ No newline at end of file diff --git a/gateway/src/main/java/ru/practicum/shareit/booking/BookingClient.java b/gateway/src/main/java/ru/practicum/shareit/booking/BookingClient.java new file mode 100644 index 0000000..00c38e0 --- /dev/null +++ b/gateway/src/main/java/ru/practicum/shareit/booking/BookingClient.java @@ -0,0 +1,61 @@ +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.ResponseEntity; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.stereotype.Service; +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; + +import java.util.Map; + +@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(HttpComponentsClientHttpRequestFactory::new) + .build() + ); + } + + public ResponseEntity postBooking(long userId, BookItemRequestDto bookItemRequestDto) { + return post("", userId, bookItemRequestDto); + } + + public ResponseEntity confirmationOrRejectionBooking(long bookingId, long userId, String bookingStatus) { + Map parameters = Map.of( + "approved", bookingStatus); + + return patch("/" + bookingId + "?approved={approved}", userId, parameters); + } + + public ResponseEntity getBookingById(long userId, long bookingId) { + return get("/" + bookingId, userId); + } + + public ResponseEntity getAllBookingCurrentUser(long userId, BookingState state, Integer from, Integer size) { + Map parameters = Map.of( + "state", state.name(), + "from", from, + "size", size + ); + return get("?state={state}&from={from}&size={size}", userId, parameters); + } + + public ResponseEntity getAllBookingCurrentOwner(long userId, BookingState state, Integer from, Integer size) { + Map parameters = Map.of( + "state", state.name(), + "from", from, + "size", size); + return get("/owner?state={state}&from={from}&size={size}", userId, parameters); + } +} \ No newline at end of file diff --git a/gateway/src/main/java/ru/practicum/shareit/booking/BookingController.java b/gateway/src/main/java/ru/practicum/shareit/booking/BookingController.java new file mode 100644 index 0000000..e948437 --- /dev/null +++ b/gateway/src/main/java/ru/practicum/shareit/booking/BookingController.java @@ -0,0 +1,70 @@ +package ru.practicum.shareit.booking; + +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.booking.dto.BookItemRequestDto; +import ru.practicum.shareit.booking.dto.BookingState; +import ru.practicum.shareit.exception.UnavailableStateException; + +import javax.validation.constraints.Positive; +import javax.validation.constraints.PositiveOrZero; + +@Controller +@RequestMapping(path = "/bookings") +@RequiredArgsConstructor +@Slf4j +@Validated +public class BookingController { + private final BookingClient bookingClient; + + @PostMapping + public ResponseEntity postBooking(@RequestBody BookItemRequestDto bookItemRequestDto, + @RequestHeader("X-Sharer-User-Id") int userId) { + return bookingClient.postBooking(userId, bookItemRequestDto); + } + + @PatchMapping("/{bookingId}") + public ResponseEntity confirmationOrRejectionBooking(@PathVariable int bookingId, + @RequestHeader("X-Sharer-User-Id") int userId, + @RequestParam(name = "approved") + String bookingStatus) { + + return bookingClient.confirmationOrRejectionBooking(bookingId, userId, bookingStatus); + } + + @GetMapping("/{bookingId}") + public ResponseEntity getBookingById(@RequestHeader("X-Sharer-User-Id") int userId, + @PathVariable int bookingId) { + return bookingClient.getBookingById(userId, bookingId); + } + + @GetMapping + public ResponseEntity getAllBookingCurrentUser(@RequestHeader("X-Sharer-User-Id") 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 UnavailableStateException("Unknown state: UNSUPPORTED_STATUS")); + log.info("Get booking with state {}, userId={}, from={}, size={}", stateParam, userId, from, size); + return bookingClient.getAllBookingCurrentUser(userId, state, from, size); + } + + @GetMapping("/owner") + public ResponseEntity getAllBookingCurrentOwner(@RequestHeader("X-Sharer-User-Id") 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 UnavailableStateException("Unknown state: UNSUPPORTED_STATUS")); + return bookingClient.getAllBookingCurrentOwner(userId, state, from, size); + } +} \ No newline at end of file diff --git a/src/main/java/ru/practicum/shareit/booking/BookingStatus.java b/gateway/src/main/java/ru/practicum/shareit/booking/BookingStatus.java similarity index 100% rename from src/main/java/ru/practicum/shareit/booking/BookingStatus.java rename to gateway/src/main/java/ru/practicum/shareit/booking/BookingStatus.java diff --git a/gateway/src/main/java/ru/practicum/shareit/booking/dto/BookItemRequestDto.java b/gateway/src/main/java/ru/practicum/shareit/booking/dto/BookItemRequestDto.java new file mode 100644 index 0000000..8ec83b5 --- /dev/null +++ b/gateway/src/main/java/ru/practicum/shareit/booking/dto/BookItemRequestDto.java @@ -0,0 +1,28 @@ +package ru.practicum.shareit.booking.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import ru.practicum.shareit.booking.BookingStatus; +import ru.practicum.shareit.item.dto.ItemRequestDto; +import ru.practicum.shareit.user.dto.UserRequestDto; + +import javax.validation.constraints.Future; +import javax.validation.constraints.FutureOrPresent; +import java.time.LocalDateTime; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class BookItemRequestDto { + private int id; + private long itemId; + @FutureOrPresent + private LocalDateTime start; + @Future + private LocalDateTime end; + private UserRequestDto booker; + private ItemRequestDto item; + private BookingStatus status; + int bookerId; +} \ No newline at end of file diff --git a/gateway/src/main/java/ru/practicum/shareit/booking/dto/BookingState.java b/gateway/src/main/java/ru/practicum/shareit/booking/dto/BookingState.java new file mode 100644 index 0000000..f98fb8f --- /dev/null +++ b/gateway/src/main/java/ru/practicum/shareit/booking/dto/BookingState.java @@ -0,0 +1,21 @@ +package ru.practicum.shareit.booking.dto; + +import java.util.Optional; + +public enum BookingState { + ALL, + CURRENT, + FUTURE, + PAST, + REJECTED, + WAITING; + + public static Optional from(String stringState) { + for (BookingState state : values()) { + if (state.name().equalsIgnoreCase(stringState)) { + return Optional.of(state); + } + } + return Optional.empty(); + } +} \ No newline at end of file diff --git a/gateway/src/main/java/ru/practicum/shareit/client/BaseClient.java b/gateway/src/main/java/ru/practicum/shareit/client/BaseClient.java new file mode 100644 index 0000000..1ef1961 --- /dev/null +++ b/gateway/src/main/java/ru/practicum/shareit/client/BaseClient.java @@ -0,0 +1,124 @@ +package ru.practicum.shareit.client; + +import org.springframework.http.*; +import org.springframework.lang.Nullable; +import org.springframework.web.client.HttpStatusCodeException; +import org.springframework.web.client.RestTemplate; + +import java.util.List; +import java.util.Map; + +public class BaseClient { + + protected final RestTemplate rest; + + public BaseClient(RestTemplate rest) { + this.rest = rest; + } + + protected ResponseEntity get(String path) { + return get(path, null, null); + } + + protected ResponseEntity get(String path, long userId) { + return get(path, userId, null); + } + + protected ResponseEntity get(String path, Long userId, @Nullable Map parameters) { + return makeAndSendRequest(HttpMethod.GET, path, userId, parameters, null); + } + + protected ResponseEntity post(String path, T body) { + return post(path, null, null, body); + } + + protected ResponseEntity post(String path, long userId, T body) { + return post(path, userId, null, body); + } + + protected ResponseEntity post(String path, Long userId, @Nullable Map parameters, T body) { + return makeAndSendRequest(HttpMethod.POST, path, userId, parameters, body); + } + + protected ResponseEntity put(String path, long userId, T body) { + return put(path, userId, null, body); + } + + protected ResponseEntity put(String path, long userId, @Nullable Map parameters, T body) { + return makeAndSendRequest(HttpMethod.PUT, path, userId, parameters, body); + } + + protected ResponseEntity patch(String path, T body) { + return patch(path, null, null, body); + } + + protected ResponseEntity patch(String path, long userId) { + return patch(path, userId, null, null); + } + + protected ResponseEntity patch(String path, long userId, Map parameters) { + return patch(path, userId, parameters, null); + } + + protected ResponseEntity patch(String path, long userId, T body) { + return patch(path, userId, null, body); + } + + + protected ResponseEntity patch(String path, Long userId, @Nullable Map parameters, T body) { + return makeAndSendRequest(HttpMethod.PATCH, path, userId, parameters, body); + } + + protected ResponseEntity delete(String path) { + return delete(path, null, null); + } + + protected ResponseEntity delete(String path, long userId) { + return delete(path, userId, null); + } + + protected ResponseEntity delete(String path, Long userId, @Nullable Map parameters) { + return makeAndSendRequest(HttpMethod.DELETE, path, userId, parameters, null); + } + + private ResponseEntity makeAndSendRequest(HttpMethod method, String path, Long userId, @Nullable Map parameters, @Nullable T body) { + HttpEntity requestEntity = new HttpEntity<>(body, defaultHeaders(userId)); + + ResponseEntity shareitServerResponse; + try { + if (parameters != null) { + shareitServerResponse = rest.exchange(path, method, requestEntity, Object.class, parameters); + } else { + shareitServerResponse = rest.exchange(path, method, requestEntity, Object.class); + } + } catch (HttpStatusCodeException e) { + return ResponseEntity.status(e.getStatusCode()).body(e.getResponseBodyAsByteArray()); + } + return prepareGatewayResponse(shareitServerResponse); + } + + private HttpHeaders defaultHeaders(Long userId) { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + headers.setAccept(List.of(MediaType.APPLICATION_JSON)); + if (userId != null) { + headers.set("X-Sharer-User-Id", String.valueOf(userId)); + } + return headers; + } + + private static ResponseEntity prepareGatewayResponse(ResponseEntity response) { + if (response.getStatusCode().is2xxSuccessful()) { + return response; + } + + ResponseEntity.BodyBuilder responseBuilder = ResponseEntity.status(response.getStatusCode()); + + if (response.hasBody()) { + return responseBuilder.body(response.getBody()); + } + + return responseBuilder.build(); + } + +} diff --git a/src/main/java/ru/practicum/shareit/exception/EmailDuplicateException.java b/gateway/src/main/java/ru/practicum/shareit/exception/EmailDuplicateException.java similarity index 100% rename from src/main/java/ru/practicum/shareit/exception/EmailDuplicateException.java rename to gateway/src/main/java/ru/practicum/shareit/exception/EmailDuplicateException.java diff --git a/src/main/java/ru/practicum/shareit/exception/ErrorHandler.java b/gateway/src/main/java/ru/practicum/shareit/exception/ErrorHandler.java similarity index 100% rename from src/main/java/ru/practicum/shareit/exception/ErrorHandler.java rename to gateway/src/main/java/ru/practicum/shareit/exception/ErrorHandler.java diff --git a/src/main/java/ru/practicum/shareit/exception/ErrorResponse.java b/gateway/src/main/java/ru/practicum/shareit/exception/ErrorResponse.java similarity index 100% rename from src/main/java/ru/practicum/shareit/exception/ErrorResponse.java rename to gateway/src/main/java/ru/practicum/shareit/exception/ErrorResponse.java diff --git a/src/main/java/ru/practicum/shareit/exception/NoDataFoundException.java b/gateway/src/main/java/ru/practicum/shareit/exception/NoDataFoundException.java similarity index 100% rename from src/main/java/ru/practicum/shareit/exception/NoDataFoundException.java rename to gateway/src/main/java/ru/practicum/shareit/exception/NoDataFoundException.java diff --git a/src/main/java/ru/practicum/shareit/exception/PostWithoutBookingException.java b/gateway/src/main/java/ru/practicum/shareit/exception/PostWithoutBookingException.java similarity index 100% rename from src/main/java/ru/practicum/shareit/exception/PostWithoutBookingException.java rename to gateway/src/main/java/ru/practicum/shareit/exception/PostWithoutBookingException.java diff --git a/src/main/java/ru/practicum/shareit/exception/UnavailableItemException.java b/gateway/src/main/java/ru/practicum/shareit/exception/UnavailableItemException.java similarity index 100% rename from src/main/java/ru/practicum/shareit/exception/UnavailableItemException.java rename to gateway/src/main/java/ru/practicum/shareit/exception/UnavailableItemException.java diff --git a/src/main/java/ru/practicum/shareit/exception/UnavailableStateException.java b/gateway/src/main/java/ru/practicum/shareit/exception/UnavailableStateException.java similarity index 100% rename from src/main/java/ru/practicum/shareit/exception/UnavailableStateException.java rename to gateway/src/main/java/ru/practicum/shareit/exception/UnavailableStateException.java diff --git a/gateway/src/main/java/ru/practicum/shareit/item/ItemClient.java b/gateway/src/main/java/ru/practicum/shareit/item/ItemClient.java new file mode 100644 index 0000000..179e222 --- /dev/null +++ b/gateway/src/main/java/ru/practicum/shareit/item/ItemClient.java @@ -0,0 +1,63 @@ +package ru.practicum.shareit.item; + +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.client.BaseClient; +import ru.practicum.shareit.item.dto.CommentRequestDto; +import ru.practicum.shareit.item.dto.ItemRequestDto; + +import java.util.Map; + +@Service +public class ItemClient extends BaseClient { + + private static final String API_PREFIX = "/items"; + + @Autowired + public ItemClient(@Value("${shareit-server.url}") String serverUrl, RestTemplateBuilder builder) { + super( + builder + .uriTemplateHandler(new DefaultUriBuilderFactory(serverUrl + API_PREFIX)) + .requestFactory(HttpComponentsClientHttpRequestFactory::new) + .build() + ); + } + + public ResponseEntity postItem(long userId, ItemRequestDto itemRequestDto) { + return post("", userId, itemRequestDto); + } + + public ResponseEntity getItemById(int itemId, int userId) { + return get("/" + itemId, userId); + } + + + public ResponseEntity getAllItemForOwner(long userId, Integer from, Integer size) { + Map parameters = Map.of( + "from", from, + "size", size + ); + return get("?from={from}&size={size}", userId, parameters); + } + + public ResponseEntity updateItem(long userId, long itemId, Map body) { + return patch("/" + itemId, userId, body); + } + + public ResponseEntity searchItem(String request, long userId, Integer from, Integer size) { + Map parameters = Map.of( + "request", request, + "from", from, + "size", size); + return get("/search?text={request}", userId, parameters); + } + + public ResponseEntity addComment(long itemId, long userId, CommentRequestDto commentRequestDto) { + return post("/" + itemId + "/comment", userId, commentRequestDto); + } +} diff --git a/gateway/src/main/java/ru/practicum/shareit/item/ItemGatewayController.java b/gateway/src/main/java/ru/practicum/shareit/item/ItemGatewayController.java new file mode 100644 index 0000000..699517f --- /dev/null +++ b/gateway/src/main/java/ru/practicum/shareit/item/ItemGatewayController.java @@ -0,0 +1,69 @@ +package ru.practicum.shareit.item; + +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.item.dto.CommentRequestDto; +import ru.practicum.shareit.item.dto.ItemRequestDto; + +import javax.validation.Valid; +import javax.validation.constraints.Positive; +import javax.validation.constraints.PositiveOrZero; +import java.util.Map; + +@Controller +@RequestMapping(path = "/items") +@RequiredArgsConstructor +@Slf4j +@Validated +public class ItemGatewayController { + + private final ItemClient itemClient; + + @PostMapping + public ResponseEntity postItem(@RequestHeader("X-Sharer-User-Id") int userId, + @Valid @RequestBody ItemRequestDto itemRequestDto) { + return itemClient.postItem(userId, itemRequestDto); + } + + @GetMapping("/{id}") + public ResponseEntity getItemById(@PathVariable(name = "id") int itemId, + @RequestHeader("X-Sharer-User-Id") int userId) { + return itemClient.getItemById(itemId, userId); + } + + @GetMapping + public ResponseEntity getAllItemForOwner(@RequestHeader("X-Sharer-User-Id") int userId, + @PositiveOrZero @RequestParam(name = "from", + defaultValue = "0") Integer from, + @Positive @RequestParam(name = "size", + defaultValue = "10") Integer size) { + return itemClient.getAllItemForOwner(userId, from, size); + } + + @PatchMapping("/{id}") + public ResponseEntity updateItem(@RequestHeader("X-Sharer-User-Id") int userId, + @PathVariable(name = "id") int itemId, + @RequestBody Map fields) { + return itemClient.updateItem(userId, itemId, fields); + } + + @GetMapping("/search") + public ResponseEntity searchItem(@RequestParam(name = "text") String request, + @RequestHeader("X-Sharer-User-Id") int userId, + @PositiveOrZero @RequestParam(name = "from", + defaultValue = "0") Integer from, + @Positive @RequestParam(name = "size", + defaultValue = "10") Integer size) { + return itemClient.searchItem(request, userId, from, size); + } + + @PostMapping("/{itemId}/comment") + public ResponseEntity addComment(@PathVariable int itemId, @RequestHeader("X-Sharer-User-Id") + int userId, @RequestBody CommentRequestDto commentRequestDto) { + return itemClient.addComment(itemId, userId, commentRequestDto); + } +} \ No newline at end of file diff --git a/gateway/src/main/java/ru/practicum/shareit/item/dto/CommentRequestDto.java b/gateway/src/main/java/ru/practicum/shareit/item/dto/CommentRequestDto.java new file mode 100644 index 0000000..6f8568a --- /dev/null +++ b/gateway/src/main/java/ru/practicum/shareit/item/dto/CommentRequestDto.java @@ -0,0 +1,28 @@ +package ru.practicum.shareit.item.dto; + +import lombok.Getter; +import lombok.Setter; +import lombok.experimental.Accessors; +import org.springframework.stereotype.Component; +import ru.practicum.shareit.user.dto.UserRequestDto; + +import java.time.LocalDateTime; + +@Component +@Getter +@Setter +@Accessors(chain = true) +public class CommentRequestDto { + + private int id; + + private String text; + + private ItemRequestDto item; + + private UserRequestDto author; + + private LocalDateTime created; + + String authorName; +} diff --git a/gateway/src/main/java/ru/practicum/shareit/item/dto/ItemRequestDto.java b/gateway/src/main/java/ru/practicum/shareit/item/dto/ItemRequestDto.java new file mode 100644 index 0000000..e230cb4 --- /dev/null +++ b/gateway/src/main/java/ru/practicum/shareit/item/dto/ItemRequestDto.java @@ -0,0 +1,30 @@ +package ru.practicum.shareit.item.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import ru.practicum.shareit.booking.dto.BookItemRequestDto; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import java.util.ArrayList; +import java.util.List; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class ItemRequestDto { + private int id; + @NotBlank(message = "Поле \'name\' не может быть пустым.") + @NotNull(message = "Поле \'name\' не может быть пустым.") + private String name; + @NotBlank(message = "Поле \'description\' не может быть пустым.") + private String description; + @NotNull(message = "Поле \'available\' не может быть пустым.") + private Boolean available; + private int ownerId; + private Integer requestId; + private BookItemRequestDto lastBooking; + private BookItemRequestDto nextBooking; + private List comments = new ArrayList<>(); +} diff --git a/gateway/src/main/java/ru/practicum/shareit/request/RequestClient.java b/gateway/src/main/java/ru/practicum/shareit/request/RequestClient.java new file mode 100644 index 0000000..c66bcc3 --- /dev/null +++ b/gateway/src/main/java/ru/practicum/shareit/request/RequestClient.java @@ -0,0 +1,48 @@ +package ru.practicum.shareit.request; + +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.client.BaseClient; +import ru.practicum.shareit.request.dto.RequestDto; + +import java.util.Map; + +@Service +public class RequestClient extends BaseClient { + + private static final String API_PREFIX = "/requests"; + + @Autowired + public RequestClient(@Value("${shareit-server.url}") String serverUrl, RestTemplateBuilder builder) { + super( + builder + .uriTemplateHandler(new DefaultUriBuilderFactory(serverUrl + API_PREFIX)) + .requestFactory(HttpComponentsClientHttpRequestFactory::new) + .build() + ); + } + + public ResponseEntity postRequest(long userId, RequestDto requestDto) { + return post("", userId, requestDto); + } + + public ResponseEntity getRequestsList(long userId) { + return get("", userId); + } + + public ResponseEntity getRequestById(long userId, long requestId) { + return get("/" + requestId, userId); + } + + public ResponseEntity getAllRequestsList(long userId, Integer from, Integer size) { + Map parameters = Map.of( + "from", from, + "size", size); + return get("/all?from={from}&size={size}", userId, parameters); + } +} diff --git a/gateway/src/main/java/ru/practicum/shareit/request/RequestGatewayController.java b/gateway/src/main/java/ru/practicum/shareit/request/RequestGatewayController.java new file mode 100644 index 0000000..919cfa6 --- /dev/null +++ b/gateway/src/main/java/ru/practicum/shareit/request/RequestGatewayController.java @@ -0,0 +1,49 @@ +package ru.practicum.shareit.request; + +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.request.dto.RequestDto; + +import javax.validation.Valid; +import javax.validation.constraints.Positive; +import javax.validation.constraints.PositiveOrZero; + +@Controller +@RequestMapping(path = "/requests") +@RequiredArgsConstructor +@Slf4j +@Validated +public class RequestGatewayController { + + private final RequestClient requestClient; + + @PostMapping + public ResponseEntity postRequest(@Valid @RequestBody RequestDto request, + @RequestHeader("X-Sharer-User-Id") int userId) { + return requestClient.postRequest(userId, request); + } + + @GetMapping + public ResponseEntity getRequestsList(@RequestHeader("X-Sharer-User-Id") int userId) { + return requestClient.getRequestsList(userId); + } + + @GetMapping("/{requestId}") + public ResponseEntity getRequestById(@RequestHeader("X-Sharer-User-Id") int userId, + @PathVariable(name = "requestId") int requestId) { + return requestClient.getRequestById(userId, requestId); + } + + @GetMapping("/all") + public ResponseEntity getAllRequestsList(@RequestHeader("X-Sharer-User-Id") int userId, + @PositiveOrZero @RequestParam(name = "from", + defaultValue = "0") Integer from, + @Positive @RequestParam(name = "size", + defaultValue = "10") Integer size) { + return requestClient.getAllRequestsList(userId, from, size); + } +} diff --git a/gateway/src/main/java/ru/practicum/shareit/request/dto/RequestDto.java b/gateway/src/main/java/ru/practicum/shareit/request/dto/RequestDto.java new file mode 100644 index 0000000..df216c0 --- /dev/null +++ b/gateway/src/main/java/ru/practicum/shareit/request/dto/RequestDto.java @@ -0,0 +1,23 @@ +package ru.practicum.shareit.request.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import ru.practicum.shareit.item.dto.ItemRequestDto; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import java.time.LocalDateTime; +import java.util.List; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class RequestDto { + private int id; + @NotBlank(message = "Поле не может быть пустым!") + @NotNull(message = "Поле не может быть пустым!") + private String description; + private LocalDateTime created; + private List items; +} diff --git a/gateway/src/main/java/ru/practicum/shareit/user/UserClient.java b/gateway/src/main/java/ru/practicum/shareit/user/UserClient.java new file mode 100644 index 0000000..0a1aaa2 --- /dev/null +++ b/gateway/src/main/java/ru/practicum/shareit/user/UserClient.java @@ -0,0 +1,49 @@ +package ru.practicum.shareit.user; + +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.client.BaseClient; +import ru.practicum.shareit.user.dto.UserRequestDto; + +import java.util.Map; + +@Service +public class UserClient extends BaseClient { + private static final String API_PREFIX = "/users"; + + @Autowired + public UserClient(@Value("${shareit-server.url}") String serverUrl, RestTemplateBuilder builder) { + super( + builder + .uriTemplateHandler(new DefaultUriBuilderFactory(serverUrl + API_PREFIX)) + .requestFactory(HttpComponentsClientHttpRequestFactory::new) + .build() + ); + } + + public ResponseEntity postUser(UserRequestDto userRequestDto) { + return post("", userRequestDto); + } + + public ResponseEntity getAllUsers() { + return get(""); + } + + public ResponseEntity getUserById(long userId) { + return get("/" + userId); + } + + public ResponseEntity removeUserById(long userId) { + return delete("/" + userId); + } + + public ResponseEntity updateUser(long userId, Map body) { + return patch("/" + userId, body); + } + +} diff --git a/gateway/src/main/java/ru/practicum/shareit/user/UserGatewayController.java b/gateway/src/main/java/ru/practicum/shareit/user/UserGatewayController.java new file mode 100644 index 0000000..5ffc1dc --- /dev/null +++ b/gateway/src/main/java/ru/practicum/shareit/user/UserGatewayController.java @@ -0,0 +1,49 @@ +package ru.practicum.shareit.user; + +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.user.dto.UserRequestDto; + +import javax.validation.Valid; +import java.util.Map; + +@Controller +@RequestMapping(path = "/users") +@RequiredArgsConstructor +@Slf4j +@Validated +public class UserGatewayController { + + private final UserClient userClient; + + @PostMapping + public ResponseEntity postUser(@Valid @RequestBody UserRequestDto userRequestDto) { + return userClient.postUser(userRequestDto); + } + + @GetMapping + public ResponseEntity postUser() { + return userClient.getAllUsers(); + } + + @GetMapping("/{id}") + public ResponseEntity getUserById(@PathVariable(name = "id") int userId) { + return userClient.getUserById(userId); + } + + @DeleteMapping("/{id}") + public ResponseEntity removeUserById(@PathVariable(name = "id") int userId) { + return userClient.removeUserById(userId); + } + + @PatchMapping("/{id}") + public ResponseEntity updateUser(@PathVariable(name = "id") int userId, + @RequestBody Map body) { + return userClient.updateUser(userId, body); + } + +} diff --git a/gateway/src/main/java/ru/practicum/shareit/user/dto/UserRequestDto.java b/gateway/src/main/java/ru/practicum/shareit/user/dto/UserRequestDto.java new file mode 100644 index 0000000..9b06d8c --- /dev/null +++ b/gateway/src/main/java/ru/practicum/shareit/user/dto/UserRequestDto.java @@ -0,0 +1,22 @@ +package ru.practicum.shareit.user.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.Email; +import javax.validation.constraints.NotBlank; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class UserRequestDto { + + private int id; + @NotBlank(message = "Name - не может быть пустым.") + private String name; + @NotBlank(message = "Email - не может быть пустым.") + @Email(message = "Не правильный формат email") + private String email; + +} diff --git a/gateway/src/main/resources/application.properties b/gateway/src/main/resources/application.properties new file mode 100644 index 0000000..9df4b1d --- /dev/null +++ b/gateway/src/main/resources/application.properties @@ -0,0 +1,5 @@ +logging.level.org.springframework.web.client.RestTemplate=DEBUG +#logging.level.org.apache.http=DEBUG +#logging.level.httpclient.wire=DEBUG +server.port=8080 +shareit-server.url=http://localhost:9090 \ No newline at end of file diff --git a/pom.xml b/pom.xml index 6906cbc..33fadc7 100644 --- a/pom.xml +++ b/pom.xml @@ -12,8 +12,13 @@ ru.practicum shareit 0.0.1-SNAPSHOT + pom ShareIt + + gateway + server + 11 @@ -78,12 +83,6 @@ spring-boot-starter-data-jpa - - - org.postgresql - postgresql - 42.6.0 - com.google.code.gson gson diff --git a/server/Dockerfile b/server/Dockerfile new file mode 100644 index 0000000..0939c6e --- /dev/null +++ b/server/Dockerfile @@ -0,0 +1,3 @@ +FROM amazoncorretto:11 +COPY target/*.jar app.jar +ENTRYPOINT ["java","-jar","/app.jar"] \ No newline at end of file diff --git a/server/pom.xml b/server/pom.xml new file mode 100644 index 0000000..705cb14 --- /dev/null +++ b/server/pom.xml @@ -0,0 +1,87 @@ + + + 4.0.0 + + + ru.practicum + shareit + 0.0.1-SNAPSHOT + + + shareit-server + 0.0.1-SNAPSHOT + + ShareIt Server + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.postgresql + postgresql + runtime + + + + com.h2database + h2 + runtime + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + org.projectlombok + lombok + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + coverage + + + + org.jacoco + jacoco-maven-plugin + + + + + + + \ No newline at end of file diff --git a/src/main/java/ru/practicum/shareit/ShareItApp.java b/server/src/main/java/ru/practicum/shareit/ShareitServer.java similarity index 71% rename from src/main/java/ru/practicum/shareit/ShareItApp.java rename to server/src/main/java/ru/practicum/shareit/ShareitServer.java index fe48b8f..05b2013 100644 --- a/src/main/java/ru/practicum/shareit/ShareItApp.java +++ b/server/src/main/java/ru/practicum/shareit/ShareitServer.java @@ -1,12 +1,13 @@ package ru.practicum.shareit; + import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -public class ShareItApp { +public class ShareitServer { public static void main(String[] args) { - SpringApplication.run(ShareItApp.class, args); + SpringApplication.run(ShareitServer.class, args); } -} +} \ No newline at end of file diff --git a/src/main/java/ru/practicum/shareit/booking/Booking.java b/server/src/main/java/ru/practicum/shareit/booking/Booking.java similarity index 100% rename from src/main/java/ru/practicum/shareit/booking/Booking.java rename to server/src/main/java/ru/practicum/shareit/booking/Booking.java diff --git a/src/main/java/ru/practicum/shareit/booking/BookingController.java b/server/src/main/java/ru/practicum/shareit/booking/BookingController.java similarity index 99% rename from src/main/java/ru/practicum/shareit/booking/BookingController.java rename to server/src/main/java/ru/practicum/shareit/booking/BookingController.java index 5a05280..811e9bb 100644 --- a/src/main/java/ru/practicum/shareit/booking/BookingController.java +++ b/server/src/main/java/ru/practicum/shareit/booking/BookingController.java @@ -43,7 +43,6 @@ public List getAllBookingCurrentUser(@RequestHeader("X-Sharer-User-I Optional from, @RequestParam(name = "size", required = false) Optional size) { - return bookingService.getAllBookingCurrentUser(userId, state, from, size); } diff --git a/src/main/java/ru/practicum/shareit/booking/BookingMapper.java b/server/src/main/java/ru/practicum/shareit/booking/BookingMapper.java similarity index 99% rename from src/main/java/ru/practicum/shareit/booking/BookingMapper.java rename to server/src/main/java/ru/practicum/shareit/booking/BookingMapper.java index fd8a294..fb2234d 100644 --- a/src/main/java/ru/practicum/shareit/booking/BookingMapper.java +++ b/server/src/main/java/ru/practicum/shareit/booking/BookingMapper.java @@ -8,6 +8,7 @@ import ru.practicum.shareit.item.ItemDtoMapper; import ru.practicum.shareit.user.UserDtoMapper; + @Component @Getter @Setter diff --git a/server/src/main/java/ru/practicum/shareit/booking/BookingStatus.java b/server/src/main/java/ru/practicum/shareit/booking/BookingStatus.java new file mode 100644 index 0000000..bdd840a --- /dev/null +++ b/server/src/main/java/ru/practicum/shareit/booking/BookingStatus.java @@ -0,0 +1,8 @@ +package ru.practicum.shareit.booking; + +public enum BookingStatus { + WAITING, + APPROVED, + REJECTED, + CANCELED; +} diff --git a/src/main/java/ru/practicum/shareit/booking/dao/BookingRepository.java b/server/src/main/java/ru/practicum/shareit/booking/dao/BookingRepository.java similarity index 100% rename from src/main/java/ru/practicum/shareit/booking/dao/BookingRepository.java rename to server/src/main/java/ru/practicum/shareit/booking/dao/BookingRepository.java diff --git a/src/main/java/ru/practicum/shareit/booking/dto/BookingDto.java b/server/src/main/java/ru/practicum/shareit/booking/dto/BookingDto.java similarity index 100% rename from src/main/java/ru/practicum/shareit/booking/dto/BookingDto.java rename to server/src/main/java/ru/practicum/shareit/booking/dto/BookingDto.java diff --git a/src/main/java/ru/practicum/shareit/booking/service/BookingService.java b/server/src/main/java/ru/practicum/shareit/booking/service/BookingService.java similarity index 100% rename from src/main/java/ru/practicum/shareit/booking/service/BookingService.java rename to server/src/main/java/ru/practicum/shareit/booking/service/BookingService.java diff --git a/src/main/java/ru/practicum/shareit/booking/service/BookingServiceImpl.java b/server/src/main/java/ru/practicum/shareit/booking/service/BookingServiceImpl.java similarity index 100% rename from src/main/java/ru/practicum/shareit/booking/service/BookingServiceImpl.java rename to server/src/main/java/ru/practicum/shareit/booking/service/BookingServiceImpl.java diff --git a/server/src/main/java/ru/practicum/shareit/exception/EmailDuplicateException.java b/server/src/main/java/ru/practicum/shareit/exception/EmailDuplicateException.java new file mode 100644 index 0000000..3c1e0e3 --- /dev/null +++ b/server/src/main/java/ru/practicum/shareit/exception/EmailDuplicateException.java @@ -0,0 +1,8 @@ +package ru.practicum.shareit.exception; + +public class EmailDuplicateException extends RuntimeException { + + public EmailDuplicateException(String message) { + super(message); + } +} diff --git a/server/src/main/java/ru/practicum/shareit/exception/ErrorHandler.java b/server/src/main/java/ru/practicum/shareit/exception/ErrorHandler.java new file mode 100644 index 0000000..7c6d9c8 --- /dev/null +++ b/server/src/main/java/ru/practicum/shareit/exception/ErrorHandler.java @@ -0,0 +1,41 @@ +package ru.practicum.shareit.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class ErrorHandler { + + @ExceptionHandler + @ResponseStatus(HttpStatus.NOT_FOUND) + public ErrorResponse noDataFound(NoDataFoundException e) { + return new ErrorResponse(e.getMessage(), "Данные не найдены."); + } + + @ExceptionHandler + @ResponseStatus(HttpStatus.CONFLICT) + public ErrorResponse emailDuplicate(EmailDuplicateException e) { + return new ErrorResponse(e.getMessage(), "Данный email уже существует."); + } + + @ExceptionHandler + @ResponseStatus(HttpStatus.BAD_REQUEST) + public ErrorResponse unavailableItem(UnavailableItemException e) { + return new ErrorResponse(e.getMessage(), "Данная вещь не доступна для бронирования."); + } + + @ExceptionHandler + @ResponseStatus(HttpStatus.BAD_REQUEST) + public ErrorResponse unavailableState(UnavailableStateException e) { + return new ErrorResponse(e.getMessage(), "Не верный статус."); + } + + @ExceptionHandler + @ResponseStatus(HttpStatus.BAD_REQUEST) + public ErrorResponse postWithoutBooking(PostWithoutBookingException e) { + return new ErrorResponse(e.getMessage(), "Пользователь не может оставить комментарий к данной вещи."); + } + +} diff --git a/server/src/main/java/ru/practicum/shareit/exception/ErrorResponse.java b/server/src/main/java/ru/practicum/shareit/exception/ErrorResponse.java new file mode 100644 index 0000000..e98af6f --- /dev/null +++ b/server/src/main/java/ru/practicum/shareit/exception/ErrorResponse.java @@ -0,0 +1,22 @@ +package ru.practicum.shareit.exception; + +import lombok.Data; + +@Data +public class ErrorResponse { + private String error; + private String description; + + public ErrorResponse(String error, String description) { + this.error = error; + this.description = description; + } + + public String getError() { + return error; + } + + public String getDescription() { + return description; + } +} diff --git a/server/src/main/java/ru/practicum/shareit/exception/NoDataFoundException.java b/server/src/main/java/ru/practicum/shareit/exception/NoDataFoundException.java new file mode 100644 index 0000000..dbef4b4 --- /dev/null +++ b/server/src/main/java/ru/practicum/shareit/exception/NoDataFoundException.java @@ -0,0 +1,7 @@ +package ru.practicum.shareit.exception; + +public class NoDataFoundException extends RuntimeException { + public NoDataFoundException(String message) { + super(message); + } +} diff --git a/server/src/main/java/ru/practicum/shareit/exception/PostWithoutBookingException.java b/server/src/main/java/ru/practicum/shareit/exception/PostWithoutBookingException.java new file mode 100644 index 0000000..93485bb --- /dev/null +++ b/server/src/main/java/ru/practicum/shareit/exception/PostWithoutBookingException.java @@ -0,0 +1,8 @@ +package ru.practicum.shareit.exception; + +public class PostWithoutBookingException extends RuntimeException { + + public PostWithoutBookingException(String message) { + super(message); + } +} diff --git a/server/src/main/java/ru/practicum/shareit/exception/UnavailableItemException.java b/server/src/main/java/ru/practicum/shareit/exception/UnavailableItemException.java new file mode 100644 index 0000000..98a4d11 --- /dev/null +++ b/server/src/main/java/ru/practicum/shareit/exception/UnavailableItemException.java @@ -0,0 +1,7 @@ +package ru.practicum.shareit.exception; + +public class UnavailableItemException extends RuntimeException { + public UnavailableItemException(String message) { + super(message); + } +} diff --git a/server/src/main/java/ru/practicum/shareit/exception/UnavailableStateException.java b/server/src/main/java/ru/practicum/shareit/exception/UnavailableStateException.java new file mode 100644 index 0000000..84e0fbb --- /dev/null +++ b/server/src/main/java/ru/practicum/shareit/exception/UnavailableStateException.java @@ -0,0 +1,7 @@ +package ru.practicum.shareit.exception; + +public class UnavailableStateException extends RuntimeException { + public UnavailableStateException(String message) { + super(message); + } +} diff --git a/src/main/java/ru/practicum/shareit/item/Comment.java b/server/src/main/java/ru/practicum/shareit/item/Comment.java similarity index 100% rename from src/main/java/ru/practicum/shareit/item/Comment.java rename to server/src/main/java/ru/practicum/shareit/item/Comment.java diff --git a/src/main/java/ru/practicum/shareit/item/CommentMapper.java b/server/src/main/java/ru/practicum/shareit/item/CommentMapper.java similarity index 99% rename from src/main/java/ru/practicum/shareit/item/CommentMapper.java rename to server/src/main/java/ru/practicum/shareit/item/CommentMapper.java index a945c86..36590cf 100644 --- a/src/main/java/ru/practicum/shareit/item/CommentMapper.java +++ b/server/src/main/java/ru/practicum/shareit/item/CommentMapper.java @@ -7,6 +7,7 @@ import ru.practicum.shareit.item.dto.CommentDto; import ru.practicum.shareit.user.UserDtoMapper; + @Component @Getter @Setter diff --git a/src/main/java/ru/practicum/shareit/item/ItemController.java b/server/src/main/java/ru/practicum/shareit/item/ItemController.java similarity index 100% rename from src/main/java/ru/practicum/shareit/item/ItemController.java rename to server/src/main/java/ru/practicum/shareit/item/ItemController.java diff --git a/src/main/java/ru/practicum/shareit/item/ItemDtoMapper.java b/server/src/main/java/ru/practicum/shareit/item/ItemDtoMapper.java similarity index 99% rename from src/main/java/ru/practicum/shareit/item/ItemDtoMapper.java rename to server/src/main/java/ru/practicum/shareit/item/ItemDtoMapper.java index 7931d4e..ad41821 100644 --- a/src/main/java/ru/practicum/shareit/item/ItemDtoMapper.java +++ b/server/src/main/java/ru/practicum/shareit/item/ItemDtoMapper.java @@ -4,6 +4,7 @@ import ru.practicum.shareit.item.dto.ItemDto; import ru.practicum.shareit.item.model.Item; + @Component public class ItemDtoMapper { public ItemDto itemDto(Item item) { diff --git a/src/main/java/ru/practicum/shareit/item/ItemMapper.java b/server/src/main/java/ru/practicum/shareit/item/ItemMapper.java similarity index 99% rename from src/main/java/ru/practicum/shareit/item/ItemMapper.java rename to server/src/main/java/ru/practicum/shareit/item/ItemMapper.java index 53bec55..285db75 100644 --- a/src/main/java/ru/practicum/shareit/item/ItemMapper.java +++ b/server/src/main/java/ru/practicum/shareit/item/ItemMapper.java @@ -4,6 +4,7 @@ import ru.practicum.shareit.item.dto.ItemDto; import ru.practicum.shareit.item.model.Item; + @Component public class ItemMapper { diff --git a/src/main/java/ru/practicum/shareit/item/dao/CommentRepository.java b/server/src/main/java/ru/practicum/shareit/item/dao/CommentRepository.java similarity index 100% rename from src/main/java/ru/practicum/shareit/item/dao/CommentRepository.java rename to server/src/main/java/ru/practicum/shareit/item/dao/CommentRepository.java diff --git a/src/main/java/ru/practicum/shareit/item/dao/ItemRepository.java b/server/src/main/java/ru/practicum/shareit/item/dao/ItemRepository.java similarity index 100% rename from src/main/java/ru/practicum/shareit/item/dao/ItemRepository.java rename to server/src/main/java/ru/practicum/shareit/item/dao/ItemRepository.java diff --git a/src/main/java/ru/practicum/shareit/item/dto/CommentDto.java b/server/src/main/java/ru/practicum/shareit/item/dto/CommentDto.java similarity index 100% rename from src/main/java/ru/practicum/shareit/item/dto/CommentDto.java rename to server/src/main/java/ru/practicum/shareit/item/dto/CommentDto.java diff --git a/src/main/java/ru/practicum/shareit/item/dto/ItemDto.java b/server/src/main/java/ru/practicum/shareit/item/dto/ItemDto.java similarity index 100% rename from src/main/java/ru/practicum/shareit/item/dto/ItemDto.java rename to server/src/main/java/ru/practicum/shareit/item/dto/ItemDto.java diff --git a/src/main/java/ru/practicum/shareit/item/dto/ItemRequestDto.java b/server/src/main/java/ru/practicum/shareit/item/dto/ItemRequestDto.java similarity index 100% rename from src/main/java/ru/practicum/shareit/item/dto/ItemRequestDto.java rename to server/src/main/java/ru/practicum/shareit/item/dto/ItemRequestDto.java diff --git a/src/main/java/ru/practicum/shareit/item/dto/ItemRequestDtoMapper.java b/server/src/main/java/ru/practicum/shareit/item/dto/ItemRequestDtoMapper.java similarity index 100% rename from src/main/java/ru/practicum/shareit/item/dto/ItemRequestDtoMapper.java rename to server/src/main/java/ru/practicum/shareit/item/dto/ItemRequestDtoMapper.java diff --git a/src/main/java/ru/practicum/shareit/item/model/Item.java b/server/src/main/java/ru/practicum/shareit/item/model/Item.java similarity index 100% rename from src/main/java/ru/practicum/shareit/item/model/Item.java rename to server/src/main/java/ru/practicum/shareit/item/model/Item.java diff --git a/src/main/java/ru/practicum/shareit/item/service/ItemService.java b/server/src/main/java/ru/practicum/shareit/item/service/ItemService.java similarity index 100% rename from src/main/java/ru/practicum/shareit/item/service/ItemService.java rename to server/src/main/java/ru/practicum/shareit/item/service/ItemService.java diff --git a/src/main/java/ru/practicum/shareit/item/service/ItemServiceImpl.java b/server/src/main/java/ru/practicum/shareit/item/service/ItemServiceImpl.java similarity index 96% rename from src/main/java/ru/practicum/shareit/item/service/ItemServiceImpl.java rename to server/src/main/java/ru/practicum/shareit/item/service/ItemServiceImpl.java index 309a966..8f67cf0 100644 --- a/src/main/java/ru/practicum/shareit/item/service/ItemServiceImpl.java +++ b/server/src/main/java/ru/practicum/shareit/item/service/ItemServiceImpl.java @@ -137,18 +137,20 @@ public Item getItemById(int itemId, int userId) { @Override public List getAllItemForOwner(int ownerId, Optional from, Optional size) { + List itemList; if (from.isPresent() && size.isPresent()) { if (from.isPresent() && from.get() >= 0 && size.isPresent() && size.get() > 0) { - return itemRepository.findByOwnerId(ownerId, + itemList = itemRepository.findByOwnerId(ownerId, PageRequest.of((int) Math.ceil((double) from.get() / size.get()), - size.get(), Sort.by("id").descending())).stream() - .map(e -> itemDtoMapper.itemDto(e)).collect(Collectors.toList()); + size.get(), Sort.by("id").ascending())).stream() + .collect(Collectors.toList()); } else { throw new UnavailableItemException("Не допустимое значение."); } + } else { + itemList = itemRepository.findByOwnerId(ownerId).stream() + .sorted(Comparator.comparingInt(Item::getId)).collect(Collectors.toList()); } - List itemList = itemRepository.findByOwnerId(ownerId).stream() - .sorted(Comparator.comparingInt(Item::getId)).collect(Collectors.toList()); List itemDtoList = new ArrayList<>(); for (Item item : itemList) { ItemDto itemDto = itemDtoMapper.itemDto(item); diff --git a/src/main/java/ru/practicum/shareit/request/Request.java b/server/src/main/java/ru/practicum/shareit/request/Request.java similarity index 100% rename from src/main/java/ru/practicum/shareit/request/Request.java rename to server/src/main/java/ru/practicum/shareit/request/Request.java diff --git a/src/main/java/ru/practicum/shareit/request/RequestController.java b/server/src/main/java/ru/practicum/shareit/request/RequestController.java similarity index 100% rename from src/main/java/ru/practicum/shareit/request/RequestController.java rename to server/src/main/java/ru/practicum/shareit/request/RequestController.java diff --git a/src/main/java/ru/practicum/shareit/request/RequestDtoMapper.java b/server/src/main/java/ru/practicum/shareit/request/RequestDtoMapper.java similarity index 100% rename from src/main/java/ru/practicum/shareit/request/RequestDtoMapper.java rename to server/src/main/java/ru/practicum/shareit/request/RequestDtoMapper.java diff --git a/src/main/java/ru/practicum/shareit/request/RequestMapper.java b/server/src/main/java/ru/practicum/shareit/request/RequestMapper.java similarity index 99% rename from src/main/java/ru/practicum/shareit/request/RequestMapper.java rename to server/src/main/java/ru/practicum/shareit/request/RequestMapper.java index 33343f2..7fbc15b 100644 --- a/src/main/java/ru/practicum/shareit/request/RequestMapper.java +++ b/server/src/main/java/ru/practicum/shareit/request/RequestMapper.java @@ -3,6 +3,7 @@ import org.springframework.stereotype.Component; import ru.practicum.shareit.request.dto.RequestDto; + @Component public class RequestMapper { diff --git a/src/main/java/ru/practicum/shareit/request/dao/RequestRepository.java b/server/src/main/java/ru/practicum/shareit/request/dao/RequestRepository.java similarity index 91% rename from src/main/java/ru/practicum/shareit/request/dao/RequestRepository.java rename to server/src/main/java/ru/practicum/shareit/request/dao/RequestRepository.java index c369928..7f41a14 100644 --- a/src/main/java/ru/practicum/shareit/request/dao/RequestRepository.java +++ b/server/src/main/java/ru/practicum/shareit/request/dao/RequestRepository.java @@ -12,6 +12,8 @@ @Repository public interface RequestRepository extends JpaRepository { + Page findByRequesterId(int requesterId, Pageable pageable); + List findByRequesterId(int requesterId); @Query(value = "SELECT r FROM Request AS r WHERE r.requesterId != :userId ORDER BY created DESC") diff --git a/src/main/java/ru/practicum/shareit/request/dto/RequestDto.java b/server/src/main/java/ru/practicum/shareit/request/dto/RequestDto.java similarity index 100% rename from src/main/java/ru/practicum/shareit/request/dto/RequestDto.java rename to server/src/main/java/ru/practicum/shareit/request/dto/RequestDto.java diff --git a/src/main/java/ru/practicum/shareit/request/service/RequestService.java b/server/src/main/java/ru/practicum/shareit/request/service/RequestService.java similarity index 99% rename from src/main/java/ru/practicum/shareit/request/service/RequestService.java rename to server/src/main/java/ru/practicum/shareit/request/service/RequestService.java index 1c739f6..373a0b3 100644 --- a/src/main/java/ru/practicum/shareit/request/service/RequestService.java +++ b/server/src/main/java/ru/practicum/shareit/request/service/RequestService.java @@ -1,5 +1,6 @@ package ru.practicum.shareit.request.service; + import ru.practicum.shareit.request.dto.RequestDto; import java.util.List; diff --git a/src/main/java/ru/practicum/shareit/request/service/RequestServiceImpl.java b/server/src/main/java/ru/practicum/shareit/request/service/RequestServiceImpl.java similarity index 100% rename from src/main/java/ru/practicum/shareit/request/service/RequestServiceImpl.java rename to server/src/main/java/ru/practicum/shareit/request/service/RequestServiceImpl.java diff --git a/src/main/java/ru/practicum/shareit/user/User.java b/server/src/main/java/ru/practicum/shareit/user/User.java similarity index 100% rename from src/main/java/ru/practicum/shareit/user/User.java rename to server/src/main/java/ru/practicum/shareit/user/User.java diff --git a/src/main/java/ru/practicum/shareit/user/UserController.java b/server/src/main/java/ru/practicum/shareit/user/UserController.java similarity index 100% rename from src/main/java/ru/practicum/shareit/user/UserController.java rename to server/src/main/java/ru/practicum/shareit/user/UserController.java diff --git a/src/main/java/ru/practicum/shareit/user/UserDtoMapper.java b/server/src/main/java/ru/practicum/shareit/user/UserDtoMapper.java similarity index 99% rename from src/main/java/ru/practicum/shareit/user/UserDtoMapper.java rename to server/src/main/java/ru/practicum/shareit/user/UserDtoMapper.java index 6547124..015da6c 100644 --- a/src/main/java/ru/practicum/shareit/user/UserDtoMapper.java +++ b/server/src/main/java/ru/practicum/shareit/user/UserDtoMapper.java @@ -3,6 +3,7 @@ import org.springframework.stereotype.Component; import ru.practicum.shareit.user.dto.UserDto; + @Component public class UserDtoMapper { public UserDto toUserDto(User user) { diff --git a/src/main/java/ru/practicum/shareit/user/UserMapper.java b/server/src/main/java/ru/practicum/shareit/user/UserMapper.java similarity index 99% rename from src/main/java/ru/practicum/shareit/user/UserMapper.java rename to server/src/main/java/ru/practicum/shareit/user/UserMapper.java index 4af22b7..5d71b65 100644 --- a/src/main/java/ru/practicum/shareit/user/UserMapper.java +++ b/server/src/main/java/ru/practicum/shareit/user/UserMapper.java @@ -3,6 +3,7 @@ import org.springframework.stereotype.Component; import ru.practicum.shareit.user.dto.UserDto; + @Component public class UserMapper { public User toUser(UserDto user) { diff --git a/src/main/java/ru/practicum/shareit/user/dao/UserRepository.java b/server/src/main/java/ru/practicum/shareit/user/dao/UserRepository.java similarity index 100% rename from src/main/java/ru/practicum/shareit/user/dao/UserRepository.java rename to server/src/main/java/ru/practicum/shareit/user/dao/UserRepository.java diff --git a/src/main/java/ru/practicum/shareit/user/dto/UserDto.java b/server/src/main/java/ru/practicum/shareit/user/dto/UserDto.java similarity index 100% rename from src/main/java/ru/practicum/shareit/user/dto/UserDto.java rename to server/src/main/java/ru/practicum/shareit/user/dto/UserDto.java diff --git a/src/main/java/ru/practicum/shareit/user/service/UserService.java b/server/src/main/java/ru/practicum/shareit/user/service/UserService.java similarity index 100% rename from src/main/java/ru/practicum/shareit/user/service/UserService.java rename to server/src/main/java/ru/practicum/shareit/user/service/UserService.java diff --git a/src/main/java/ru/practicum/shareit/user/service/UserServiceImpl.java b/server/src/main/java/ru/practicum/shareit/user/service/UserServiceImpl.java similarity index 100% rename from src/main/java/ru/practicum/shareit/user/service/UserServiceImpl.java rename to server/src/main/java/ru/practicum/shareit/user/service/UserServiceImpl.java diff --git a/src/main/resources/application.properties b/server/src/main/resources/application.properties similarity index 57% rename from src/main/resources/application.properties rename to server/src/main/resources/application.properties index d995053..ba794da 100644 --- a/src/main/resources/application.properties +++ b/server/src/main/resources/application.properties @@ -1,21 +1,19 @@ +server.port=9090 + spring.jpa.hibernate.ddl-auto=none spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL10Dialect spring.jpa.properties.hibernate.format_sql=true spring.sql.init.mode=always -logging.level.org.springframework.orm.jpa=INFO -logging.level.org.springframework.transaction=INFO -logging.level.org.springframework.transaction.interceptor=TRACE -logging.level.org.springframework.orm.jpa.JpaTransactionManager=DEBUG + #--- +spring.datasource.driverClassName=org.postgresql.Driver +spring.datasource.url=jdbc:postgresql://localhost:5432/shareit +spring.datasource.username=andersen +spring.datasource.password=andersen +#--- spring.config.activate.on-profile=ci,test spring.datasource.driverClassName=org.h2.Driver spring.datasource.url=jdbc:h2:mem:shareit spring.datasource.username=test -spring.datasource.password=test - - - - - - +spring.datasource.password=test \ No newline at end of file diff --git a/src/main/resources/booking.json b/server/src/main/resources/booking.json similarity index 100% rename from src/main/resources/booking.json rename to server/src/main/resources/booking.json diff --git a/src/main/resources/item.json b/server/src/main/resources/item.json similarity index 100% rename from src/main/resources/item.json rename to server/src/main/resources/item.json diff --git a/src/main/resources/resource.json b/server/src/main/resources/resource.json similarity index 100% rename from src/main/resources/resource.json rename to server/src/main/resources/resource.json diff --git a/src/main/resources/schema.sql b/server/src/main/resources/schema.sql similarity index 100% rename from src/main/resources/schema.sql rename to server/src/main/resources/schema.sql diff --git a/src/main/resources/user.json b/server/src/main/resources/user.json similarity index 100% rename from src/main/resources/user.json rename to server/src/main/resources/user.json diff --git a/src/test/java/ru/practicum/shareit/LocalDateTimeAdapter.java b/server/src/test/java/ru/practicum/shareit/LocalDateTimeAdapter.java similarity index 100% rename from src/test/java/ru/practicum/shareit/LocalDateTimeAdapter.java rename to server/src/test/java/ru/practicum/shareit/LocalDateTimeAdapter.java diff --git a/src/test/java/ru/practicum/shareit/ShareItTests.java b/server/src/test/java/ru/practicum/shareit/ShareItTests.java similarity index 100% rename from src/test/java/ru/practicum/shareit/ShareItTests.java rename to server/src/test/java/ru/practicum/shareit/ShareItTests.java diff --git a/src/test/java/ru/practicum/shareit/booking/BookingControllerTest.java b/server/src/test/java/ru/practicum/shareit/booking/BookingControllerTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/booking/BookingControllerTest.java rename to server/src/test/java/ru/practicum/shareit/booking/BookingControllerTest.java diff --git a/src/test/java/ru/practicum/shareit/booking/dao/BookingRepositoryTest.java b/server/src/test/java/ru/practicum/shareit/booking/dao/BookingRepositoryTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/booking/dao/BookingRepositoryTest.java rename to server/src/test/java/ru/practicum/shareit/booking/dao/BookingRepositoryTest.java diff --git a/src/test/java/ru/practicum/shareit/booking/dto/BookingDtoJsonTest.java b/server/src/test/java/ru/practicum/shareit/booking/dto/BookingDtoJsonTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/booking/dto/BookingDtoJsonTest.java rename to server/src/test/java/ru/practicum/shareit/booking/dto/BookingDtoJsonTest.java diff --git a/src/test/java/ru/practicum/shareit/booking/service/BookingServiceImpSpringBootlTest.java b/server/src/test/java/ru/practicum/shareit/booking/service/BookingServiceImpSpringBootlTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/booking/service/BookingServiceImpSpringBootlTest.java rename to server/src/test/java/ru/practicum/shareit/booking/service/BookingServiceImpSpringBootlTest.java diff --git a/src/test/java/ru/practicum/shareit/booking/service/BookingServiceImplTest.java b/server/src/test/java/ru/practicum/shareit/booking/service/BookingServiceImplTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/booking/service/BookingServiceImplTest.java rename to server/src/test/java/ru/practicum/shareit/booking/service/BookingServiceImplTest.java diff --git a/src/test/java/ru/practicum/shareit/exception/ErrorHandlerTest.java b/server/src/test/java/ru/practicum/shareit/exception/ErrorHandlerTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/exception/ErrorHandlerTest.java rename to server/src/test/java/ru/practicum/shareit/exception/ErrorHandlerTest.java diff --git a/src/test/java/ru/practicum/shareit/exception/ErrorResponseTest.java b/server/src/test/java/ru/practicum/shareit/exception/ErrorResponseTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/exception/ErrorResponseTest.java rename to server/src/test/java/ru/practicum/shareit/exception/ErrorResponseTest.java diff --git a/src/test/java/ru/practicum/shareit/item/ItemControllerTest.java b/server/src/test/java/ru/practicum/shareit/item/ItemControllerTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/item/ItemControllerTest.java rename to server/src/test/java/ru/practicum/shareit/item/ItemControllerTest.java diff --git a/src/test/java/ru/practicum/shareit/item/dao/ItemRepositoryTest.java b/server/src/test/java/ru/practicum/shareit/item/dao/ItemRepositoryTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/item/dao/ItemRepositoryTest.java rename to server/src/test/java/ru/practicum/shareit/item/dao/ItemRepositoryTest.java diff --git a/src/test/java/ru/practicum/shareit/item/dto/ItemDtoJsonTest.java b/server/src/test/java/ru/practicum/shareit/item/dto/ItemDtoJsonTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/item/dto/ItemDtoJsonTest.java rename to server/src/test/java/ru/practicum/shareit/item/dto/ItemDtoJsonTest.java diff --git a/src/test/java/ru/practicum/shareit/item/service/ItemServiceImplSpringBooTest.java b/server/src/test/java/ru/practicum/shareit/item/service/ItemServiceImplSpringBooTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/item/service/ItemServiceImplSpringBooTest.java rename to server/src/test/java/ru/practicum/shareit/item/service/ItemServiceImplSpringBooTest.java diff --git a/src/test/java/ru/practicum/shareit/item/service/ItemServiceImplTest.java b/server/src/test/java/ru/practicum/shareit/item/service/ItemServiceImplTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/item/service/ItemServiceImplTest.java rename to server/src/test/java/ru/practicum/shareit/item/service/ItemServiceImplTest.java diff --git a/src/test/java/ru/practicum/shareit/request/RequestControllerTest.java b/server/src/test/java/ru/practicum/shareit/request/RequestControllerTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/request/RequestControllerTest.java rename to server/src/test/java/ru/practicum/shareit/request/RequestControllerTest.java diff --git a/src/test/java/ru/practicum/shareit/request/dao/RequestRepositoryTest.java b/server/src/test/java/ru/practicum/shareit/request/dao/RequestRepositoryTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/request/dao/RequestRepositoryTest.java rename to server/src/test/java/ru/practicum/shareit/request/dao/RequestRepositoryTest.java diff --git a/src/test/java/ru/practicum/shareit/request/dto/RequestDtoJsonTest.java b/server/src/test/java/ru/practicum/shareit/request/dto/RequestDtoJsonTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/request/dto/RequestDtoJsonTest.java rename to server/src/test/java/ru/practicum/shareit/request/dto/RequestDtoJsonTest.java diff --git a/src/test/java/ru/practicum/shareit/request/service/RequestServiceImplSpringBootTest.java b/server/src/test/java/ru/practicum/shareit/request/service/RequestServiceImplSpringBootTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/request/service/RequestServiceImplSpringBootTest.java rename to server/src/test/java/ru/practicum/shareit/request/service/RequestServiceImplSpringBootTest.java diff --git a/src/test/java/ru/practicum/shareit/request/service/RequestServiceImplTest.java b/server/src/test/java/ru/practicum/shareit/request/service/RequestServiceImplTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/request/service/RequestServiceImplTest.java rename to server/src/test/java/ru/practicum/shareit/request/service/RequestServiceImplTest.java diff --git a/src/test/java/ru/practicum/shareit/user/UserControllerTest.java b/server/src/test/java/ru/practicum/shareit/user/UserControllerTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/user/UserControllerTest.java rename to server/src/test/java/ru/practicum/shareit/user/UserControllerTest.java diff --git a/src/test/java/ru/practicum/shareit/user/dto/UserDtoJsonTest.java b/server/src/test/java/ru/practicum/shareit/user/dto/UserDtoJsonTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/user/dto/UserDtoJsonTest.java rename to server/src/test/java/ru/practicum/shareit/user/dto/UserDtoJsonTest.java diff --git a/src/test/java/ru/practicum/shareit/user/service/UserServiceImplSpringBootTest.java b/server/src/test/java/ru/practicum/shareit/user/service/UserServiceImplSpringBootTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/user/service/UserServiceImplSpringBootTest.java rename to server/src/test/java/ru/practicum/shareit/user/service/UserServiceImplSpringBootTest.java diff --git a/src/test/java/ru/practicum/shareit/user/service/UserServiceImplTest.java b/server/src/test/java/ru/practicum/shareit/user/service/UserServiceImplTest.java similarity index 100% rename from src/test/java/ru/practicum/shareit/user/service/UserServiceImplTest.java rename to server/src/test/java/ru/practicum/shareit/user/service/UserServiceImplTest.java From 80830b36e487df448332ed1c446ff9c4975cfef6 Mon Sep 17 00:00:00 2001 From: Andersen Date: Fri, 8 Dec 2023 19:58:50 +0300 Subject: [PATCH 2/2] checkStyke --- docker-compose.yml | 6 +++--- .../java/ru/practicum/shareit/booking/dto/BookingDto.java | 2 +- .../main/java/ru/practicum/shareit/item/ItemController.java | 3 +-- .../main/java/ru/practicum/shareit/item/dto/CommentDto.java | 2 +- .../main/java/ru/practicum/shareit/item/dto/ItemDto.java | 6 ------ .../java/ru/practicum/shareit/item/dto/ItemRequestDto.java | 4 +--- .../ru/practicum/shareit/request/RequestController.java | 3 +-- .../java/ru/practicum/shareit/request/dto/RequestDto.java | 4 ---- .../main/java/ru/practicum/shareit/user/UserController.java | 3 +-- .../main/java/ru/practicum/shareit/user/dto/UserDto.java | 6 ------ 10 files changed, 9 insertions(+), 30 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index a914772..fbdf478 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,11 +16,11 @@ services: ports: - "9090:9090" depends_on: - - db + - postgres environment: - - SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/shareit + - SPRING_DATASOURCE_URL=jdbc:postgresql://postgres:5432/shareit - db: + postgres: image: postgres:13.7-alpine container_name: data_base ports: diff --git a/server/src/main/java/ru/practicum/shareit/booking/dto/BookingDto.java b/server/src/main/java/ru/practicum/shareit/booking/dto/BookingDto.java index 5d9db44..086b42f 100644 --- a/server/src/main/java/ru/practicum/shareit/booking/dto/BookingDto.java +++ b/server/src/main/java/ru/practicum/shareit/booking/dto/BookingDto.java @@ -23,5 +23,5 @@ public class BookingDto { private BookingStatus status; private ItemDto item; private UserDto booker; - int bookerId; + private int bookerId; } diff --git a/server/src/main/java/ru/practicum/shareit/item/ItemController.java b/server/src/main/java/ru/practicum/shareit/item/ItemController.java index 50c1336..0a84423 100644 --- a/server/src/main/java/ru/practicum/shareit/item/ItemController.java +++ b/server/src/main/java/ru/practicum/shareit/item/ItemController.java @@ -6,7 +6,6 @@ import ru.practicum.shareit.item.dto.ItemDto; import ru.practicum.shareit.item.service.ItemService; -import javax.validation.Valid; import java.util.List; import java.util.Map; import java.util.Optional; @@ -19,7 +18,7 @@ public class ItemController { @PostMapping public ItemDto addItem(@RequestHeader("X-Sharer-User-Id") int userId, - @Valid @RequestBody ItemDto item) { + @RequestBody ItemDto item) { return itemService.addItem(userId, item); } diff --git a/server/src/main/java/ru/practicum/shareit/item/dto/CommentDto.java b/server/src/main/java/ru/practicum/shareit/item/dto/CommentDto.java index 7f08fe2..4c1ea14 100644 --- a/server/src/main/java/ru/practicum/shareit/item/dto/CommentDto.java +++ b/server/src/main/java/ru/practicum/shareit/item/dto/CommentDto.java @@ -24,5 +24,5 @@ public class CommentDto { private LocalDateTime created; - String authorName; + private String authorName; } diff --git a/server/src/main/java/ru/practicum/shareit/item/dto/ItemDto.java b/server/src/main/java/ru/practicum/shareit/item/dto/ItemDto.java index 5e1e427..cf3e20d 100644 --- a/server/src/main/java/ru/practicum/shareit/item/dto/ItemDto.java +++ b/server/src/main/java/ru/practicum/shareit/item/dto/ItemDto.java @@ -6,8 +6,6 @@ import lombok.experimental.Accessors; import ru.practicum.shareit.booking.dto.BookingDto; -import javax.validation.constraints.NotBlank; -import javax.validation.constraints.NotNull; import java.util.ArrayList; import java.util.List; @@ -18,12 +16,8 @@ public class ItemDto { private int id; - @NotBlank(message = "Поле \'name\' не может быть пустым.") - @NotNull(message = "Поле \'name\' не может быть пустым.") private String name; - @NotBlank(message = "Поле \'description\' не может быть пустым.") private String description; - @NotNull(message = "Поле \'available\' не может быть пустым.") private Boolean available; private int ownerId; private Integer requestId; diff --git a/server/src/main/java/ru/practicum/shareit/item/dto/ItemRequestDto.java b/server/src/main/java/ru/practicum/shareit/item/dto/ItemRequestDto.java index 5819dfe..b12412c 100644 --- a/server/src/main/java/ru/practicum/shareit/item/dto/ItemRequestDto.java +++ b/server/src/main/java/ru/practicum/shareit/item/dto/ItemRequestDto.java @@ -8,12 +8,10 @@ @Setter @Accessors(chain = true) public class ItemRequestDto { - private int id; + private int id; private String name; - private String description; - private Boolean available; private Integer requestId; diff --git a/server/src/main/java/ru/practicum/shareit/request/RequestController.java b/server/src/main/java/ru/practicum/shareit/request/RequestController.java index abbcb62..47ea559 100644 --- a/server/src/main/java/ru/practicum/shareit/request/RequestController.java +++ b/server/src/main/java/ru/practicum/shareit/request/RequestController.java @@ -5,7 +5,6 @@ import ru.practicum.shareit.request.dto.RequestDto; import ru.practicum.shareit.request.service.RequestService; -import javax.validation.Valid; import java.util.List; import java.util.Optional; @@ -16,7 +15,7 @@ public class RequestController { RequestService requestService; @PostMapping() - public RequestDto postRequest(@Valid @RequestBody RequestDto request, @RequestHeader("X-Sharer-User-Id") int userId) { + public RequestDto postRequest(@RequestBody RequestDto request, @RequestHeader("X-Sharer-User-Id") int userId) { return requestService.addRequest(request, userId); } diff --git a/server/src/main/java/ru/practicum/shareit/request/dto/RequestDto.java b/server/src/main/java/ru/practicum/shareit/request/dto/RequestDto.java index c47c4bf..716dbfe 100644 --- a/server/src/main/java/ru/practicum/shareit/request/dto/RequestDto.java +++ b/server/src/main/java/ru/practicum/shareit/request/dto/RequestDto.java @@ -7,8 +7,6 @@ import lombok.experimental.Accessors; import ru.practicum.shareit.item.dto.ItemRequestDto; -import javax.validation.constraints.NotBlank; -import javax.validation.constraints.NotNull; import java.time.LocalDateTime; import java.util.List; @@ -20,8 +18,6 @@ public class RequestDto { private int id; - @NotBlank(message = "Поле не может быть пустым!") - @NotNull(message = "Поле не может быть пустым!") private String description; private LocalDateTime created; private List items; diff --git a/server/src/main/java/ru/practicum/shareit/user/UserController.java b/server/src/main/java/ru/practicum/shareit/user/UserController.java index 9c5e3b0..2cda56a 100644 --- a/server/src/main/java/ru/practicum/shareit/user/UserController.java +++ b/server/src/main/java/ru/practicum/shareit/user/UserController.java @@ -5,7 +5,6 @@ import ru.practicum.shareit.user.dto.UserDto; import ru.practicum.shareit.user.service.UserService; -import javax.validation.Valid; import java.util.List; import java.util.Map; @@ -19,7 +18,7 @@ public class UserController { @PostMapping - public UserDto addUser(@Valid @RequestBody UserDto user) { + public UserDto addUser(@RequestBody UserDto user) { return userService.addUser(user); } diff --git a/server/src/main/java/ru/practicum/shareit/user/dto/UserDto.java b/server/src/main/java/ru/practicum/shareit/user/dto/UserDto.java index 43ef18c..dbfb06a 100644 --- a/server/src/main/java/ru/practicum/shareit/user/dto/UserDto.java +++ b/server/src/main/java/ru/practicum/shareit/user/dto/UserDto.java @@ -6,9 +6,6 @@ import lombok.experimental.Accessors; import org.springframework.stereotype.Component; -import javax.validation.constraints.Email; -import javax.validation.constraints.NotBlank; - @Data @Accessors(chain = true) @EqualsAndHashCode @@ -17,9 +14,6 @@ public class UserDto { private int id; - @NotBlank(message = "Name - не может быть пустым.") private String name; - @NotBlank(message = "Email - не может быть пустым.") - @Email(message = "Не правильный формат email") private String email; }