-
Notifications
You must be signed in to change notification settings - Fork 0
Add controllers #2
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
8 commits
Select commit
Hold shift + click to select a range
79339d9
Написана первая реализация функционала фз
dendzim 406ed76
Исправлены ошибки в логике
dendzim 77b9f91
Исправлены ошибки в логике
dendzim c27e7fd
Исправлен checkstyle
dendzim 66f6dae
Исправлен checkstyle
dendzim 6bbe53e
Исправлен checkstyle
dendzim ca53f6e
Исправлен checkstyle
dendzim e78c4f1
Внесены правки согласно замечаниям
dendzim 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 was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...um/shareit/booking/BookingController.java → ...booking/controller/BookingController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/ru/practicum/shareit/booking/dao/BookingMapper.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.booking.dao; | ||
|
|
||
| import lombok.experimental.UtilityClass; | ||
| import ru.practicum.shareit.booking.dto.BookingDto; | ||
| import ru.practicum.shareit.booking.model.Booking; | ||
|
|
||
| @UtilityClass | ||
| public class BookingMapper { | ||
| public static BookingDto mapToBooking(Booking booking) { | ||
| return new BookingDto( | ||
| booking.getStart(), | ||
| booking.getEnd(), | ||
| booking.getItem(), | ||
| booking.getBooker(), | ||
| booking.getStatus() | ||
| ); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/ru/practicum/shareit/booking/dto/BookingDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,20 @@ | ||
| package ru.practicum.shareit.booking.dto; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import ru.practicum.shareit.item.model.Item; | ||
| import ru.practicum.shareit.booking.model.BookingStatus; | ||
| import ru.practicum.shareit.user.model.User; | ||
|
|
||
| import java.time.LocalDate; | ||
|
|
||
| /** | ||
| * TODO Sprint add-bookings. | ||
| */ | ||
| @AllArgsConstructor | ||
| public class BookingDto { | ||
| private LocalDate start; | ||
| private LocalDate end; | ||
| private Item item; | ||
| private User booker; | ||
| private BookingStatus status; | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/main/java/ru/practicum/shareit/booking/model/Booking.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,22 @@ | ||
| package ru.practicum.shareit.booking.model; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import ru.practicum.shareit.item.model.Item; | ||
| import ru.practicum.shareit.user.model.User; | ||
|
|
||
| import java.time.LocalDate; | ||
|
|
||
| /** | ||
| * TODO Sprint add-bookings. | ||
| */ | ||
| @Data | ||
| @AllArgsConstructor | ||
| public class Booking { | ||
| private Long id; | ||
| private LocalDate start; | ||
| private LocalDate end; | ||
| private Item item; | ||
| private User booker; | ||
| private BookingStatus status; | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/main/java/ru/practicum/shareit/booking/model/BookingStatus.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package ru.practicum.shareit.booking.model; | ||
|
|
||
| public enum BookingStatus { | ||
| WAITING, | ||
| APPROVED, | ||
| REJECTED, | ||
| CANCELED | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/ru/practicum/shareit/exceptions/BadRequestException.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.exceptions; | ||
|
|
||
| public class BadRequestException extends RuntimeException { | ||
| public BadRequestException(String message) { | ||
| super(message); | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
src/main/java/ru/practicum/shareit/exceptions/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,34 @@ | ||
| package ru.practicum.shareit.exceptions; | ||
|
|
||
| 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 handleValidationExceptions(MethodArgumentNotValidException e) { | ||
| log.warn("Ошибка запроса"); | ||
| return new ErrorResponse(e.getMessage()); | ||
| } | ||
|
|
||
| @ExceptionHandler | ||
| @ResponseStatus(HttpStatus.NOT_FOUND) | ||
| public ErrorResponse handleNotFoundException(final NotFoundException e) { | ||
| log.warn("Объект не найден"); | ||
| return new ErrorResponse(e.getMessage()); | ||
| } | ||
|
|
||
| @ExceptionHandler | ||
| @ResponseStatus(HttpStatus.NOT_FOUND) | ||
| public ErrorResponse handleInternalServerException(final InternalServerException e) { | ||
| log.warn("Ошибка сервера"); | ||
| return new ErrorResponse(e.getMessage()); | ||
| } | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
src/main/java/ru/practicum/shareit/exceptions/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,13 @@ | ||
| package ru.practicum.shareit.exceptions; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class ErrorResponse { | ||
| private final String error; | ||
|
|
||
| public ErrorResponse(String error) { | ||
| this.error = error; | ||
| } | ||
|
|
||
| } |
6 changes: 6 additions & 0 deletions
6
src/main/java/ru/practicum/shareit/exceptions/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,6 @@ | ||
| package ru.practicum.shareit.exceptions; | ||
|
|
||
| public class InternalServerException extends RuntimeException { | ||
| public InternalServerException(String s) { | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
src/main/java/ru/practicum/shareit/exceptions/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,6 @@ | ||
| package ru.practicum.shareit.exceptions; | ||
|
|
||
| public class NotFoundException extends RuntimeException { | ||
| public NotFoundException(String s) { | ||
| } | ||
| } |
12 changes: 0 additions & 12 deletions
12
src/main/java/ru/practicum/shareit/item/ItemController.java
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
src/main/java/ru/practicum/shareit/item/controller/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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package ru.practicum.shareit.item.controller; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.validation.annotation.Validated; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import ru.practicum.shareit.validation.OnCreate; | ||
| import ru.practicum.shareit.validation.OnUpdate; | ||
| import ru.practicum.shareit.item.dto.ItemDto; | ||
| import ru.practicum.shareit.item.service.ItemService; | ||
|
|
||
| import java.util.Collection; | ||
|
|
||
| /** | ||
| * TODO Sprint add-controllers. | ||
| */ | ||
| @Slf4j | ||
| @RestController | ||
| @RequestMapping("/items") | ||
| @RequiredArgsConstructor | ||
| public class ItemController { | ||
|
|
||
| private final ItemService itemService; | ||
|
|
||
| @PostMapping | ||
| public ItemDto addItem(@Validated(OnCreate.class) @RequestBody final ItemDto itemDto, | ||
| @RequestHeader("X-Sharer-User-Id") Long ownerId) { | ||
| log.info("Предмет добавлен"); | ||
| return itemService.addItem(itemDto, ownerId); | ||
| } | ||
|
|
||
| @PatchMapping("/{itemId}") | ||
| public ItemDto updateItem(@PathVariable("itemId") final long itemId, | ||
| @RequestHeader("X-Sharer-User-Id") long ownerId, | ||
| @Validated(OnUpdate.class) @RequestBody final ItemDto itemDto) { | ||
| itemDto.setId(itemId); | ||
| log.info("Предмет обновлен"); | ||
| return itemService.updateItem(itemDto, ownerId); | ||
| } | ||
|
|
||
| @GetMapping("/{itemId}") | ||
| public ItemDto getItem(@PathVariable("itemId") final long itemId) { | ||
| log.info("Предмет выведен"); | ||
| return itemService.getItem(itemId); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public Collection<ItemDto> getAllOwnerItems(@RequestHeader("X-Sharer-User-Id") long ownerId) { | ||
| log.info("Список всех предметов владельца выведен"); | ||
| return itemService.getAllOwnerItems(ownerId); | ||
| } | ||
|
|
||
| @GetMapping("/search") | ||
| public Collection<ItemDto> getNecessaryItem(@RequestParam String text) { | ||
| log.info("Список предметов содержащих " + text + " выведен"); | ||
| return itemService.getNecessaryItem(text); | ||
| } | ||
| } |
79 changes: 79 additions & 0 deletions
79
src/main/java/ru/practicum/shareit/item/dao/InMemoryItemStorage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package ru.practicum.shareit.item.dao; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Repository; | ||
| import ru.practicum.shareit.exceptions.NotFoundException; | ||
| import ru.practicum.shareit.item.model.Item; | ||
|
|
||
| import java.util.*; | ||
|
|
||
|
|
||
| @Slf4j | ||
| @Repository | ||
| @RequiredArgsConstructor | ||
| public class InMemoryItemStorage { | ||
| private final Map<Long, Item> items = new HashMap<>(); | ||
|
|
||
| private Long getNextId() { | ||
| long currentMaxId = items.keySet() | ||
| .stream() | ||
| .mapToLong(id -> id) | ||
| .max() | ||
| .orElse(0); | ||
| return ++currentMaxId; | ||
| } | ||
|
|
||
| public Item addItem(Item item) { | ||
| item.setId(getNextId()); | ||
| items.put(item.getId(), item); | ||
| return item; | ||
| } | ||
|
|
||
| public Item updateItem(Item newItem) { | ||
| Item item = items.get(newItem.getId()); | ||
| if (newItem.getName() != null) { | ||
| item.setName(newItem.getName()); | ||
| } | ||
| if (newItem.getDescription() != null) { | ||
| item.setDescription(newItem.getDescription()); | ||
| } | ||
| if (newItem.getAvailable() != null) { | ||
| item.setAvailable(newItem.getAvailable()); | ||
| } | ||
| return item; | ||
| } | ||
|
|
||
| public Item getItem(long itemId) { | ||
| if (!items.containsKey(itemId)) { | ||
| log.warn("Предмет с указанным id " + itemId + " не найден"); | ||
| throw new NotFoundException("Предмет с id = " + itemId + " не найден"); | ||
| } | ||
| return items.get(itemId); | ||
| } | ||
|
|
||
| public List<Item> getAllOwnerItems(long ownerId) { | ||
| List<Item> ownerItems = new ArrayList<>(); | ||
| for (Item item : items.values()) { | ||
| if (item.getOwnerId() == ownerId) { | ||
| ownerItems.add(item); | ||
| } | ||
| } | ||
| return ownerItems; | ||
| } | ||
|
|
||
| public List<Item> getNecessaryItem(String text) { | ||
| List<Item> necessaryItems = new ArrayList<>(); | ||
| if (text == null || text.isBlank()) { | ||
| return Collections.emptyList(); | ||
| } | ||
| text = text.toLowerCase(Locale.ROOT); | ||
| for (Item item : items.values()) { | ||
| if (item.getAvailable() && item.getName().toLowerCase(Locale.ROOT).contains(text) | ||
| || item.getDescription().toLowerCase(Locale.ROOT).contains(text)) { | ||
| necessaryItems.add(item); | ||
| } | ||
| } | ||
| return necessaryItems; | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
src/main/java/ru/practicum/shareit/item/dao/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,26 @@ | ||
| package ru.practicum.shareit.item.dao; | ||
|
|
||
| import lombok.experimental.UtilityClass; | ||
| import ru.practicum.shareit.item.model.Item; | ||
| import ru.practicum.shareit.item.dto.ItemDto; | ||
|
|
||
| @UtilityClass | ||
| public class ItemMapper { | ||
| public static ItemDto toItemDto(Item item) { | ||
| return new ItemDto( | ||
| item.getId(), | ||
| item.getName(), | ||
| item.getDescription(), | ||
| item.getAvailable() | ||
| ); | ||
| } | ||
|
|
||
| public static Item toItem(ItemDto itemDto) { | ||
| Item item = new Item(); | ||
| item.setId(itemDto.getId()); | ||
| item.setName(itemDto.getName()); | ||
| item.setDescription(itemDto.getDescription()); | ||
| 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,22 @@ | ||
| package ru.practicum.shareit.item.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import ru.practicum.shareit.validation.OnCreate; | ||
|
|
||
| /** | ||
| * TODO Sprint add-controllers. | ||
| */ | ||
| @Data | ||
| @AllArgsConstructor | ||
| public class ItemDto { | ||
| private Long id; | ||
| @NotBlank(groups = OnCreate.class, message = "Название предмета должно быть заполнено") | ||
| private String name; | ||
| @NotBlank(groups = OnCreate.class, message = "Описание предмета должно быть заполнено") | ||
| private String description; | ||
| @NotNull(groups = OnCreate.class, message = "Статус должен быть заполнен") | ||
| private Boolean available; | ||
| } |
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.model; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| /** | ||
| * TODO Sprint add-controllers. | ||
| */ | ||
| @Data | ||
| @AllArgsConstructor | ||
| @NoArgsConstructor | ||
| public class Item { | ||
| private Long id; | ||
| private String name; | ||
| private String description; | ||
| private Boolean available; | ||
| private Long ownerId; | ||
| } |
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.