generated from yandex-praktikum/java-shareit
-
Notifications
You must be signed in to change notification settings - Fork 0
Добавлена базовая логика: Item, User, сервисы и DTO #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 all commits
Commits
Show all changes
2 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
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,22 @@ | ||
| package ru.practicum.shareit.booking; | ||
|
|
||
| /** | ||
| * TODO Sprint add-bookings. | ||
| */ | ||
| import lombok.Data; | ||
| import java.time.LocalDateTime; | ||
|
|
||
| @Data | ||
| public class Booking { | ||
| } | ||
|
|
||
| public enum Status { | ||
| WAITING, | ||
| APPROVED, | ||
| REJECTED, | ||
| CANCELED | ||
| } | ||
|
|
||
| private Long id; | ||
| private LocalDateTime start; | ||
| private LocalDateTime end; | ||
| private Long itemId; | ||
| private Long bookerId; | ||
| private Status status; | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/ru/practicum/shareit/exception/ConflictException.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 ConflictException extends RuntimeException { | ||
| public ConflictException(String message) { | ||
| super(message); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/java/ru/practicum/shareit/exception/InternalServerException.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 org.springframework.http.HttpStatus; | ||
| import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
|
||
| @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
| public class InternalServerException extends RuntimeException { | ||
| public InternalServerException(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); | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/ru/practicum/shareit/exception/ValidationException.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 ValidationException extends RuntimeException { | ||
| public ValidationException(String message) { | ||
| super(message); | ||
| } | ||
| } |
87 changes: 87 additions & 0 deletions
87
src/main/java/ru/practicum/shareit/exception/handler/GlobalExceptionHandler.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,87 @@ | ||
| package ru.practicum.shareit.exception.handler; | ||
|
|
||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.MethodArgumentNotValidException; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
| import ru.practicum.shareit.exception.ConflictException; | ||
| import ru.practicum.shareit.exception.NotFoundException; | ||
| import ru.practicum.shareit.exception.ValidationException; | ||
| import ru.practicum.shareit.response.ErrorResponse; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @RestControllerAdvice | ||
| public class GlobalExceptionHandler { | ||
|
|
||
| @ExceptionHandler(NotFoundException.class) | ||
| public ResponseEntity<ErrorResponse> handleNotFound(NotFoundException e, HttpServletRequest request) { | ||
| ErrorResponse errorResponse = new ErrorResponse( | ||
| LocalDateTime.now(), | ||
| HttpStatus.NOT_FOUND.value(), | ||
| "Not Found", | ||
| e.getMessage(), | ||
| request.getRequestURI() | ||
| ); | ||
| return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); | ||
| } | ||
|
|
||
| @ExceptionHandler({ValidationException.class}) | ||
| public ResponseEntity<ErrorResponse> handleValidation( | ||
| ValidationException e, HttpServletRequest request) { | ||
| ErrorResponse errorResponse = new ErrorResponse( | ||
| LocalDateTime.now(), | ||
| HttpStatus.BAD_REQUEST.value(), | ||
| "Bad Request", | ||
| e.getMessage(), | ||
| request.getRequestURI() | ||
| ); | ||
| return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse); | ||
| } | ||
|
|
||
| @ExceptionHandler({ConflictException.class}) | ||
| public ResponseEntity<ErrorResponse> handleConflict( | ||
| ConflictException e, HttpServletRequest request) { | ||
| ErrorResponse errorResponse = new ErrorResponse( | ||
| LocalDateTime.now(), | ||
| HttpStatus.CONFLICT.value(), | ||
| "Conflict", | ||
| e.getMessage(), | ||
| request.getRequestURI() | ||
| ); | ||
| return ResponseEntity.status(HttpStatus.CONFLICT).body(errorResponse); | ||
| } | ||
|
|
||
| @ExceptionHandler(Exception.class) | ||
| public ResponseEntity<ErrorResponse> handleOtherExceptions(Exception e, HttpServletRequest request) { | ||
| ErrorResponse errorResponse = new ErrorResponse( | ||
| LocalDateTime.now(), | ||
| HttpStatus.INTERNAL_SERVER_ERROR.value(), | ||
| "Internal Server Error", | ||
| "Произошла ошибка: " + e.getMessage(), | ||
| request.getRequestURI() | ||
| ); | ||
| return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse); | ||
| } | ||
|
|
||
| @ExceptionHandler(MethodArgumentNotValidException.class) | ||
| public ResponseEntity<ErrorResponse> handleMethodArgumentNotValid(MethodArgumentNotValidException e, | ||
| HttpServletRequest request) { | ||
| String message = e.getBindingResult().getFieldErrors().stream() | ||
| .map(error -> error.getField() + ": " + error.getDefaultMessage()) | ||
| .findFirst() | ||
| .orElse("Ошибка валидации"); | ||
|
|
||
| ErrorResponse errorResponse = new ErrorResponse( | ||
| LocalDateTime.now(), | ||
| HttpStatus.BAD_REQUEST.value(), | ||
| "Bad Request", | ||
| message, | ||
| request.getRequestURI() | ||
| ); | ||
|
|
||
| return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse); | ||
| } | ||
| } | ||
52 changes: 46 additions & 6 deletions
52
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,52 @@ | ||
| 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.RequiredArgsConstructor; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import ru.practicum.shareit.item.dto.ItemDto; | ||
| import ru.practicum.shareit.item.mapper.ItemMapper; | ||
| import ru.practicum.shareit.item.service.ItemService; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * TODO Sprint add-controllers. | ||
| */ | ||
| @RestController | ||
| @RequestMapping("/items") | ||
| @RequiredArgsConstructor | ||
| public class ItemController { | ||
| } | ||
|
|
||
| private final ItemService itemService; | ||
|
|
||
| private static final String USER_HEADER = "X-Sharer-User-Id"; | ||
|
|
||
| @PostMapping | ||
| public ItemDto create(@RequestHeader(USER_HEADER) Long userId, | ||
| @Valid @RequestBody ItemDto itemDto) { | ||
| return ItemMapper.toItemDto(itemService.create(userId, itemDto)); | ||
| } | ||
|
|
||
| @PatchMapping("/{itemId}") | ||
| public ItemDto update(@RequestHeader(USER_HEADER) Long userId, | ||
| @PathVariable Long itemId, | ||
| @RequestBody ItemDto dto) { | ||
| return ItemMapper.toItemDto(itemService.update(userId, itemId, dto)); | ||
| } | ||
|
|
||
| @GetMapping("/{itemId}") | ||
| public ItemDto getById(@PathVariable Long itemId) { | ||
| return ItemMapper.toItemDto(itemService.getItemById(itemId)); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public List<ItemDto> getAllByOwner(@RequestHeader(USER_HEADER) Long userId) { | ||
| return itemService.getAllByOwner(userId).stream() | ||
| .map(ItemMapper::toItemDto) | ||
| .toList(); | ||
| } | ||
|
|
||
| @GetMapping("/search") | ||
| public List<ItemDto> search(@RequestParam String text) { | ||
| return itemService.search(text).stream() | ||
| .map(ItemMapper::toItemDto) | ||
| .toList(); | ||
| } | ||
| } |
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,21 @@ | ||
| package ru.practicum.shareit.item.dto; | ||
|
|
||
| /** | ||
| * TODO Sprint add-controllers. | ||
| */ | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| @Builder | ||
| public class ItemDto { | ||
| } | ||
| private Long id; | ||
|
|
||
| @NotNull | ||
| @NotBlank | ||
| private String name; | ||
| @NotNull | ||
| private String description; | ||
| @NotNull | ||
| private Boolean available; | ||
| private Long requestId; | ||
| } |
42 changes: 42 additions & 0 deletions
42
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,42 @@ | ||
| 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 ItemDto.builder() | ||
| .id(item.getId()) | ||
| .name(item.getName()) | ||
| .description(item.getDescription()) | ||
| .available(item.getAvailable()) | ||
| .requestId(item.getRequest() != null ? item.getRequest().getId() : null) | ||
| .build(); | ||
| } | ||
|
|
||
| public static Item toItem(ItemDto itemDto, User owner, ItemRequest itemRequest) { | ||
| return Item.builder() | ||
| .name(itemDto.getName()) | ||
| .description(itemDto.getDescription()) | ||
| .available(itemDto.getAvailable()) | ||
| .ownerId(owner.getId()) | ||
| .request(itemRequest) | ||
| .build(); | ||
| } | ||
|
|
||
| public static Item updateItemFields(Item item, ItemDto itemDto) { | ||
| if (itemDto.getDescription() != null) { | ||
| item.setDescription(itemDto.getDescription()); | ||
| } | ||
| if (itemDto.getName() != null) { | ||
| item.setName(itemDto.getName()); | ||
| } | ||
| if (itemDto.getAvailable() != null) { | ||
| item.setAvailable(itemDto.getAvailable()); | ||
| } | ||
| return item; | ||
| } | ||
| } |
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,16 @@ | ||
| package ru.practicum.shareit.item.model; | ||
|
|
||
| /** | ||
| * TODO Sprint add-controllers. | ||
| */ | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
| import ru.practicum.shareit.request.ItemRequest; | ||
|
|
||
| @Data | ||
| @Builder | ||
| public class Item { | ||
| } | ||
| private Long id; | ||
| private String name; | ||
| private String description; | ||
| private Boolean available; | ||
| private Long ownerId; | ||
| private ItemRequest request; | ||
| } |
18 changes: 18 additions & 0 deletions
18
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,18 @@ | ||
| package ru.practicum.shareit.item.service; | ||
|
|
||
| import ru.practicum.shareit.item.dto.ItemDto; | ||
| import ru.practicum.shareit.item.model.Item; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface ItemService { | ||
| Item create(Long userId, ItemDto itemDto); | ||
|
|
||
| Item update(Long userId, Long itemId, ItemDto itemDto); | ||
|
|
||
| Item getItemById(Long itemId); | ||
|
|
||
| List<Item> getAllByOwner(Long userId); | ||
|
|
||
| List<Item> search(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.
Uh oh!
There was an error while loading. Please reload this page.