From a1985bec145e596cd7c0c7f40397f9947adb0999 Mon Sep 17 00:00:00 2001 From: yiqes <155768368+yiqes@users.noreply.github.com> Date: Thu, 16 Jan 2025 14:34:31 +0300 Subject: [PATCH 1/2] feat: added documentation by using javadoc + information in README.md --- README.md | 203 +++++++++++++++++- .../shareit/booking/BookingClient.java | 49 +++++ .../shareit/booking/BookingController.java | 43 ++++ .../booking/dto/BookItemRequestDto.java | 3 + .../shareit/booking/dto/BookingShortDto.java | 3 + .../shareit/booking/dto/BookingState.java | 9 + .../practicum/shareit/client/BaseClient.java | 134 ++++++++++++ .../shareit/handler/ErrorHandler.java | 9 + .../shareit/handler/ErrorResponse.java | 13 ++ .../ru/practicum/shareit/item/ItemClient.java | 62 ++++++ .../shareit/item/ItemController.java | 56 +++++ .../shareit/item/dto/CommentDto.java | 3 + .../practicum/shareit/item/dto/ItemDto.java | 3 + .../shareit/request/RequestClient.java | 37 ++++ .../shareit/request/RequestController.java | 31 +++ .../shareit/request/dto/RequestDto.java | 3 + .../ru/practicum/shareit/user/UserClient.java | 39 ++++ .../shareit/user/UserController.java | 33 +++ .../practicum/shareit/user/dto/UserDto.java | 3 + .../ru/practicum/shareit/booking/Booking.java | 3 + .../shareit/booking/BookingController.java | 44 ++++ .../shareit/booking/BookingMapper.java | 30 +++ .../shareit/booking/BookingRepository.java | 106 +++++++++ .../shareit/booking/BookingService.java | 58 +++++ .../exception/BookingNotFoundException.java | 8 + .../exception/ItemNotFoundException.java | 8 + .../exception/RequestNotFoundException.java | 8 + .../exception/UserAlreadyExistsException.java | 8 + .../exception/UserNotFoundException.java | 8 + .../exception/ValidationException.java | 8 + .../shareit/handler/ErrorHandler.java | 39 ++++ .../shareit/handler/ErrorResponse.java | 13 ++ .../ru/practicum/shareit/item/Comment.java | 3 + .../ru/practicum/shareit/item/CommentDto.java | 3 + .../shareit/item/CommentRepository.java | 10 + .../java/ru/practicum/shareit/item/Item.java | 3 + .../shareit/item/ItemController.java | 56 +++++ .../ru/practicum/shareit/item/ItemDto.java | 3 + .../ru/practicum/shareit/item/ItemMapper.java | 33 +++ .../shareit/item/ItemRepository.java | 22 ++ .../practicum/shareit/item/ItemService.java | 69 ++++++ .../shareit/item/ItemServiceImpl.java | 11 + .../ru/practicum/shareit/request/Request.java | 3 + .../shareit/request/RequestController.java | 36 ++++ .../practicum/shareit/request/RequestDto.java | 3 + .../shareit/request/RequestMapper.java | 24 +++ .../shareit/request/RequestRepository.java | 25 ++- .../shareit/request/RequestService.java | 32 +++ .../shareit/request/RequestServiceImpl.java | 10 + .../shareit/service/ValidationService.java | 60 ++++++ .../java/ru/practicum/shareit/user/User.java | 3 + .../shareit/user/UserController.java | 33 +++ .../ru/practicum/shareit/user/UserDto.java | 3 + .../ru/practicum/shareit/user/UserMapper.java | 15 ++ .../shareit/user/UserRepository.java | 3 + .../practicum/shareit/user/UserService.java | 44 ++++ .../shareit/user/UserServiceImpl.java | 9 + .../ru/practicum/shareit/util/Pagination.java | 9 + 58 files changed, 1599 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 47a75f0..0fd8044 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,201 @@ -# java-shareit -Template repository for Shareit project. +# ShareIt + +**ShareIt** — это приложение для обмена вещами между пользователями. Оно позволяет пользователям создавать запросы на аренду вещей, бронировать их, оставлять отзывы и управлять своими вещами. + +--- + +## Основные функции + +- **Управление пользователями**: создание, обновление, удаление и получение информации о пользователях. +- **Управление вещами**: добавление, обновление, удаление и поиск вещей. +- **Бронирование вещей**: создание, подтверждение и отмена бронирования. +- **Запросы на аренду**: создание и просмотр запросов на аренду вещей. +- **Отзывы**: возможность оставлять отзывы на арендованные вещи. + +--- + +## Технологии + +- **Java 21** +- **Spring Boot** +- **RestTemplate** для взаимодействия с основным сервисом +- **Maven** для сборки проекта +- **Логирование** с использованием Slf4j + +--- + +## API + +### 1. **Пользователи (`/users`)** +- **Создание пользователя**: + `POST /users` + Пример тела запроса: + ```json + { + "name": "John Doe", + "email": "john.doe@example.com" + } + ``` + +- **Получение пользователя по ID**: + `GET /users/{user-id}` + +- **Получение всех пользователей**: + `GET /users` + +- **Обновление пользователя**: + `PATCH /users/{user-id}` + Пример тела запроса: + ```json + { + "name": "Jane Doe" + } + ``` + +- **Удаление пользователя**: + `DELETE /users/{user-id}` + +--- + +### 2. **Вещи (`/items`)** +- **Создание вещи**: + `POST /items` + Пример тела запроса: + ```json + { + "name": "Дрель", + "description": "Мощная дрель для ремонта", + "available": true + } + ``` + +- **Получение вещи по ID**: + `GET /items/{item-id}` + +- **Получение всех вещей пользователя**: + `GET /items` + +- **Обновление вещи**: + `PATCH /items/{item-id}` + Пример тела запроса: + ```json + { + "description": "Мощная дрель с аккумулятором" + } + ``` + +- **Удаление вещи**: + `DELETE /items/{item-id}` + +- **Поиск вещей**: + `GET /items/search?text={query}` + +- **Добавление отзыва**: + `POST /items/{item-id}/comment` + Пример тела запроса: + ```json + { + "text": "Отличная дрель, все работает!" + } + ``` + +--- + +### 3. **Бронирование (`/bookings`)** +- **Создание бронирования**: + `POST /bookings` + Пример тела запроса: + ```json + { + "itemId": 1, + "start": "2023-10-10T10:00:00", + "end": "2023-10-15T10:00:00" + } + ``` + +- **Подтверждение бронирования**: + `PATCH /bookings/{booking-id}?approved=true` + +- **Получение бронирования по ID**: + `GET /bookings/{booking-id}` + +- **Получение всех бронирований пользователя**: + `GET /bookings?state={state}` + +- **Получение бронирований владельца**: + `GET /bookings/owner?state={state}` + +--- + +### 4. **Запросы на аренду (`/requests`)** +- **Создание запроса**: + `POST /requests` + Пример тела запроса: + ```json + { + "description": "Нужна дрель для ремонта" + } + ``` + +- **Получение запроса по ID**: + `GET /requests/{request-id}` + +- **Получение всех запросов**: + `GET /requests/all` + +- **Получение собственных запросов**: + `GET /requests` + +--- + +## Запуск приложения + +1. Убедитесь, что у вас установлены: + - Java 21 + - Maven + +2. Клонируйте репозиторий: + ```bash + git clone https://github.com/yiqes/java-shareit + cd java-shareit + ``` + +3. Соберите проект: + ```bash + mvn clean install + ``` + +4. Запустите приложение: + ```bash + mvn spring-boot:run + ``` + +5. Приложение будет доступно по адресу: + `http://localhost:8080` + +--- + +## Примеры использования + +### Создание пользователя +```bash +curl -X POST -H "Content-Type: application/json" -d '{"name": "John Doe", "email": "john.doe@example.com"}' http://localhost:8080/users +``` + +### Создание вещи +```bash +curl -X POST -H "Content-Type: application/json" -H "X-Sharer-User-Id: 1" -d '{"name": "Дрель", "description": "Мощная дрель", "available": true}' http://localhost:8080/items +``` + +### Создание бронирования +```bash +curl -X POST -H "Content-Type: application/json" -H "X-Sharer-User-Id: 2" -d '{"itemId": 1, "start": "2023-10-10T10:00:00", "end": "2023-10-15T10:00:00"}' http://localhost:8080/bookings +``` + +--- + +## Логирование + +Логирование настроено с использованием Slf4j. Логи можно найти в консоли или в файле `logs/app.log`. + +--- \ 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 index 122266f..c6c77a5 100644 --- a/gateway/src/main/java/ru/practicum/shareit/booking/BookingClient.java +++ b/gateway/src/main/java/ru/practicum/shareit/booking/BookingClient.java @@ -12,10 +12,19 @@ import ru.practicum.shareit.booking.dto.BookingState; import ru.practicum.shareit.client.BaseClient; +/** + * The type Booking client. + */ @Service public class BookingClient extends BaseClient { private static final String API_PREFIX = "/bookings"; + /** + * Instantiates a new Booking client. + * + * @param serverUrl the server url + * @param builder the builder + */ @Autowired public BookingClient(@Value("${shareit-server.url}") String serverUrl, RestTemplateBuilder builder) { super( @@ -26,6 +35,15 @@ public BookingClient(@Value("${shareit-server.url}") String serverUrl, RestTempl ); } + /** + * Gets bookings. + * + * @param userId the user id + * @param state the state + * @param from the from + * @param size the size + * @return the bookings + */ public ResponseEntity getBookings(Long userId, BookingState state, Integer from, Integer size) { String path = "?state=" + state.name() + "&from=" + from; if (size != null) { @@ -34,6 +52,15 @@ public ResponseEntity getBookings(Long userId, BookingState state, Integ return get(path, userId, null); } + /** + * Gets bookings owner. + * + * @param userId the user id + * @param state the state + * @param from the form + * @param size the size + * @return the bookings owner + */ public ResponseEntity getBookingsOwner(Long userId, BookingState state, Integer from, Integer size) { String path = "/owner?state=" + state.name() + "&from=" + from; if (size != null) { @@ -43,14 +70,36 @@ public ResponseEntity getBookingsOwner(Long userId, BookingState state, } + /** + * Create response entity. + * + * @param userId the user id + * @param requestDto the request dto + * @return the response entity + */ public ResponseEntity create(Long userId, BookItemRequestDto requestDto) { return post("", userId, requestDto); } + /** + * Gets booking. + * + * @param userId the user id + * @param bookingId the booking id + * @return the booking + */ public ResponseEntity getBooking(Long userId, Long bookingId) { return get("/" + bookingId, userId); } + /** + * Update response entity. + * + * @param bookingId the booking id + * @param userId the user id + * @param approved the approved + * @return the response entity + */ public ResponseEntity update(Long bookingId, Long userId, Boolean approved) { String path = "/" + bookingId + "?approved=" + approved; return patch(path, userId, null, null); diff --git a/gateway/src/main/java/ru/practicum/shareit/booking/BookingController.java b/gateway/src/main/java/ru/practicum/shareit/booking/BookingController.java index 67df62b..87a9fb5 100644 --- a/gateway/src/main/java/ru/practicum/shareit/booking/BookingController.java +++ b/gateway/src/main/java/ru/practicum/shareit/booking/BookingController.java @@ -12,6 +12,9 @@ import ru.practicum.shareit.booking.dto.BookItemRequestDto; import ru.practicum.shareit.booking.dto.BookingState; +/** + * The type Booking controller. + */ @Controller @RequestMapping(path = "/bookings") @RequiredArgsConstructor @@ -22,6 +25,15 @@ public class BookingController { private final BookingClient bookingClient; private static final String BOOKING_ID = "{booking-id}"; + /** + * Gets bookings. + * + * @param userId the user id + * @param stateParam the state param + * @param from the from + * @param size the size + * @return the bookings + */ @GetMapping public ResponseEntity getBookings(@RequestHeader(USER_ID) Long userId, @RequestParam(name = "state", defaultValue = "all") String stateParam, @@ -34,6 +46,15 @@ public ResponseEntity getBookings(@RequestHeader(USER_ID) Long userId, return bookingClient.getBookings(userId, state, from, size); } + /** + * Gets bookings owner. + * + * @param stateParam the state param + * @param userId the user id + * @param from the from + * @param size the size + * @return the bookings owner + */ @GetMapping("/owner") public ResponseEntity getBookingsOwner(@RequestParam(name = "state", defaultValue = "all") String stateParam, @@ -47,6 +68,13 @@ public ResponseEntity getBookingsOwner(@RequestParam(name = "state", def return bookingClient.getBookingsOwner(userId, state, from, size); } + /** + * Create response entity. + * + * @param userId the user id + * @param requestDto the request dto + * @return the response entity + */ @PostMapping public ResponseEntity create(@RequestHeader(USER_ID) Long userId, @RequestBody @Valid BookItemRequestDto requestDto) { @@ -54,6 +82,13 @@ public ResponseEntity create(@RequestHeader(USER_ID) Long userId, return bookingClient.create(userId, requestDto); } + /** + * Gets booking. + * + * @param userId the user id + * @param bookingId the booking id + * @return the booking + */ @GetMapping(BOOKING_ID) public ResponseEntity getBooking(@RequestHeader(USER_ID) Long userId, @PathVariable("booking-id") Long bookingId) { @@ -61,6 +96,14 @@ public ResponseEntity getBooking(@RequestHeader(USER_ID) Long userId, return bookingClient.getBooking(userId, bookingId); } + /** + * Update response entity. + * + * @param bookingId the booking id + * @param userId the user id + * @param approved the approved + * @return the response entity + */ @ResponseBody @PatchMapping(BOOKING_ID) public ResponseEntity update(@PathVariable("booking-id") Long bookingId, 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 index 6f8e11d..26f2e9b 100644 --- a/gateway/src/main/java/ru/practicum/shareit/booking/dto/BookItemRequestDto.java +++ b/gateway/src/main/java/ru/practicum/shareit/booking/dto/BookItemRequestDto.java @@ -10,6 +10,9 @@ import lombok.NoArgsConstructor; import lombok.experimental.FieldDefaults; +/** + * The type Book item request dto. + */ @Getter @NoArgsConstructor @AllArgsConstructor diff --git a/gateway/src/main/java/ru/practicum/shareit/booking/dto/BookingShortDto.java b/gateway/src/main/java/ru/practicum/shareit/booking/dto/BookingShortDto.java index 89adfd4..29f319a 100644 --- a/gateway/src/main/java/ru/practicum/shareit/booking/dto/BookingShortDto.java +++ b/gateway/src/main/java/ru/practicum/shareit/booking/dto/BookingShortDto.java @@ -7,6 +7,9 @@ import java.time.LocalDateTime; +/** + * The type Booking short dto. + */ @Data @AllArgsConstructor @FieldDefaults(level = AccessLevel.PRIVATE) 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 index 943af85..62a7aee 100644 --- a/gateway/src/main/java/ru/practicum/shareit/booking/dto/BookingState.java +++ b/gateway/src/main/java/ru/practicum/shareit/booking/dto/BookingState.java @@ -2,6 +2,9 @@ import java.util.Optional; +/** + * The enum Booking state. + */ public enum BookingState { // Все ALL, @@ -16,6 +19,12 @@ public enum BookingState { // Ожидающие подтверждения WAITING; + /** + * From optional. + * + * @param stringState the string state + * @return the optional + */ public static Optional from(String stringState) { for (BookingState state : values()) { if (state.name().equalsIgnoreCase(stringState)) { diff --git a/gateway/src/main/java/ru/practicum/shareit/client/BaseClient.java b/gateway/src/main/java/ru/practicum/shareit/client/BaseClient.java index f9781be..ee3c9a0 100644 --- a/gateway/src/main/java/ru/practicum/shareit/client/BaseClient.java +++ b/gateway/src/main/java/ru/practicum/shareit/client/BaseClient.java @@ -12,69 +12,203 @@ import org.springframework.web.client.HttpStatusCodeException; import org.springframework.web.client.RestTemplate; +/** + * The type Base client. + */ public class BaseClient { + /** + * The Rest. + */ protected final RestTemplate rest; + /** + * Instantiates a new Base client. + * + * @param rest the rest + */ public BaseClient(RestTemplate rest) { this.rest = rest; } + /** + * Get response entity. + * + * @param path the path + * @return the response entity + */ protected ResponseEntity get(String path) { return get(path, null, null); } + /** + * Get response entity. + * + * @param path the path + * @param userId the user id + * @return the response entity + */ protected ResponseEntity get(String path, long userId) { return get(path, userId, null); } + /** + * Get response entity. + * + * @param path the path + * @param userId the user id + * @param parameters the parameters + * @return the response entity + */ protected ResponseEntity get(String path, Long userId, @Nullable Map parameters) { return makeAndSendRequest(HttpMethod.GET, path, userId, parameters, null); } + /** + * Post response entity. + * + * @param the type parameter + * @param path the path + * @param body the body + * @return the response entity + */ protected ResponseEntity post(String path, T body) { return post(path, null, null, body); } + /** + * Post response entity. + * + * @param the type parameter + * @param path the path + * @param userId the user id + * @param body the body + * @return the response entity + */ protected ResponseEntity post(String path, long userId, T body) { return post(path, userId, null, body); } + /** + * Post response entity. + * + * @param the type parameter + * @param path the path + * @param userId the user id + * @param parameters the parameters + * @param body the body + * @return the response entity + */ protected ResponseEntity post(String path, Long userId, @Nullable Map parameters, T body) { return makeAndSendRequest(HttpMethod.POST, path, userId, parameters, body); } + /** + * Put response entity. + * + * @param the type parameter + * @param path the path + * @param userId the user id + * @param body the body + * @return the response entity + */ protected ResponseEntity put(String path, long userId, T body) { return put(path, userId, null, body); } + /** + * Put response entity. + * + * @param the type parameter + * @param path the path + * @param userId the user id + * @param parameters the parameters + * @param body the body + * @return the response entity + */ protected ResponseEntity put(String path, long userId, @Nullable Map parameters, T body) { return makeAndSendRequest(HttpMethod.PUT, path, userId, parameters, body); } + /** + * Patch response entity. + * + * @param the type parameter + * @param path the path + * @param body the body + * @return the response entity + */ protected ResponseEntity patch(String path, T body) { return patch(path, null, null, body); } + /** + * Patch response entity. + * + * @param the type parameter + * @param path the path + * @param userId the user id + * @return the response entity + */ protected ResponseEntity patch(String path, long userId) { return patch(path, userId, null, null); } + /** + * Patch response entity. + * + * @param the type parameter + * @param path the path + * @param userId the user id + * @param body the body + * @return the response entity + */ protected ResponseEntity patch(String path, long userId, T body) { return patch(path, userId, null, body); } + /** + * Patch response entity. + * + * @param the type parameter + * @param path the path + * @param userId the user id + * @param parameters the parameters + * @param body the body + * @return the response entity + */ protected ResponseEntity patch(String path, Long userId, @Nullable Map parameters, T body) { return makeAndSendRequest(HttpMethod.PATCH, path, userId, parameters, body); } + /** + * Delete response entity. + * + * @param path the path + * @return the response entity + */ protected ResponseEntity delete(String path) { return delete(path, null, null); } + /** + * Delete response entity. + * + * @param path the path + * @param userId the user id + * @return the response entity + */ protected ResponseEntity delete(String path, long userId) { return delete(path, userId, null); } + /** + * Delete response entity. + * + * @param path the path + * @param userId the user id + * @param parameters the parameters + * @return the response entity + */ protected ResponseEntity delete(String path, Long userId, @Nullable Map parameters) { return makeAndSendRequest(HttpMethod.DELETE, path, userId, parameters, null); } diff --git a/gateway/src/main/java/ru/practicum/shareit/handler/ErrorHandler.java b/gateway/src/main/java/ru/practicum/shareit/handler/ErrorHandler.java index 6439bd7..9bb726b 100644 --- a/gateway/src/main/java/ru/practicum/shareit/handler/ErrorHandler.java +++ b/gateway/src/main/java/ru/practicum/shareit/handler/ErrorHandler.java @@ -5,9 +5,18 @@ import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; +/** + * The type Error handler. + */ @RestControllerAdvice public class ErrorHandler { + /** + * Handle illegal argument exception error response. + * + * @param e the e + * @return the error response + */ @ExceptionHandler @ResponseStatus(HttpStatus.BAD_REQUEST) public ErrorResponse handleIllegalArgumentException(final IllegalArgumentException e) { diff --git a/gateway/src/main/java/ru/practicum/shareit/handler/ErrorResponse.java b/gateway/src/main/java/ru/practicum/shareit/handler/ErrorResponse.java index 6cca749..479e68a 100644 --- a/gateway/src/main/java/ru/practicum/shareit/handler/ErrorResponse.java +++ b/gateway/src/main/java/ru/practicum/shareit/handler/ErrorResponse.java @@ -1,12 +1,25 @@ package ru.practicum.shareit.handler; +/** + * The type Error response. + */ public class ErrorResponse { private final String error; + /** + * Instantiates a new Error response. + * + * @param error the error + */ public ErrorResponse(String error) { this.error = error; } + /** + * Gets error. + * + * @return the error + */ public String getError() { return error; } diff --git a/gateway/src/main/java/ru/practicum/shareit/item/ItemClient.java b/gateway/src/main/java/ru/practicum/shareit/item/ItemClient.java index 5cf7e64..6b4a2a4 100644 --- a/gateway/src/main/java/ru/practicum/shareit/item/ItemClient.java +++ b/gateway/src/main/java/ru/practicum/shareit/item/ItemClient.java @@ -11,10 +11,19 @@ import ru.practicum.shareit.item.dto.CommentDto; import ru.practicum.shareit.item.dto.ItemDto; +/** + * The type Item client. + */ @Service public class ItemClient extends BaseClient { private static final String API_PREFIX = "/items"; + /** + * Instantiates a new Item client. + * + * @param serverUrl the server url + * @param builder the builder + */ @Autowired public ItemClient(@Value("${shareit-server.url}") String serverUrl, RestTemplateBuilder builder) { super( @@ -25,14 +34,36 @@ public ItemClient(@Value("${shareit-server.url}") String serverUrl, RestTemplate ); } + /** + * Create response entity. + * + * @param userId the user id + * @param itemDto the item dto + * @return the response entity + */ public ResponseEntity create(Long userId, ItemDto itemDto) { return post("", userId, itemDto); } + /** + * Gets item by id. + * + * @param userId the user id + * @param itemId the item id + * @return the item by id + */ public ResponseEntity getItemById(Long userId, Long itemId) { return get("/" + itemId, userId); } + /** + * Gets items by owner. + * + * @param userId the user id + * @param from the from + * @param size the size + * @return the items by owner + */ public ResponseEntity getItemsByOwner(Long userId, Integer from, Integer size) { String path = "?from=" + from; if (size != null) { @@ -41,14 +72,37 @@ public ResponseEntity getItemsByOwner(Long userId, Integer from, Integer return get(path, userId); } + /** + * Update response entity. + * + * @param itemDto the item dto + * @param itemId the item id + * @param userId the user id + * @return the response entity + */ public ResponseEntity update(ItemDto itemDto, Long itemId, Long userId) { return patch("/" + itemId, userId, itemDto); } + /** + * Delete response entity. + * + * @param itemId the item id + * @param userId the user id + * @return the response entity + */ public ResponseEntity delete(Long itemId, Long userId) { return delete("/" + itemId, userId); } + /** + * Gets items by search query. + * + * @param text the text + * @param from the from + * @param size the size + * @return the items by search query + */ public ResponseEntity getItemsBySearchQuery(String text, Integer from, Integer size) { String path = "/search?text=" + text + "&from=" + from; if (size != null) { @@ -57,6 +111,14 @@ public ResponseEntity getItemsBySearchQuery(String text, Integer from, I return get(path); } + /** + * Create comment response entity. + * + * @param commentDto the comment dto + * @param itemId the item id + * @param userId the user id + * @return the response entity + */ public ResponseEntity createComment(CommentDto commentDto, Long itemId, Long userId) { return post("/" + itemId + "/comment", userId, commentDto); } diff --git a/gateway/src/main/java/ru/practicum/shareit/item/ItemController.java b/gateway/src/main/java/ru/practicum/shareit/item/ItemController.java index 2b3d378..6b10e6d 100644 --- a/gateway/src/main/java/ru/practicum/shareit/item/ItemController.java +++ b/gateway/src/main/java/ru/practicum/shareit/item/ItemController.java @@ -12,6 +12,9 @@ import ru.practicum.shareit.item.dto.ItemDto; +/** + * The type Item controller. + */ @Controller @RequestMapping(path = "/items") @RequiredArgsConstructor @@ -23,6 +26,14 @@ public class ItemController { private static final String ITEM_ID = "{item-id}"; + /** + * Gets items by owner. + * + * @param ownerId the owner id + * @param from the from + * @param size the size + * @return the items by owner + */ @GetMapping public ResponseEntity getItemsByOwner(@RequestHeader(USER_ID) Long ownerId, @PositiveOrZero @RequestParam(defaultValue = "0") Integer from, @@ -31,6 +42,13 @@ public ResponseEntity getItemsByOwner(@RequestHeader(USER_ID) Long owner return itemClient.getItemsByOwner(ownerId, from, size); } + /** + * Create response entity. + * + * @param userId the user id + * @param itemDto the item dto + * @return the response entity + */ @PostMapping public ResponseEntity create(@RequestHeader(USER_ID) Long userId, @RequestBody @Valid ItemDto itemDto) { @@ -38,6 +56,13 @@ public ResponseEntity create(@RequestHeader(USER_ID) Long userId, return itemClient.create(userId, itemDto); } + /** + * Gets item by id. + * + * @param userId the user id + * @param itemId the item id + * @return the item by id + */ @GetMapping(ITEM_ID) public ResponseEntity getItemById(@RequestHeader(USER_ID) Long userId, @PathVariable("item-id") Long itemId) { @@ -45,6 +70,14 @@ public ResponseEntity getItemById(@RequestHeader(USER_ID) Long userId, return itemClient.getItemById(userId, itemId); } + /** + * Update response entity. + * + * @param itemDto the item dto + * @param itemId the item id + * @param userId the user id + * @return the response entity + */ @ResponseBody @PatchMapping(ITEM_ID) public ResponseEntity update(@RequestBody ItemDto itemDto, @PathVariable("item-id") Long itemId, @@ -53,12 +86,27 @@ public ResponseEntity update(@RequestBody ItemDto itemDto, @PathVariable return itemClient.update(itemDto, itemId, userId); } + /** + * Delete response entity. + * + * @param itemId the item id + * @param ownerId the owner id + * @return the response entity + */ @DeleteMapping(ITEM_ID) public ResponseEntity delete(@PathVariable("item-id") Long itemId, @RequestHeader(USER_ID) Long ownerId) { log.info("Получен DELETE-запрос к эндпоинту: '/items' на удаление вещи с ID={}", itemId); return itemClient.delete(itemId, ownerId); } + /** + * Gets items by search query. + * + * @param text the text + * @param from the from + * @param size the size + * @return the items by search query + */ @GetMapping("/search") public ResponseEntity getItemsBySearchQuery(@RequestParam String text, @PositiveOrZero @RequestParam(defaultValue = "0") Integer from, @@ -67,6 +115,14 @@ public ResponseEntity getItemsBySearchQuery(@RequestParam String text, return itemClient.getItemsBySearchQuery(text, from, size); } + /** + * Create comment response entity. + * + * @param commentDto the comment dto + * @param userId the user id + * @param itemId the item id + * @return the response entity + */ @ResponseBody @PostMapping("/{item-id}/comment") public ResponseEntity createComment(@RequestBody @Valid CommentDto commentDto, diff --git a/gateway/src/main/java/ru/practicum/shareit/item/dto/CommentDto.java b/gateway/src/main/java/ru/practicum/shareit/item/dto/CommentDto.java index d803ab1..604ce80 100644 --- a/gateway/src/main/java/ru/practicum/shareit/item/dto/CommentDto.java +++ b/gateway/src/main/java/ru/practicum/shareit/item/dto/CommentDto.java @@ -8,6 +8,9 @@ import java.time.LocalDateTime; +/** + * The type Comment dto. + */ @Data @AllArgsConstructor @FieldDefaults(level = AccessLevel.PRIVATE) diff --git a/gateway/src/main/java/ru/practicum/shareit/item/dto/ItemDto.java b/gateway/src/main/java/ru/practicum/shareit/item/dto/ItemDto.java index 24ef98c..5cc63cb 100644 --- a/gateway/src/main/java/ru/practicum/shareit/item/dto/ItemDto.java +++ b/gateway/src/main/java/ru/practicum/shareit/item/dto/ItemDto.java @@ -11,6 +11,9 @@ import java.util.List; +/** + * The type Item dto. + */ @Data @AllArgsConstructor @FieldDefaults(level = AccessLevel.PRIVATE) diff --git a/gateway/src/main/java/ru/practicum/shareit/request/RequestClient.java b/gateway/src/main/java/ru/practicum/shareit/request/RequestClient.java index bc02206..ac3baa5 100644 --- a/gateway/src/main/java/ru/practicum/shareit/request/RequestClient.java +++ b/gateway/src/main/java/ru/practicum/shareit/request/RequestClient.java @@ -10,10 +10,19 @@ import ru.practicum.shareit.client.BaseClient; import ru.practicum.shareit.request.dto.RequestDto; +/** + * The type Request client. + */ @Service public class RequestClient extends BaseClient { private static final String API_PREFIX = "/requests"; + /** + * Instantiates a new Request client. + * + * @param serverUrl the server url + * @param builder the builder + */ @Autowired public RequestClient(@Value("${shareit-server.url}") String serverUrl, RestTemplateBuilder builder) { super( @@ -24,18 +33,46 @@ public RequestClient(@Value("${shareit-server.url}") String serverUrl, RestTempl ); } + /** + * Create response entity. + * + * @param requestDto the request dto + * @param requestorId the requestor id + * @return the response entity + */ public ResponseEntity create(RequestDto requestDto, Long requestorId) { return post("", requestorId, requestDto); } + /** + * Gets item request by id. + * + * @param userId the user id + * @param requestId the request id + * @return the item request by id + */ public ResponseEntity getItemRequestById(Long userId, Long requestId) { return get("/" + requestId, userId); } + /** + * Gets own item requests. + * + * @param userId the user id + * @return the own item requests + */ public ResponseEntity getOwnItemRequests(Long userId) { return get("", userId); } + /** + * Gets all item requests. + * + * @param userId the user id + * @param from the from + * @param size the size + * @return the all item requests + */ public ResponseEntity getAllItemRequests(Long userId, Integer from, Integer size) { String path = "/all" + "?from=" + from; if (size != null) { diff --git a/gateway/src/main/java/ru/practicum/shareit/request/RequestController.java b/gateway/src/main/java/ru/practicum/shareit/request/RequestController.java index 11d82e7..d968080 100644 --- a/gateway/src/main/java/ru/practicum/shareit/request/RequestController.java +++ b/gateway/src/main/java/ru/practicum/shareit/request/RequestController.java @@ -10,6 +10,9 @@ import org.springframework.web.bind.annotation.*; import ru.practicum.shareit.request.dto.RequestDto; +/** + * The type Request controller. + */ @Controller @RequiredArgsConstructor @Slf4j @@ -19,6 +22,13 @@ public class RequestController { private static final String USER_ID = "X-Sharer-User-Id"; private final RequestClient requestClient; + /** + * Create response entity. + * + * @param requestDto the request dto + * @param requestorId the requestor id + * @return the response entity + */ @ResponseBody @PostMapping public ResponseEntity create(@RequestBody @Valid RequestDto requestDto, @@ -28,6 +38,13 @@ public ResponseEntity create(@RequestBody @Valid RequestDto requestDto, return requestClient.create(requestDto, requestorId); } + /** + * Gets item request by id. + * + * @param itemRequestId the item request id + * @param userId the user id + * @return the item request by id + */ @GetMapping("/{request-id}") public ResponseEntity getItemRequestById(@PathVariable("request-id") Long itemRequestId, @RequestHeader(USER_ID) Long userId) { @@ -36,6 +53,12 @@ public ResponseEntity getItemRequestById(@PathVariable("request-id") Lon } + /** + * Gets own item requests. + * + * @param userId the user id + * @return the own item requests + */ @GetMapping public ResponseEntity getOwnItemRequests(@RequestHeader(USER_ID) Long userId) { log.info("Получен GET-запрос к эндпоинту: '/requests' на получение запросов пользователя ID={}", @@ -43,6 +66,14 @@ public ResponseEntity getOwnItemRequests(@RequestHeader(USER_ID) Long us return requestClient.getOwnItemRequests(userId); } + /** + * Gets all item requests. + * + * @param userId the user id + * @param from the from + * @param size the size + * @return the all item requests + */ @GetMapping("/all") public ResponseEntity getAllItemRequests(@RequestHeader(USER_ID) Long userId, @PositiveOrZero @RequestParam(name = "from", defaultValue = "0") 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 index bc4f5f8..8d9c4cd 100644 --- a/gateway/src/main/java/ru/practicum/shareit/request/dto/RequestDto.java +++ b/gateway/src/main/java/ru/practicum/shareit/request/dto/RequestDto.java @@ -11,6 +11,9 @@ import java.time.LocalDateTime; import java.util.List; +/** + * The type Request dto. + */ @Data @AllArgsConstructor @FieldDefaults(level = AccessLevel.PRIVATE) diff --git a/gateway/src/main/java/ru/practicum/shareit/user/UserClient.java b/gateway/src/main/java/ru/practicum/shareit/user/UserClient.java index d11107d..c0fbfce 100644 --- a/gateway/src/main/java/ru/practicum/shareit/user/UserClient.java +++ b/gateway/src/main/java/ru/practicum/shareit/user/UserClient.java @@ -10,10 +10,19 @@ import ru.practicum.shareit.client.BaseClient; import ru.practicum.shareit.user.dto.UserDto; +/** + * The type User client. + */ @Service public class UserClient extends BaseClient { private static final String API = "/users"; + /** + * Instantiates a new User client. + * + * @param serverUrl the server url + * @param builder the builder + */ @Autowired public UserClient(@Value("${shareit-server.url}") String serverUrl, RestTemplateBuilder builder) { super( @@ -24,22 +33,52 @@ public UserClient(@Value("${shareit-server.url}") String serverUrl, RestTemplate ); } + /** + * Create response entity. + * + * @param userDto the user dto + * @return the response entity + */ public ResponseEntity create(UserDto userDto) { return post("", userDto); } + /** + * Gets user by id. + * + * @param userId the user id + * @return the user by id + */ public ResponseEntity getUserById(Long userId) { return get("/" + userId); } + /** + * Gets users. + * + * @return the users + */ public ResponseEntity getUsers() { return get(""); } + /** + * Update response entity. + * + * @param userDto the user dto + * @param userId the user id + * @return the response entity + */ public ResponseEntity update(UserDto userDto, long userId) { return patch("/" + userId, userDto); } + /** + * Delete response entity. + * + * @param userId the user id + * @return the response entity + */ public ResponseEntity delete(Long userId) { return delete("/" + userId); } diff --git a/gateway/src/main/java/ru/practicum/shareit/user/UserController.java b/gateway/src/main/java/ru/practicum/shareit/user/UserController.java index 1810521..d1472db 100644 --- a/gateway/src/main/java/ru/practicum/shareit/user/UserController.java +++ b/gateway/src/main/java/ru/practicum/shareit/user/UserController.java @@ -9,6 +9,9 @@ import org.springframework.web.bind.annotation.*; import ru.practicum.shareit.user.dto.UserDto; +/** + * The type User controller. + */ @Controller @RequestMapping(path = "/users") @RequiredArgsConstructor @@ -18,16 +21,33 @@ public class UserController { private final UserClient userClient; private static final String PATH = "{user-id}"; + /** + * Gets users. + * + * @return the users + */ @GetMapping public ResponseEntity getUsers() { return userClient.getUsers(); } + /** + * Gets user by id. + * + * @param userId the user id + * @return the user by id + */ @GetMapping(PATH) public ResponseEntity getUserById(@PathVariable("user-id") Long userId) { return userClient.getUserById(userId); } + /** + * Create response entity. + * + * @param userDto the user dto + * @return the response entity + */ @ResponseBody @PostMapping public ResponseEntity create(@Valid @RequestBody UserDto userDto) { @@ -35,6 +55,13 @@ public ResponseEntity create(@Valid @RequestBody UserDto userDto) { return userClient.create(userDto); } + /** + * Update response entity. + * + * @param userId the user id + * @param userDto the user dto + * @return the response entity + */ @ResponseBody @PatchMapping(PATH) public ResponseEntity update(@PathVariable("user-id") Long userId, @RequestBody UserDto userDto) { @@ -42,6 +69,12 @@ public ResponseEntity update(@PathVariable("user-id") Long userId, @Requ return userClient.update(userDto, userId); } + /** + * Delete response entity. + * + * @param userId the user id + * @return the response entity + */ @DeleteMapping(PATH) public ResponseEntity delete(@PathVariable("user-id") Long userId) { log.info("Получен DELETE-запрос к эндпоинту: '/users' на обновление пользователя с ID{}", userId); diff --git a/gateway/src/main/java/ru/practicum/shareit/user/dto/UserDto.java b/gateway/src/main/java/ru/practicum/shareit/user/dto/UserDto.java index bd0511d..5653133 100644 --- a/gateway/src/main/java/ru/practicum/shareit/user/dto/UserDto.java +++ b/gateway/src/main/java/ru/practicum/shareit/user/dto/UserDto.java @@ -5,6 +5,9 @@ import lombok.AllArgsConstructor; import lombok.Data; +/** + * The type User dto. + */ @Data @AllArgsConstructor public class UserDto { diff --git a/server/src/main/java/ru/practicum/shareit/booking/Booking.java b/server/src/main/java/ru/practicum/shareit/booking/Booking.java index 37b3a66..4aa7510 100644 --- a/server/src/main/java/ru/practicum/shareit/booking/Booking.java +++ b/server/src/main/java/ru/practicum/shareit/booking/Booking.java @@ -9,6 +9,9 @@ import java.time.LocalDateTime; +/** + * The type Booking. + */ @Entity @Getter @Setter diff --git a/server/src/main/java/ru/practicum/shareit/booking/BookingController.java b/server/src/main/java/ru/practicum/shareit/booking/BookingController.java index e7ec33e..5fa82d6 100644 --- a/server/src/main/java/ru/practicum/shareit/booking/BookingController.java +++ b/server/src/main/java/ru/practicum/shareit/booking/BookingController.java @@ -8,6 +8,9 @@ import java.util.List; +/** + * The type Booking controller. + */ @Slf4j @RestController @RequestMapping(path = "/bookings") @@ -16,11 +19,23 @@ public class BookingController { private final BookingService service; private static final String BOOKING_ID = "{booking-id}"; + /** + * Instantiates a new Booking controller. + * + * @param bookingService the booking service + */ @Autowired public BookingController(BookingService bookingService) { this.service = bookingService; } + /** + * Create booking dto. + * + * @param bookingInputDto the booking input dto + * @param bookerId the booker id + * @return the booking dto + */ @ResponseBody @PostMapping public BookingDto create(@RequestBody BookingInputDto bookingInputDto, @@ -30,6 +45,14 @@ public BookingDto create(@RequestBody BookingInputDto bookingInputDto, return service.create(bookingInputDto, bookerId); } + /** + * Update booking dto. + * + * @param bookingId the booking id + * @param userId the user id + * @param approved the approved + * @return the booking dto + */ @ResponseBody @PatchMapping(BOOKING_ID) public BookingDto update(@PathVariable("booking-id") Long bookingId, @@ -38,12 +61,26 @@ public BookingDto update(@PathVariable("booking-id") Long bookingId, return service.update(bookingId, userId, approved); } + /** + * Gets booking by id. + * + * @param bookingId the booking id + * @param userId the user id + * @return the booking by id + */ @GetMapping(BOOKING_ID) public BookingDto getBookingById(@PathVariable("booking-id") Long bookingId, @RequestHeader(USER_ID) Long userId) { log.info("Получен GET-запрос к эндпоинту: '/bookings' на получение бронирования с ID={}", bookingId); return service.getBookingById(bookingId, userId); } + /** + * Gets bookings. + * + * @param state the state + * @param userId the user id + * @return the bookings + */ @GetMapping public List getBookings(@RequestParam(name = "state", defaultValue = "ALL") String state, @RequestHeader(USER_ID) Long userId) { @@ -52,6 +89,13 @@ public List getBookings(@RequestParam(name = "state", defaultValue = return service.getBookings(state, userId); } + /** + * Gets bookings owner. + * + * @param state the state + * @param userId the user id + * @return the bookings owner + */ @GetMapping("/owner") public List getBookingsOwner(@RequestParam(name = "state", defaultValue = "ALL") String state, @RequestHeader(USER_ID) Long userId) { diff --git a/server/src/main/java/ru/practicum/shareit/booking/BookingMapper.java b/server/src/main/java/ru/practicum/shareit/booking/BookingMapper.java index a61efa2..ccefe95 100644 --- a/server/src/main/java/ru/practicum/shareit/booking/BookingMapper.java +++ b/server/src/main/java/ru/practicum/shareit/booking/BookingMapper.java @@ -12,6 +12,9 @@ import ru.practicum.shareit.user.UserMapper; import ru.practicum.shareit.user.UserServiceImpl; +/** + * The type Booking mapper. + */ @Component @FieldDefaults(level = AccessLevel.PRIVATE) public class BookingMapper { @@ -20,6 +23,14 @@ public class BookingMapper { UserMapper userMapper; ItemMapper itemMapper; + /** + * Instantiates a new Booking mapper. + * + * @param userService the user service + * @param itemService the item service + * @param userMapper the user mapper + * @param itemMapper the item mapper + */ @Autowired public BookingMapper(UserServiceImpl userService, ItemServiceImpl itemService, UserMapper userMapper, ItemMapper itemMapper) { @@ -29,6 +40,12 @@ public BookingMapper(UserServiceImpl userService, ItemServiceImpl itemService, this.itemMapper = itemMapper; } + /** + * To booking dto booking dto. + * + * @param booking the booking + * @return the booking dto + */ public BookingDto toBookingDto(Booking booking) { if (booking != null) { return new BookingDto( @@ -44,6 +61,12 @@ public BookingDto toBookingDto(Booking booking) { } } + /** + * To booking short dto booking short dto. + * + * @param booking the booking + * @return the booking short dto + */ public BookingShortDto toBookingShortDto(Booking booking) { if (booking != null) { return new BookingShortDto( @@ -57,6 +80,13 @@ public BookingShortDto toBookingShortDto(Booking booking) { } } + /** + * To booking booking. + * + * @param bookingInputDto the booking input dto + * @param bookerId the booker id + * @return the booking + */ public Booking toBooking(BookingInputDto bookingInputDto, Long bookerId) { return new Booking( null, diff --git a/server/src/main/java/ru/practicum/shareit/booking/BookingRepository.java b/server/src/main/java/ru/practicum/shareit/booking/BookingRepository.java index eb1c93f..b10e14f 100644 --- a/server/src/main/java/ru/practicum/shareit/booking/BookingRepository.java +++ b/server/src/main/java/ru/practicum/shareit/booking/BookingRepository.java @@ -6,33 +6,139 @@ import java.time.LocalDateTime; import java.util.List; +/** + * The interface Booking repository. + */ public interface BookingRepository extends JpaRepository { + /** + * Find by booker id list. + * + * @param bookerId the booker id + * @param sort the sort + * @return the list + */ List findByBookerId(Long bookerId, Sort sort); + /** + * Find by booker id and start is before and end is after list. + * + * @param bookerId the booker id + * @param start the start + * @param end the end + * @param sort the sort + * @return the list + */ List findByBookerIdAndStartIsBeforeAndEndIsAfter(Long bookerId, LocalDateTime start, LocalDateTime end, Sort sort); + /** + * Find by booker id and end is before list. + * + * @param bookerId the booker id + * @param end the end + * @param sort the sort + * @return the list + */ List findByBookerIdAndEndIsBefore(Long bookerId, LocalDateTime end, Sort sort); + /** + * Find by booker id and start is after list. + * + * @param bookerId the booker id + * @param start the start + * @param sort the sort + * @return the list + */ List findByBookerIdAndStartIsAfter(Long bookerId, LocalDateTime start, Sort sort); + /** + * Find by booker id and status list. + * + * @param bookerId the booker id + * @param status the status + * @param sort the sort + * @return the list + */ List findByBookerIdAndStatus(Long bookerId, BookingStatus status, Sort sort); + /** + * Find by item owner id list. + * + * @param ownerId the owner id + * @param sort the sort + * @return the list + */ List findByItemOwnerId(Long ownerId, Sort sort); + /** + * Find by item owner id and start is before and end is after list. + * + * @param ownerId the owner id + * @param start the start + * @param end the end + * @param sort the sort + * @return the list + */ List findByItemOwnerIdAndStartIsBeforeAndEndIsAfter(Long ownerId, LocalDateTime start, LocalDateTime end, Sort sort); + /** + * Find by item owner id and end is before list. + * + * @param bookerId the booker id + * @param end the end + * @param sort the sort + * @return the list + */ List findByItemOwnerIdAndEndIsBefore(Long bookerId, LocalDateTime end, Sort sort); + /** + * Find by item owner id and start is after list. + * + * @param bookerId the booker id + * @param start the start + * @param sort the sort + * @return the list + */ List findByItemOwnerIdAndStartIsAfter(Long bookerId, LocalDateTime start, Sort sort); + /** + * Find by item owner id and status list. + * + * @param bookerId the booker id + * @param status the status + * @param sort the sort + * @return the list + */ List findByItemOwnerIdAndStatus(Long bookerId, BookingStatus status, Sort sort); + /** + * Find first by item id and end before order by end desc booking. + * + * @param itemId the item id + * @param end the end + * @return the booking + */ Booking findFirstByItemIdAndEndBeforeOrderByEndDesc(Long itemId, LocalDateTime end); + /** + * Find first by item id and start after order by start asc booking. + * + * @param itemId the item id + * @param end the end + * @return the booking + */ Booking findFirstByItemIdAndStartAfterOrderByStartAsc(Long itemId, LocalDateTime end); + /** + * Find first by item id and booker id and end is before and status booking. + * + * @param itemId the item id + * @param userId the user id + * @param end the end + * @param status the status + * @return the booking + */ Booking findFirstByItemIdAndBookerIdAndEndIsBeforeAndStatus(Long itemId, Long userId, LocalDateTime end, BookingStatus status); } \ No newline at end of file diff --git a/server/src/main/java/ru/practicum/shareit/booking/BookingService.java b/server/src/main/java/ru/practicum/shareit/booking/BookingService.java index 866bdc8..64bae7e 100644 --- a/server/src/main/java/ru/practicum/shareit/booking/BookingService.java +++ b/server/src/main/java/ru/practicum/shareit/booking/BookingService.java @@ -6,21 +6,79 @@ import java.util.List; +/** + * The interface Booking service. + */ public interface BookingService { + /** + * Create booking dto. + * + * @param bookingDto the booking dto + * @param bookerId the booker id + * @return the booking dto + */ BookingDto create(BookingInputDto bookingDto, Long bookerId); + /** + * Update booking dto. + * + * @param bookingId the booking id + * @param userId the user id + * @param approved the approved + * @return the booking dto + */ BookingDto update(Long bookingId, Long userId, Boolean approved); + /** + * Gets booking by id. + * + * @param bookingId the booking id + * @param userId the user id + * @return the booking by id + */ BookingDto getBookingById(Long bookingId, Long userId); + /** + * Gets bookings. + * + * @param state the state + * @param userId the user id + * @return the bookings + */ List getBookings(String state, Long userId); + /** + * Gets bookings owner. + * + * @param state the state + * @param userId the user id + * @return the bookings owner + */ List getBookingsOwner(String state, Long userId); + /** + * Gets last booking. + * + * @param itemId the item id + * @return the last booking + */ BookingShortDto getLastBooking(Long itemId); + /** + * Gets next booking. + * + * @param itemId the item id + * @return the next booking + */ BookingShortDto getNextBooking(Long itemId); + /** + * Gets booking with user booked item. + * + * @param itemId the item id + * @param userId the user id + * @return the booking with user booked item + */ Booking getBookingWithUserBookedItem(Long itemId, Long userId); } \ No newline at end of file diff --git a/server/src/main/java/ru/practicum/shareit/exception/BookingNotFoundException.java b/server/src/main/java/ru/practicum/shareit/exception/BookingNotFoundException.java index cb60b59..f312d45 100644 --- a/server/src/main/java/ru/practicum/shareit/exception/BookingNotFoundException.java +++ b/server/src/main/java/ru/practicum/shareit/exception/BookingNotFoundException.java @@ -2,8 +2,16 @@ import lombok.extern.slf4j.Slf4j; +/** + * The type Booking not found exception. + */ @Slf4j public class BookingNotFoundException extends IllegalArgumentException { + /** + * Instantiates a new Booking not found exception. + * + * @param message the message + */ public BookingNotFoundException(String message) { super(message); log.error(message); diff --git a/server/src/main/java/ru/practicum/shareit/exception/ItemNotFoundException.java b/server/src/main/java/ru/practicum/shareit/exception/ItemNotFoundException.java index 7eb5543..af08703 100644 --- a/server/src/main/java/ru/practicum/shareit/exception/ItemNotFoundException.java +++ b/server/src/main/java/ru/practicum/shareit/exception/ItemNotFoundException.java @@ -2,8 +2,16 @@ import lombok.extern.slf4j.Slf4j; +/** + * The type Item not found exception. + */ @Slf4j public class ItemNotFoundException extends IllegalArgumentException { + /** + * Instantiates a new Item not found exception. + * + * @param message the message + */ public ItemNotFoundException(String message) { super(message); log.error(message); diff --git a/server/src/main/java/ru/practicum/shareit/exception/RequestNotFoundException.java b/server/src/main/java/ru/practicum/shareit/exception/RequestNotFoundException.java index 318207c..dc7fba1 100644 --- a/server/src/main/java/ru/practicum/shareit/exception/RequestNotFoundException.java +++ b/server/src/main/java/ru/practicum/shareit/exception/RequestNotFoundException.java @@ -2,8 +2,16 @@ import lombok.extern.slf4j.Slf4j; +/** + * The type Request not found exception. + */ @Slf4j public class RequestNotFoundException extends IllegalArgumentException { + /** + * Instantiates a new Request not found exception. + * + * @param message the message + */ public RequestNotFoundException(String message) { super(message); log.error(message); diff --git a/server/src/main/java/ru/practicum/shareit/exception/UserAlreadyExistsException.java b/server/src/main/java/ru/practicum/shareit/exception/UserAlreadyExistsException.java index 3dd82f4..03aa412 100644 --- a/server/src/main/java/ru/practicum/shareit/exception/UserAlreadyExistsException.java +++ b/server/src/main/java/ru/practicum/shareit/exception/UserAlreadyExistsException.java @@ -2,9 +2,17 @@ import lombok.extern.slf4j.Slf4j; +/** + * The type User already exists exception. + */ @Slf4j public class UserAlreadyExistsException extends IllegalArgumentException { + /** + * Instantiates a new User already exists exception. + * + * @param message the message + */ public UserAlreadyExistsException(String message) { super(message); log.error(message); diff --git a/server/src/main/java/ru/practicum/shareit/exception/UserNotFoundException.java b/server/src/main/java/ru/practicum/shareit/exception/UserNotFoundException.java index 38cfbe0..d7ee751 100644 --- a/server/src/main/java/ru/practicum/shareit/exception/UserNotFoundException.java +++ b/server/src/main/java/ru/practicum/shareit/exception/UserNotFoundException.java @@ -2,9 +2,17 @@ import lombok.extern.slf4j.Slf4j; +/** + * The type User not found exception. + */ @Slf4j public class UserNotFoundException extends IllegalArgumentException { + /** + * Instantiates a new User not found exception. + * + * @param message the message + */ public UserNotFoundException(String message) { super(message); log.error(message); diff --git a/server/src/main/java/ru/practicum/shareit/exception/ValidationException.java b/server/src/main/java/ru/practicum/shareit/exception/ValidationException.java index 2760e8d..1fa1be0 100644 --- a/server/src/main/java/ru/practicum/shareit/exception/ValidationException.java +++ b/server/src/main/java/ru/practicum/shareit/exception/ValidationException.java @@ -2,8 +2,16 @@ import lombok.extern.slf4j.Slf4j; +/** + * The type Validation exception. + */ @Slf4j public class ValidationException extends RuntimeException { + /** + * Instantiates a new Validation exception. + * + * @param message the message + */ public ValidationException(String message) { super(message); log.error(message); diff --git a/server/src/main/java/ru/practicum/shareit/handler/ErrorHandler.java b/server/src/main/java/ru/practicum/shareit/handler/ErrorHandler.java index a5f0918..b97ee78 100644 --- a/server/src/main/java/ru/practicum/shareit/handler/ErrorHandler.java +++ b/server/src/main/java/ru/practicum/shareit/handler/ErrorHandler.java @@ -7,39 +7,78 @@ import org.springframework.web.bind.annotation.RestControllerAdvice; import ru.practicum.shareit.exception.*; +/** + * The type Error handler. + */ @RestControllerAdvice public class ErrorHandler { + /** + * Handle user not found exception error response. + * + * @param e the e + * @return the error response + */ @ExceptionHandler @ResponseStatus(HttpStatus.NOT_FOUND) public ErrorResponse handleUserNotFoundException(final UserNotFoundException e) { return new ErrorResponse(e.getMessage()); } + /** + * Handle item not found exception error response. + * + * @param e the e + * @return the error response + */ @ExceptionHandler @ResponseStatus(HttpStatus.NOT_FOUND) public ErrorResponse handleItemNotFoundException(final ItemNotFoundException e) { return new ErrorResponse(e.getMessage()); } + /** + * Handle booking not found exception error response. + * + * @param e the e + * @return the error response + */ @ExceptionHandler @ResponseStatus(HttpStatus.NOT_FOUND) public ErrorResponse handleBookingNotFoundException(final BookingNotFoundException e) { return new ErrorResponse(e.getMessage()); } + /** + * Handle validation exception error response. + * + * @param e the e + * @return the error response + */ @ExceptionHandler @ResponseStatus(HttpStatus.BAD_REQUEST) public ErrorResponse handleValidationException(final ValidationException e) { return new ErrorResponse(e.getMessage()); } + /** + * Handle method argument not validation exception error response. + * + * @param e the e + * @return the error response + */ @ExceptionHandler @ResponseStatus(HttpStatus.BAD_REQUEST) public ErrorResponse handleMethodArgumentNotValidationException(final MethodArgumentNotValidException e) { return new ErrorResponse("Аргумент не прошел валидацию!"); } + /** + * Handle user already exist exception error response. + * + * @param e the e + * @return the error response + */ @ExceptionHandler @ResponseStatus(HttpStatus.CONFLICT) public ErrorResponse handleUserAlreadyExistException(final UserAlreadyExistsException e) { diff --git a/server/src/main/java/ru/practicum/shareit/handler/ErrorResponse.java b/server/src/main/java/ru/practicum/shareit/handler/ErrorResponse.java index 6cca749..479e68a 100644 --- a/server/src/main/java/ru/practicum/shareit/handler/ErrorResponse.java +++ b/server/src/main/java/ru/practicum/shareit/handler/ErrorResponse.java @@ -1,12 +1,25 @@ package ru.practicum.shareit.handler; +/** + * The type Error response. + */ public class ErrorResponse { private final String error; + /** + * Instantiates a new Error response. + * + * @param error the error + */ public ErrorResponse(String error) { this.error = error; } + /** + * Gets error. + * + * @return the error + */ public String getError() { return error; } diff --git a/server/src/main/java/ru/practicum/shareit/item/Comment.java b/server/src/main/java/ru/practicum/shareit/item/Comment.java index 10cd557..98448ef 100644 --- a/server/src/main/java/ru/practicum/shareit/item/Comment.java +++ b/server/src/main/java/ru/practicum/shareit/item/Comment.java @@ -7,6 +7,9 @@ import java.time.LocalDateTime; +/** + * The type Comment. + */ @Entity @Table(name = "comments") @NoArgsConstructor diff --git a/server/src/main/java/ru/practicum/shareit/item/CommentDto.java b/server/src/main/java/ru/practicum/shareit/item/CommentDto.java index 3d727d8..445cf7a 100644 --- a/server/src/main/java/ru/practicum/shareit/item/CommentDto.java +++ b/server/src/main/java/ru/practicum/shareit/item/CommentDto.java @@ -6,6 +6,9 @@ import java.time.LocalDateTime; +/** + * The type Comment dto. + */ @Getter @Setter @AllArgsConstructor diff --git a/server/src/main/java/ru/practicum/shareit/item/CommentRepository.java b/server/src/main/java/ru/practicum/shareit/item/CommentRepository.java index 7f1b74f..cfc222d 100644 --- a/server/src/main/java/ru/practicum/shareit/item/CommentRepository.java +++ b/server/src/main/java/ru/practicum/shareit/item/CommentRepository.java @@ -5,6 +5,16 @@ import java.util.List; +/** + * The interface Comment repository. + */ public interface CommentRepository extends JpaRepository { + /** + * Find all by item id list. + * + * @param itemId the item id + * @param sort the sort + * @return the list + */ List findAllByItemId(Long itemId, Sort sort); } diff --git a/server/src/main/java/ru/practicum/shareit/item/Item.java b/server/src/main/java/ru/practicum/shareit/item/Item.java index 0a80734..d4f37a1 100644 --- a/server/src/main/java/ru/practicum/shareit/item/Item.java +++ b/server/src/main/java/ru/practicum/shareit/item/Item.java @@ -5,6 +5,9 @@ import lombok.experimental.FieldDefaults; import ru.practicum.shareit.user.User; +/** + * The type Item. + */ @Entity @AllArgsConstructor @NoArgsConstructor 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 deec33b..d2ec1f9 100644 --- a/server/src/main/java/ru/practicum/shareit/item/ItemController.java +++ b/server/src/main/java/ru/practicum/shareit/item/ItemController.java @@ -6,6 +6,9 @@ import java.util.List; +/** + * The type Item controller. + */ @Slf4j @RestController @RequestMapping("/items") @@ -14,17 +17,36 @@ public class ItemController { private ItemService itemService; private static final String ITEM_ID = "{item-id}"; + /** + * Instantiates a new Item controller. + * + * @param itemService the item service + */ @Autowired public ItemController(ItemService itemService) { this.itemService = itemService; } + /** + * Gets item by id. + * + * @param itemId the item id + * @param ownerId the owner id + * @return the item by id + */ @GetMapping(ITEM_ID) public ItemDto getItemById(@PathVariable("item-id") Long itemId, @RequestHeader(OWNER) Long ownerId) { log.info("Получен GET-запрос к эндпоинту: '/items' на получение вещи с ID={}", itemId); return itemService.getItemById(itemId, ownerId); } + /** + * Create item dto. + * + * @param itemDto the item dto + * @param ownerId the owner id + * @return the item dto + */ @ResponseBody @PostMapping public ItemDto create(@RequestBody ItemDto itemDto, @RequestHeader(OWNER) Long ownerId) { @@ -32,12 +54,26 @@ public ItemDto create(@RequestBody ItemDto itemDto, @RequestHeader(OWNER) Long o return itemService.create(itemDto, ownerId); } + /** + * Gets items by owner. + * + * @param ownerId the owner id + * @return the items by owner + */ @GetMapping public List getItemsByOwner(@RequestHeader(OWNER) Long ownerId) { log.info("Получен GET-запрос к эндпоинту: '/items' на получение всех вещей владельца с ID={}", ownerId); return itemService.getItemsByOwner(ownerId); } + /** + * Update item dto. + * + * @param itemDto the item dto + * @param itemId the item id + * @param ownerId the owner id + * @return the item dto + */ @ResponseBody @PatchMapping(ITEM_ID) public ItemDto update(@RequestBody ItemDto itemDto, @PathVariable("item-id") Long itemId, @@ -46,18 +82,38 @@ public ItemDto update(@RequestBody ItemDto itemDto, @PathVariable("item-id") Lon return itemService.update(itemDto, ownerId, itemId); } + /** + * Delete. + * + * @param itemId the item id + * @param ownerId the owner id + */ @DeleteMapping(ITEM_ID) public void delete(@PathVariable("item-id") Long itemId, @RequestHeader(OWNER) Long ownerId) { log.info("Получен DELETE-запрос к эндпоинту: '/items' на удаление вещи с ID={}", itemId); itemService.delete(itemId, ownerId); } + /** + * Gets items by search query. + * + * @param text the text + * @return the items by search query + */ @GetMapping("/search") public List getItemsBySearchQuery(@RequestParam String text) { log.info("Получен GET-запрос к эндпоинту: '/items/search' на поиск вещи с текстом={}", text); return itemService.getItemsBySearchQuery(text); } + /** + * Create comment comment dto. + * + * @param commentDto the comment dto + * @param userId the user id + * @param itemId the item id + * @return the comment dto + */ @ResponseBody @PostMapping("/{item-id}/comment") public CommentDto createComment(@RequestBody CommentDto commentDto, @RequestHeader(OWNER) Long userId, diff --git a/server/src/main/java/ru/practicum/shareit/item/ItemDto.java b/server/src/main/java/ru/practicum/shareit/item/ItemDto.java index 9ed4c2f..dfcff4a 100644 --- a/server/src/main/java/ru/practicum/shareit/item/ItemDto.java +++ b/server/src/main/java/ru/practicum/shareit/item/ItemDto.java @@ -8,6 +8,9 @@ import java.util.List; +/** + * The type Item dto. + */ @Getter @Setter @NoArgsConstructor diff --git a/server/src/main/java/ru/practicum/shareit/item/ItemMapper.java b/server/src/main/java/ru/practicum/shareit/item/ItemMapper.java index 1511efb..298c50e 100644 --- a/server/src/main/java/ru/practicum/shareit/item/ItemMapper.java +++ b/server/src/main/java/ru/practicum/shareit/item/ItemMapper.java @@ -5,17 +5,31 @@ import org.springframework.stereotype.Component; import ru.practicum.shareit.service.ValidationService; +/** + * The type Item mapper. + */ @Component public class ItemMapper { private ValidationService validationService; + /** + * Instantiates a new Item mapper. + * + * @param validationService the validation service + */ @Autowired @Lazy public ItemMapper(ValidationService validationService) { this.validationService = validationService; } + /** + * To item dto item dto. + * + * @param item the item + * @return the item dto + */ public ItemDto toItemDto(Item item) { return new ItemDto( item.getId(), @@ -29,6 +43,12 @@ public ItemDto toItemDto(Item item) { validationService.getCommentsByItemId(item.getId())); } + /** + * To item ext dto item dto. + * + * @param item the item + * @return the item dto + */ public ItemDto toItemExtDto(Item item) { return new ItemDto( item.getId(), @@ -42,6 +62,13 @@ public ItemDto toItemExtDto(Item item) { validationService.getCommentsByItemId(item.getId())); } + /** + * To item item. + * + * @param itemDto the item dto + * @param ownerId the owner id + * @return the item + */ public Item toItem(ItemDto itemDto, Long ownerId) { return new Item( itemDto.getId(), @@ -53,6 +80,12 @@ public Item toItem(ItemDto itemDto, Long ownerId) { ); } + /** + * To comment dto comment dto. + * + * @param comment the comment + * @return the comment dto + */ public CommentDto toCommentDto(Comment comment) { return new CommentDto( comment.getId(), diff --git a/server/src/main/java/ru/practicum/shareit/item/ItemRepository.java b/server/src/main/java/ru/practicum/shareit/item/ItemRepository.java index ce74654..e9d6d31 100644 --- a/server/src/main/java/ru/practicum/shareit/item/ItemRepository.java +++ b/server/src/main/java/ru/practicum/shareit/item/ItemRepository.java @@ -7,14 +7,36 @@ import java.util.List; +/** + * The interface Item repository. + */ public interface ItemRepository extends JpaRepository { + /** + * Find by owner id list. + * + * @param ownerId the owner id + * @return the list + */ List findByOwnerId(Long ownerId); + /** + * Gets items by search query. + * + * @param text the text + * @return the items by search query + */ @Query(" select i from Item i " + "where lower(i.name) like lower(concat('%', :search, '%')) " + " or lower(i.description) like lower(concat('%', :search, '%')) " + " and i.available = true") List getItemsBySearchQuery(@Param("search") String text); + /** + * Find all by request id list. + * + * @param requestId the request id + * @param sort the sort + * @return the list + */ List findAllByRequestId(Long requestId, Sort sort); } diff --git a/server/src/main/java/ru/practicum/shareit/item/ItemService.java b/server/src/main/java/ru/practicum/shareit/item/ItemService.java index 64eea83..7eae760 100644 --- a/server/src/main/java/ru/practicum/shareit/item/ItemService.java +++ b/server/src/main/java/ru/practicum/shareit/item/ItemService.java @@ -2,25 +2,94 @@ import java.util.List; +/** + * The interface Item service. + */ public interface ItemService { + /** + * Gets item by id. + * + * @param id the id + * @param userId the user id + * @return the item by id + */ ItemDto getItemById(Long id, Long userId); + /** + * Find item by id item. + * + * @param id the id + * @return the item + */ Item findItemById(Long id); + /** + * Create item dto. + * + * @param itemDto the item dto + * @param ownerId the owner id + * @return the item dto + */ ItemDto create(ItemDto itemDto, Long ownerId); + /** + * Gets items by owner. + * + * @param ownerId the owner id + * @return the items by owner + */ List getItemsByOwner(Long ownerId); + /** + * Delete. + * + * @param itemId the item id + * @param ownerId the owner id + */ void delete(Long itemId, Long ownerId); + /** + * Gets items by search query. + * + * @param text the text + * @return the items by search query + */ List getItemsBySearchQuery(String text); + /** + * Update item dto. + * + * @param itemDto the item dto + * @param ownerId the owner id + * @param itemId the item id + * @return the item dto + */ ItemDto update(ItemDto itemDto, Long ownerId, Long itemId); + /** + * Create comment comment dto. + * + * @param commentDto the comment dto + * @param itemId the item id + * @param userId the user id + * @return the comment dto + */ CommentDto createComment(CommentDto commentDto, Long itemId, Long userId); + /** + * Gets comments by item id. + * + * @param itemId the item id + * @return the comments by item id + */ List getCommentsByItemId(Long itemId); + /** + * Gets by request id. + * + * @param requestId the request id + * @return the by request id + */ List getByRequestId(Long requestId); } \ No newline at end of file diff --git a/server/src/main/java/ru/practicum/shareit/item/ItemServiceImpl.java b/server/src/main/java/ru/practicum/shareit/item/ItemServiceImpl.java index 0f587e8..e81ec68 100644 --- a/server/src/main/java/ru/practicum/shareit/item/ItemServiceImpl.java +++ b/server/src/main/java/ru/practicum/shareit/item/ItemServiceImpl.java @@ -20,6 +20,9 @@ import static java.util.stream.Collectors.toList; +/** + * The type Item service. + */ @Service @FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) public class ItemServiceImpl implements ItemService { @@ -28,6 +31,14 @@ public class ItemServiceImpl implements ItemService { ValidationService validationService; ItemMapper mapper; + /** + * Instantiates a new Item service. + * + * @param repository the repository + * @param commentRepository the comment repository + * @param validationService the validation service + * @param itemMapper the item mapper + */ @Autowired @Lazy public ItemServiceImpl(ItemRepository repository, CommentRepository commentRepository, diff --git a/server/src/main/java/ru/practicum/shareit/request/Request.java b/server/src/main/java/ru/practicum/shareit/request/Request.java index 7aa74be..8b07d4f 100644 --- a/server/src/main/java/ru/practicum/shareit/request/Request.java +++ b/server/src/main/java/ru/practicum/shareit/request/Request.java @@ -7,6 +7,9 @@ import jakarta.persistence.*; import java.time.LocalDateTime; +/** + * The type Request. + */ @Entity @Table(name = "requests") @Getter 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 6485d68..b22952e 100644 --- a/server/src/main/java/ru/practicum/shareit/request/RequestController.java +++ b/server/src/main/java/ru/practicum/shareit/request/RequestController.java @@ -8,17 +8,32 @@ import java.time.LocalDateTime; import java.util.List; +/** + * The type Request controller. + */ @RestController @RequestMapping("/requests") public class RequestController { private final RequestService requestService; private static final String header = "X-Sharer-User-Id"; + /** + * Instantiates a new Request controller. + * + * @param requestService the request service + */ @Autowired public RequestController(RequestService requestService) { this.requestService = requestService; } + /** + * Create request request dto. + * + * @param requestDto the request dto + * @param requestorId the requestor id + * @return the request dto + */ @ResponseBody @PostMapping public RequestDto createRequest(@RequestBody RequestDto requestDto, @@ -26,12 +41,27 @@ public RequestDto createRequest(@RequestBody RequestDto requestDto, return requestService.saveRequest(requestDto, requestorId, LocalDateTime.now()); } + /** + * Gets request. + * + * @param requestId the request id + * @param userId the user id + * @return the request + */ @GetMapping("/{request-id}") public RequestDto getRequest(@PathVariable("request-id") Long requestId, @RequestHeader(header) Long userId) { return requestService.getRequestById(requestId, userId); } + /** + * Gets all requests. + * + * @param userId the user id + * @param from the from + * @param size the size + * @return the all requests + */ @GetMapping("/all") public List getAllRequests(@RequestHeader(header) Long userId, @RequestParam(defaultValue = "0") Integer from, @@ -39,6 +69,12 @@ public List getAllRequests(@RequestHeader(header) Long userId, return requestService.getAllRequests(userId, from, size); } + /** + * Gets own requests. + * + * @param userId the user id + * @return the own requests + */ @GetMapping public List getOwnRequests(@RequestHeader(header) Long userId) { return requestService.getOwnRequests(userId); diff --git a/server/src/main/java/ru/practicum/shareit/request/RequestDto.java b/server/src/main/java/ru/practicum/shareit/request/RequestDto.java index 1387729..738801a 100644 --- a/server/src/main/java/ru/practicum/shareit/request/RequestDto.java +++ b/server/src/main/java/ru/practicum/shareit/request/RequestDto.java @@ -8,6 +8,9 @@ import java.time.LocalDateTime; import java.util.List; +/** + * The type Request dto. + */ @Getter @Setter @NoArgsConstructor diff --git a/server/src/main/java/ru/practicum/shareit/request/RequestMapper.java b/server/src/main/java/ru/practicum/shareit/request/RequestMapper.java index 614c6d3..525a156 100644 --- a/server/src/main/java/ru/practicum/shareit/request/RequestMapper.java +++ b/server/src/main/java/ru/practicum/shareit/request/RequestMapper.java @@ -8,6 +8,9 @@ import java.time.LocalDateTime; +/** + * The type Request mapper. + */ @Component public class RequestMapper { @@ -15,6 +18,13 @@ public class RequestMapper { private UserService userService; private ItemService itemService; + /** + * Instantiates a new Request mapper. + * + * @param userMapper the user mapper + * @param userService the user service + * @param itemService the item service + */ @Autowired public RequestMapper(UserMapper userMapper, UserService userService, ItemService itemService) { this.userMapper = userMapper; @@ -22,6 +32,12 @@ public RequestMapper(UserMapper userMapper, UserService userService, ItemService this.itemService = itemService; } + /** + * To request dto request dto. + * + * @param request the request + * @return the request dto + */ public RequestDto toRequestDto(Request request) { return new RequestDto( request.getId(), @@ -32,6 +48,14 @@ public RequestDto toRequestDto(Request request) { ); } + /** + * To request request. + * + * @param requestDto the request dto + * @param requestorId the requestor id + * @param created the created + * @return the request + */ public Request toRequest(RequestDto requestDto, Long requestorId, LocalDateTime created) { return new Request( null, diff --git a/server/src/main/java/ru/practicum/shareit/request/RequestRepository.java b/server/src/main/java/ru/practicum/shareit/request/RequestRepository.java index d005fff..4755fa4 100644 --- a/server/src/main/java/ru/practicum/shareit/request/RequestRepository.java +++ b/server/src/main/java/ru/practicum/shareit/request/RequestRepository.java @@ -7,10 +7,33 @@ import java.util.List; -public interface RequestRepository extends JpaRepository { +/** + * The interface Request repository. + */ +public interface RequestRepository extends JpaRepository { + /** + * Find all by requestor id list. + * + * @param requestorId the requestor id + * @param sort the sort + * @return the list + */ List findAllByRequestorId(Long requestorId, Sort sort); + /** + * Find all by requestor id not page. + * + * @param userId the user id + * @param pageable the pageable + * @return the page + */ Page findAllByRequestorIdNot(Long userId, Pageable pageable); + /** + * Find all by requestor id not order by created desc list. + * + * @param userId the user id + * @return the list + */ List findAllByRequestorIdNotOrderByCreatedDesc(Long userId); } diff --git a/server/src/main/java/ru/practicum/shareit/request/RequestService.java b/server/src/main/java/ru/practicum/shareit/request/RequestService.java index 934069f..ab93360 100644 --- a/server/src/main/java/ru/practicum/shareit/request/RequestService.java +++ b/server/src/main/java/ru/practicum/shareit/request/RequestService.java @@ -3,13 +3,45 @@ import java.time.LocalDateTime; import java.util.List; +/** + * The interface Request service. + */ public interface RequestService { + /** + * Save request request dto. + * + * @param requestDto the request dto + * @param requestorId the requestor id + * @param created the created + * @return the request dto + */ RequestDto saveRequest(RequestDto requestDto, Long requestorId, LocalDateTime created); + /** + * Gets request by id. + * + * @param requestId the request id + * @param userId the user id + * @return the request by id + */ RequestDto getRequestById(Long requestId, Long userId); + /** + * Gets all requests. + * + * @param userId the user id + * @param from the from + * @param size the size + * @return the all requests + */ List getAllRequests(Long userId, Integer from, Integer size); + /** + * Gets own requests. + * + * @param requestorId the requestor id + * @return the own requests + */ List getOwnRequests(Long requestorId); } diff --git a/server/src/main/java/ru/practicum/shareit/request/RequestServiceImpl.java b/server/src/main/java/ru/practicum/shareit/request/RequestServiceImpl.java index a64f1d5..91e2909 100644 --- a/server/src/main/java/ru/practicum/shareit/request/RequestServiceImpl.java +++ b/server/src/main/java/ru/practicum/shareit/request/RequestServiceImpl.java @@ -16,12 +16,22 @@ import static java.util.stream.Collectors.toList; +/** + * The type Request service. + */ @Service public class RequestServiceImpl implements RequestService { private final RequestRepository requestRepository; private final ValidationService validationService; private final RequestMapper requestMapper; + /** + * Instantiates a new Request service. + * + * @param requestRepository the request repository + * @param validationService the validation service + * @param requestMapper the request mapper + */ @Autowired public RequestServiceImpl(RequestRepository requestRepository, ValidationService validationService, RequestMapper requestMapper) { diff --git a/server/src/main/java/ru/practicum/shareit/service/ValidationService.java b/server/src/main/java/ru/practicum/shareit/service/ValidationService.java index 1553834..f2e1e70 100644 --- a/server/src/main/java/ru/practicum/shareit/service/ValidationService.java +++ b/server/src/main/java/ru/practicum/shareit/service/ValidationService.java @@ -14,6 +14,9 @@ import java.util.List; +/** + * The type Validation service. + */ @Service @FieldDefaults(level = AccessLevel.PRIVATE) public class ValidationService { @@ -21,6 +24,13 @@ public class ValidationService { ItemService itemService; BookingService bookingService; + /** + * Instantiates a new Validation service. + * + * @param userService the user service + * @param itemService the item service + * @param bookingService the booking service + */ @Autowired public ValidationService(UserService userService, ItemService itemService, BookingService bookingService) { this.userService = userService; @@ -28,6 +38,12 @@ public ValidationService(UserService userService, ItemService itemService, Booki this.bookingService = bookingService; } + /** + * Is exist user boolean. + * + * @param userId the user id + * @return the boolean + */ public boolean isExistUser(Long userId) { boolean exist = false; if (userService.getUser(userId) != null) { @@ -36,33 +52,77 @@ public boolean isExistUser(Long userId) { return exist; } + /** + * Is available item boolean. + * + * @param itemId the item id + * @return the boolean + */ public boolean isAvailableItem(Long itemId) { return itemService.findItemById(itemId).getAvailable(); } + /** + * Is item owner boolean. + * + * @param itemId the item id + * @param userId the user id + * @return the boolean + */ public boolean isItemOwner(Long itemId, Long userId) { return itemService.getItemsByOwner(userId).stream() .anyMatch(i -> i.getId().equals(itemId)); } + /** + * Find user by id user. + * + * @param userId the user id + * @return the user + */ public User findUserById(Long userId) { return userService.findUserById(userId); } + /** + * Gets last booking. + * + * @param itemId the item id + * @return the last booking + */ public BookingShortDto getLastBooking(Long itemId) { return bookingService.getLastBooking(itemId); } + /** + * Gets next booking. + * + * @param itemId the item id + * @return the next booking + */ public BookingShortDto getNextBooking(Long itemId) { return bookingService.getNextBooking(itemId); } + /** + * Gets booking with user booked item. + * + * @param itemId the item id + * @param userId the user id + * @return the booking with user booked item + */ public Booking getBookingWithUserBookedItem(Long itemId, Long userId) { return bookingService.getBookingWithUserBookedItem(itemId, userId); } + /** + * Gets comments by item id. + * + * @param itemId the item id + * @return the comments by item id + */ public List getCommentsByItemId(Long itemId) { return itemService.getCommentsByItemId(itemId); } diff --git a/server/src/main/java/ru/practicum/shareit/user/User.java b/server/src/main/java/ru/practicum/shareit/user/User.java index cffdc0e..324af0c 100644 --- a/server/src/main/java/ru/practicum/shareit/user/User.java +++ b/server/src/main/java/ru/practicum/shareit/user/User.java @@ -8,6 +8,9 @@ import java.time.Instant; import java.util.Objects; +/** + * The type User. + */ @Entity @AllArgsConstructor @NoArgsConstructor 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 0703df0..4d031b0 100644 --- a/server/src/main/java/ru/practicum/shareit/user/UserController.java +++ b/server/src/main/java/ru/practicum/shareit/user/UserController.java @@ -7,6 +7,9 @@ import java.util.List; +/** + * The type User controller. + */ @RestController @RequiredArgsConstructor @RequestMapping("/users") @@ -15,24 +18,48 @@ public class UserController { private final UserService userService; private static final String USER_ID = "{user-id}"; + /** + * Create user response entity. + * + * @param userDto the user dto + * @return the response entity + */ @PostMapping public ResponseEntity createUser(@RequestBody UserDto userDto) { UserDto createdUser = userService.saveUser(userDto); return ResponseEntity.ok(createdUser); } + /** + * Gets user. + * + * @param userId the user id + * @return the user + */ @GetMapping(USER_ID) public ResponseEntity getUser(@PathVariable("user-id") Long userId) { UserDto userDto = userService.getUser(userId); return userDto != null ? ResponseEntity.ok(userDto) : ResponseEntity.notFound().build(); } + /** + * Gets all users. + * + * @return the all users + */ @GetMapping public ResponseEntity> getAllUsers() { List users = userService.getAllUsers(); return ResponseEntity.noContent().build(); } + /** + * Update user response entity. + * + * @param userId the user id + * @param userDto the user dto + * @return the response entity + */ @PatchMapping(USER_ID) public ResponseEntity updateUser(@PathVariable("user-id") Long userId, @RequestBody UserDto userDto) { try { @@ -43,6 +70,12 @@ public ResponseEntity updateUser(@PathVariable("user-id") Long userId, } } + /** + * Delete user response entity. + * + * @param userId the user id + * @return the response entity + */ @DeleteMapping(USER_ID) public ResponseEntity deleteUser(@PathVariable("user-id") Long userId) { userService.deleteUser(userId); diff --git a/server/src/main/java/ru/practicum/shareit/user/UserDto.java b/server/src/main/java/ru/practicum/shareit/user/UserDto.java index a9c920f..e2e53a1 100644 --- a/server/src/main/java/ru/practicum/shareit/user/UserDto.java +++ b/server/src/main/java/ru/practicum/shareit/user/UserDto.java @@ -8,6 +8,9 @@ import java.time.Instant; +/** + * The type User dto. + */ @Data @AllArgsConstructor @NoArgsConstructor diff --git a/server/src/main/java/ru/practicum/shareit/user/UserMapper.java b/server/src/main/java/ru/practicum/shareit/user/UserMapper.java index a0bd663..b1cdd5f 100644 --- a/server/src/main/java/ru/practicum/shareit/user/UserMapper.java +++ b/server/src/main/java/ru/practicum/shareit/user/UserMapper.java @@ -2,8 +2,17 @@ import org.springframework.stereotype.Component; +/** + * The type User mapper. + */ @Component public class UserMapper { + /** + * To user dto user dto. + * + * @param user the user + * @return the user dto + */ public UserDto toUserDto(User user) { return new UserDto( user.getId(), @@ -13,6 +22,12 @@ public UserDto toUserDto(User user) { ); } + /** + * To user user. + * + * @param userDto the user dto + * @return the user + */ public User toUser(UserDto userDto) { return new User( userDto.getId(), diff --git a/server/src/main/java/ru/practicum/shareit/user/UserRepository.java b/server/src/main/java/ru/practicum/shareit/user/UserRepository.java index 836517d..25e105f 100644 --- a/server/src/main/java/ru/practicum/shareit/user/UserRepository.java +++ b/server/src/main/java/ru/practicum/shareit/user/UserRepository.java @@ -2,5 +2,8 @@ import org.springframework.data.jpa.repository.JpaRepository; +/** + * The interface User repository. + */ public interface UserRepository extends JpaRepository { } \ No newline at end of file diff --git a/server/src/main/java/ru/practicum/shareit/user/UserService.java b/server/src/main/java/ru/practicum/shareit/user/UserService.java index b44dac6..b6cc7e4 100644 --- a/server/src/main/java/ru/practicum/shareit/user/UserService.java +++ b/server/src/main/java/ru/practicum/shareit/user/UserService.java @@ -2,19 +2,63 @@ import java.util.List; +/** + * The interface User service. + */ public interface UserService { + /** + * Save user user dto. + * + * @param userDto the user dto + * @return the user dto + */ UserDto saveUser(UserDto userDto); + /** + * Gets user. + * + * @param id the id + * @return the user + */ UserDto getUser(Long id); + /** + * Gets all users. + * + * @return the all users + */ List getAllUsers(); + /** + * Delete user. + * + * @param id the id + */ void deleteUser(Long id); + /** + * Update user by id user dto. + * + * @param id the id + * @param userDto the user dto + * @return the user dto + */ UserDto updateUserById(Long id, UserDto userDto); + /** + * Find user by id user. + * + * @param userId the user id + * @return the user + */ User findUserById(Long userId); + /** + * Exists by id boolean. + * + * @param id the id + * @return the boolean + */ boolean existsById(Long id); } \ No newline at end of file diff --git a/server/src/main/java/ru/practicum/shareit/user/UserServiceImpl.java b/server/src/main/java/ru/practicum/shareit/user/UserServiceImpl.java index e498fe0..e8443c2 100644 --- a/server/src/main/java/ru/practicum/shareit/user/UserServiceImpl.java +++ b/server/src/main/java/ru/practicum/shareit/user/UserServiceImpl.java @@ -8,11 +8,20 @@ import java.util.List; import java.util.stream.Collectors; +/** + * The type User service. + */ @Service public class UserServiceImpl implements UserService { private final UserRepository userRepository; private final UserMapper userMapper; + /** + * Instantiates a new User service. + * + * @param userRepository the user repository + * @param userMapper the user mapper + */ @Autowired public UserServiceImpl(UserRepository userRepository, UserMapper userMapper) { this.userRepository = userRepository; diff --git a/server/src/main/java/ru/practicum/shareit/util/Pagination.java b/server/src/main/java/ru/practicum/shareit/util/Pagination.java index b282720..633c74e 100644 --- a/server/src/main/java/ru/practicum/shareit/util/Pagination.java +++ b/server/src/main/java/ru/practicum/shareit/util/Pagination.java @@ -3,6 +3,9 @@ import lombok.Getter; import ru.practicum.shareit.exception.ValidationException; +/** + * The type Pagination. + */ @Getter public class Pagination { private Integer pageSize; @@ -10,6 +13,12 @@ public class Pagination { private Integer totalPages; + /** + * Instantiates a new Pagination. + * + * @param from the from + * @param size the size + */ public Pagination(Integer from, Integer size) { if (size != null) { if ((from < 0) || (size < 0)) { From ce10c50195d6e33c54fc7be453856668cc5e7e7e Mon Sep 17 00:00:00 2001 From: yiqes <155768368+yiqes@users.noreply.github.com> Date: Thu, 16 Jan 2025 14:41:12 +0300 Subject: [PATCH 2/2] feat: added documentation by using javadoc + information in README.md --- README.md | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/README.md b/README.md index 0fd8044..f94a624 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,134 @@ ## Запуск приложения +## Запуск с использованием Docker + +### 1. **Запуск с использованием Docker Compose** + +1. Убедитесь, что у вас установлены: + - Docker + - Docker Compose + +2. Клонируйте репозиторий: + ```bash + git clone https://github.com/your-repo/shareit.git + cd shareit + ``` + +3. Запустите приложение с помощью Docker Compose: + ```bash + docker-compose up --build + ``` + +4. После запуска приложение будет доступно: + - **Gateway**: `http://localhost:8080` + - **Server**: `http://localhost:9090` + +5. Остановка приложения: + ```bash + docker-compose down + ``` + +--- + +### 2. **Конфигурация Docker Compose** + +Конфигурация `docker-compose.yml` включает следующие сервисы: + +- **PostgreSQL**: База данных для хранения данных приложения. +- **Gateway**: Шлюз для обработки запросов и взаимодействия с основным сервисом. +- **Server**: Основной сервис приложения. + +Пример конфигурации `docker-compose.yml`: +```yaml +version: '3.8' + +services: + db: + image: postgres:16.1 + container_name: postgres + ports: + - "6541:5432" + environment: + - POSTGRES_PASSWORD=shareit + - POSTGRES_USER=shareit + - POSTGRES_DB=shareit + volumes: + - ./server/src/main/resources/schema.sql:/docker-entrypoint-initdb.d/initDB.sql + healthcheck: + test: pg_isready -q -d $$POSTGRES_DB -U $$POSTGRES_USER + timeout: 5s + interval: 5s + retries: 10 + + 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: + condition: service_healthy + environment: + - SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/shareit + - SPRING_DATASOURCE_USERNAME=shareit + - SPRING_DATASOURCE_PASSWORD=shareit + volumes: + - ./server/src/main/resources/application.properties:/app/application.properties +``` + +--- + +### 3. **Сборка и запуск отдельных контейнеров** + +Если вы хотите собрать и запустить контейнеры вручную: + +1. Соберите образы: + ```bash + docker build -t shareit-gateway -f gateway/Dockerfile . + docker build -t shareit-server -f server/Dockerfile . + ``` + +2. Запустите контейнеры: + ```bash + docker run -d --name postgres -p 6541:5432 \ + -e POSTGRES_PASSWORD=shareit \ + -e POSTGRES_USER=shareit \ + -e POSTGRES_DB=shareit \ + postgres:16.1 + + docker run -d --name shareit-server -p 9090:9090 \ + -e SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/shareit \ + -e SPRING_DATASOURCE_USERNAME=shareit \ + -e SPRING_DATASOURCE_PASSWORD=shareit \ + shareit-server + + docker run -d --name shareit-gateway -p 8080:8080 \ + -e SHAREIT_SERVER_URL=http://server:9090 \ + shareit-gateway + ``` + +3. Остановка контейнеров: + ```bash + docker stop shareit-gateway shareit-server postgres + docker rm shareit-gateway shareit-server postgres + ``` + +--- + +## Запуск с использованием Maven 1. Убедитесь, что у вас установлены: - Java 21 - Maven