generated from yandex-praktikum/java-shareit
-
Notifications
You must be signed in to change notification settings - Fork 0
add-controllers. Добавить апи для пользователей и вещей. #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,28 @@ | ||
| package ru.practicum.shareit.booking; | ||
|
|
||
| import lombok.Data; | ||
| import ru.practicum.shareit.item.model.Item; | ||
| import ru.practicum.shareit.user.model.User; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| /** | ||
| * TODO Sprint add-bookings. | ||
| */ | ||
| @Data | ||
| public class Booking { | ||
| private Long id; | ||
| private LocalDateTime start; | ||
| private LocalDateTime end; | ||
| private Item item; | ||
| private User booker; | ||
| private Status status; | ||
|
|
||
| enum Status { | ||
| WAITING, | ||
| APPROVED, | ||
| REJECTED, | ||
| CANCELED | ||
| } | ||
|
|
||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/ru/practicum/shareit/exception/AlreadyExistException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package ru.practicum.shareit.exception; | ||
|
|
||
| public class AlreadyExistException extends RuntimeException { | ||
| public AlreadyExistException(String message) { | ||
| super(message); | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
src/main/java/ru/practicum/shareit/exception/ErrorHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package ru.practicum.shareit.exception; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.web.bind.MethodArgumentNotValidException; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.bind.annotation.ResponseStatus; | ||
| import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
|
||
| @Slf4j | ||
| @RestControllerAdvice | ||
| public class ErrorHandler { | ||
|
|
||
| @ExceptionHandler | ||
| @ResponseStatus(HttpStatus.BAD_REQUEST) | ||
| public ErrorResponse handleValidationException(MethodArgumentNotValidException e) { | ||
| log.debug(e.getBindingResult().getAllErrors().getFirst().getDefaultMessage(), e); | ||
| return new ErrorResponse(HttpStatus.BAD_REQUEST.value(), e.getBindingResult().getAllErrors().getFirst().getDefaultMessage()); | ||
| } | ||
|
|
||
| @ExceptionHandler | ||
| @ResponseStatus(HttpStatus.NOT_FOUND) | ||
| public ErrorResponse handleValidationException(NotFoundException e) { | ||
| log.debug(e.getMessage(), e); | ||
| return new ErrorResponse(HttpStatus.BAD_REQUEST.value(), e.getMessage()); | ||
| } | ||
|
|
||
| @ExceptionHandler | ||
| @ResponseStatus(HttpStatus.CONFLICT) | ||
| public ErrorResponse handleValidationException(AlreadyExistException e) { | ||
| log.debug(e.getMessage(), e); | ||
| return new ErrorResponse(HttpStatus.BAD_REQUEST.value(), e.getMessage()); | ||
| } | ||
|
|
||
| @ExceptionHandler | ||
| @ResponseStatus(HttpStatus.FORBIDDEN) | ||
| public ErrorResponse handleValidationException(NotAccessException e) { | ||
| log.debug(e.getMessage(), e); | ||
| return new ErrorResponse(HttpStatus.FORBIDDEN.value(), e.getMessage()); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/java/ru/practicum/shareit/exception/ErrorResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package ru.practicum.shareit.exception; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| @AllArgsConstructor | ||
| public class ErrorResponse { | ||
| private Integer statusCode; | ||
| private String error; | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/ru/practicum/shareit/exception/NotAccessException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package ru.practicum.shareit.exception; | ||
|
|
||
| public class NotAccessException extends RuntimeException { | ||
| public NotAccessException(String message) { | ||
| super(message); | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/ru/practicum/shareit/exception/NotFoundException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package ru.practicum.shareit.exception; | ||
|
|
||
| public class NotFoundException extends RuntimeException { | ||
| public NotFoundException(String message) { | ||
| super(message); | ||
| } | ||
| } |
45 changes: 40 additions & 5 deletions
45
src/main/java/ru/practicum/shareit/item/ItemController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,47 @@ | ||
| package ru.practicum.shareit.item; | ||
|
|
||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import jakarta.validation.Valid; | ||
| import lombok.AllArgsConstructor; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import ru.practicum.shareit.item.dto.ItemDto; | ||
| import ru.practicum.shareit.item.service.ItemService; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * TODO Sprint add-controllers. | ||
| */ | ||
| @RestController | ||
| @AllArgsConstructor | ||
| @RequestMapping("/items") | ||
| public class ItemController { | ||
|
|
||
| private final ItemService itemService; | ||
|
|
||
| @GetMapping | ||
| public List<ItemDto> getItemsByUserId(@RequestHeader("X-Sharer-User-Id") Long userId) { | ||
| return itemService.getItemsByUserId(userId); | ||
| } | ||
|
|
||
| @GetMapping("/{itemId}") | ||
| public ItemDto getItemById(@PathVariable Long itemId) { | ||
| return itemService.getItemById(itemId); | ||
| } | ||
|
|
||
| @PostMapping | ||
| public ItemDto createItem( | ||
| @Valid @RequestBody ItemDto itemDto, | ||
| @RequestHeader("X-Sharer-User-Id") Long userId) { | ||
| return itemService.createItem(itemDto, userId); | ||
| } | ||
|
|
||
| @PatchMapping("/{itemId}") | ||
| public ItemDto updateItem( | ||
| @RequestBody ItemDto itemDto, | ||
| @PathVariable Long itemId, | ||
| @RequestHeader("X-Sharer-User-Id") Long userId) { | ||
| return itemService.updateItem(itemDto, itemId, userId); | ||
| } | ||
|
|
||
| @GetMapping("/search") | ||
| public List<ItemDto> getItemsSearchByText(@RequestParam String text) { | ||
| return itemService.searchItemsByText(text); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,19 @@ | ||
| package ru.practicum.shareit.item.dto; | ||
|
|
||
| /** | ||
| * TODO Sprint add-controllers. | ||
| */ | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| @AllArgsConstructor | ||
| public class ItemDto { | ||
| private Long id; | ||
| @NotBlank(message = "Название не может быть пустым") | ||
| private String name; | ||
| @NotBlank(message = "Описание не может быть пустым") | ||
| private String description; | ||
| @NotNull(message = "Статус о доступности не может быть пустым") | ||
| private Boolean available; | ||
| private Long requestId; | ||
| } |
31 changes: 31 additions & 0 deletions
31
src/main/java/ru/practicum/shareit/item/mapper/ItemMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package ru.practicum.shareit.item.mapper; | ||
|
|
||
| import ru.practicum.shareit.item.dto.ItemDto; | ||
| import ru.practicum.shareit.item.model.Item; | ||
| import ru.practicum.shareit.request.ItemRequest; | ||
| import ru.practicum.shareit.user.model.User; | ||
|
|
||
| public class ItemMapper { | ||
|
|
||
| public static ItemDto toItemDto(Item item) { | ||
| return new ItemDto( | ||
| item.getId(), | ||
| item.getName(), | ||
| item.getDescription(), | ||
| item.getAvailable(), | ||
| item.getRequest() != null ? item.getRequest().getId() : null | ||
| ); | ||
| } | ||
|
|
||
| public static Item toItem(Long itemId, ItemDto itemDto, User owner, ItemRequest request) { | ||
| return new Item( | ||
| itemId, | ||
| itemDto.getName(), | ||
| itemDto.getDescription(), | ||
| itemDto.getAvailable(), | ||
| owner, | ||
| request | ||
| ); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,18 @@ | ||
| package ru.practicum.shareit.item.model; | ||
|
|
||
| /** | ||
| * TODO Sprint add-controllers. | ||
| */ | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import ru.practicum.shareit.request.ItemRequest; | ||
| import ru.practicum.shareit.user.model.User; | ||
|
|
||
| @Data | ||
| @AllArgsConstructor | ||
| public class Item { | ||
| private Long id; | ||
| private String name; | ||
| private String description; | ||
| private Boolean available; | ||
| private User owner; | ||
| private ItemRequest request; | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/java/ru/practicum/shareit/item/repository/ItemRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package ru.practicum.shareit.item.repository; | ||
|
|
||
| import ru.practicum.shareit.item.model.Item; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| public interface ItemRepository { | ||
|
|
||
| List<Item> getItems(); | ||
|
|
||
| List<Item> getItemsByUserId(Long userId); | ||
|
|
||
| Item createItem(Item item); | ||
|
|
||
| Item updateItem(Item item); | ||
|
|
||
| Optional<Item> getItemById(Long itemId); | ||
|
|
||
| List<Item> searchItemsByText(String text); | ||
|
|
||
| Long getNewItemId(); | ||
| } |
61 changes: 61 additions & 0 deletions
61
src/main/java/ru/practicum/shareit/item/repository/ItemRepositoryImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package ru.practicum.shareit.item.repository; | ||
|
|
||
| import org.springframework.stereotype.Repository; | ||
| import ru.practicum.shareit.item.model.Item; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| @Repository | ||
| public class ItemRepositoryImpl implements ItemRepository { | ||
|
|
||
| private Map<Long, Item> items = new HashMap<>(); | ||
|
|
||
| @Override | ||
| public List<Item> getItems() { | ||
| return new ArrayList<>(items.values()); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Item> getItemsByUserId(Long userId) { | ||
| return items.values().stream() | ||
| .filter(item -> Objects.equals(item.getOwner().getId(), userId)) | ||
| .toList(); | ||
| } | ||
|
|
||
| @Override | ||
| public Item createItem(Item item) { | ||
| items.put(item.getId(), item); | ||
| return items.get(item.getId()); | ||
| } | ||
|
|
||
| @Override | ||
| public Item updateItem(Item item) { | ||
| items.put(item.getId(), item); | ||
| return items.get(item.getId()); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<Item> getItemById(Long itemId) { | ||
| return Optional.ofNullable(items.get(itemId)); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Item> searchItemsByText(String text) { | ||
| return items.values().stream() | ||
| .filter(item -> (containsIgnoreCase(item.getName(), text) || | ||
| containsIgnoreCase(item.getDescription(), text)) && item.getAvailable()) | ||
| .toList(); | ||
| } | ||
|
|
||
| private Boolean containsIgnoreCase(String text, String containsText) { | ||
| return text.toLowerCase().contains(containsText.toLowerCase()); | ||
| } | ||
|
|
||
| @Override | ||
| public Long getNewItemId() { | ||
|
||
| return getItems().stream() | ||
| .map(Item::getId) | ||
| .max(Long::compareTo) | ||
| .orElse(0L) + 1; | ||
| } | ||
| } | ||
17 changes: 17 additions & 0 deletions
17
src/main/java/ru/practicum/shareit/item/service/ItemService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package ru.practicum.shareit.item.service; | ||
|
|
||
| import ru.practicum.shareit.item.dto.ItemDto; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface ItemService { | ||
| List<ItemDto> getItemsByUserId(Long userId); | ||
|
|
||
| ItemDto createItem(ItemDto itemDto, Long userId); | ||
|
|
||
| ItemDto updateItem(ItemDto itemDto, Long itemId, Long userId); | ||
|
|
||
| ItemDto getItemById(Long itemId); | ||
|
|
||
| List<ItemDto> searchItemsByText(String text); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
добавь final