generated from yandex-praktikum/java-shareit
-
Notifications
You must be signed in to change notification settings - Fork 0
Создал сервис для шеринга ShareIt. #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
4 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 was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
src/main/java/ru/practicum/shareit/booking/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,11 @@ | ||
| package ru.practicum.shareit.booking; | ||
|
|
||
| public enum BookingStatus { | ||
| WAITING, | ||
|
|
||
| APPROVED, | ||
|
|
||
| REJECTED, | ||
|
|
||
| CANCELED | ||
| } |
8 changes: 3 additions & 5 deletions
8
...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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,10 @@ | ||
| package ru.practicum.shareit.booking; | ||
| package ru.practicum.shareit.booking.controller; | ||
|
|
||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| /** | ||
| * TODO Sprint add-bookings. | ||
| */ | ||
|
|
||
| @RestController | ||
| @RequestMapping(path = "/bookings") | ||
| public class BookingController { | ||
| } | ||
| } |
29 changes: 25 additions & 4 deletions
29
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,28 @@ | ||
| package ru.practicum.shareit.booking.dto; | ||
|
|
||
| /** | ||
| * TODO Sprint add-bookings. | ||
| */ | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| import ru.practicum.shareit.booking.BookingStatus; | ||
| import ru.practicum.shareit.item.dto.ItemDto; | ||
| import ru.practicum.shareit.user.dto.UserDto; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
|
|
||
| @Getter | ||
| @Setter | ||
| @Builder(toBuilder = true) | ||
| public class BookingDto { | ||
| } | ||
| private Long id; | ||
|
|
||
| private LocalDateTime start; | ||
|
|
||
| private LocalDateTime end; | ||
|
|
||
| private ItemDto item; | ||
|
|
||
| private UserDto booker; | ||
|
|
||
| private BookingStatus status; | ||
| } |
31 changes: 31 additions & 0 deletions
31
src/main/java/ru/practicum/shareit/booking/exception/ShareItException.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.booking.exception; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
|
||
| public class ShareItException extends RuntimeException { | ||
| private final HttpStatus status; | ||
|
|
||
| public ShareItException(String message, HttpStatus status) { | ||
| super(message); | ||
| this.status = status; | ||
| } | ||
|
|
||
| public HttpStatus getStatus() { | ||
| return status; | ||
| } | ||
|
|
||
| @ResponseStatus(HttpStatus.NOT_FOUND) | ||
| public static class NotFoundException extends ShareItException { | ||
| public NotFoundException(String message) { | ||
| super(message, HttpStatus.NOT_FOUND); | ||
| } | ||
| } | ||
|
|
||
| @ResponseStatus(HttpStatus.CONFLICT) | ||
| public static class ConflictException extends ShareItException { | ||
| public ConflictException(String message) { | ||
| super(message, HttpStatus.CONFLICT); | ||
| } | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
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,26 @@ | ||
| package ru.practicum.shareit.booking.model; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| import ru.practicum.shareit.booking.BookingStatus; | ||
| import ru.practicum.shareit.item.model.Item; | ||
| import ru.practicum.shareit.user.model.User; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
|
|
||
| @Getter | ||
| @Setter | ||
| public class Booking { | ||
| private Long id; | ||
|
|
||
| private LocalDateTime start; | ||
|
|
||
| private LocalDateTime end; | ||
|
|
||
| private Item item; | ||
|
|
||
| private User booker; | ||
|
|
||
| private BookingStatus status; | ||
| } |
34 changes: 34 additions & 0 deletions
34
src/main/java/ru/practicum/shareit/item/Impl/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,34 @@ | ||
| package ru.practicum.shareit.item.Impl; | ||
|
|
||
| import org.springframework.stereotype.Component; | ||
| import ru.practicum.shareit.item.model.Item; | ||
| import ru.practicum.shareit.item.repository.AbstractRepository; | ||
| import ru.practicum.shareit.item.repository.ItemRepository; | ||
|
|
||
| @Component | ||
| public class ItemRepositoryImpl extends AbstractRepository<Item, Long> implements ItemRepository { | ||
|
|
||
| @Override | ||
| public Item create(Item item) { | ||
| setEntityId(item, nextId); | ||
| nextId++; | ||
| entities.put(item.getId(), item); | ||
| return item; | ||
| } | ||
|
|
||
| @Override | ||
| public Item update(Item item) { | ||
| entities.put(item.getId(), item); | ||
| return item; | ||
| } | ||
|
|
||
| @Override | ||
| protected void setEntityId(Item entity, Long id) { | ||
| entity.setId(id); | ||
| } | ||
|
|
||
| @Override | ||
| protected Long getEntityId(Item entity) { | ||
| return entity.getId(); | ||
| } | ||
| } |
90 changes: 90 additions & 0 deletions
90
src/main/java/ru/practicum/shareit/item/Impl/ItemServiceImpl.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,90 @@ | ||
| package ru.practicum.shareit.item.Impl; | ||
|
|
||
| import org.springframework.stereotype.Service; | ||
| import ru.practicum.shareit.booking.exception.ShareItException; | ||
| import ru.practicum.shareit.item.ItemMapper; | ||
| import ru.practicum.shareit.item.dto.ItemDto; | ||
| import ru.practicum.shareit.item.model.Item; | ||
| import ru.practicum.shareit.item.repository.ItemRepository; | ||
| import ru.practicum.shareit.item.service.ItemService; | ||
| import ru.practicum.shareit.user.model.User; | ||
| import ru.practicum.shareit.user.repository.UserRepository; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Service | ||
| public class ItemServiceImpl implements ItemService { | ||
| private final ItemRepository itemRepository; | ||
| private final UserRepository userRepository; | ||
| private final ItemMapper itemMapper; | ||
|
|
||
| public ItemServiceImpl(ItemRepository itemRepository, UserRepository userRepository, ItemMapper itemMapper) { | ||
| this.itemRepository = itemRepository; | ||
| this.userRepository = userRepository; | ||
| this.itemMapper = itemMapper; | ||
| } | ||
|
|
||
| @Override | ||
| public List<ItemDto> getAll(Long userId) { | ||
| return itemRepository.findAll().stream() | ||
| .filter(item -> item.getOwner().getId().equals(userId)) | ||
| .map(itemMapper::toItemDto) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| @Override | ||
| public ItemDto getById(Long id) { | ||
| Item item = itemRepository.findById(id) | ||
| .orElseThrow(() -> new ShareItException.NotFoundException("Не найдена вещь с id: " + id)); | ||
|
|
||
| return itemMapper.toItemDto(item); | ||
| } | ||
|
|
||
| @Override | ||
| public ItemDto create(ItemDto itemDto, Long userId) { | ||
| User user = userRepository.findById(userId) | ||
| .orElseThrow(() -> new ShareItException.NotFoundException("Невозможно создать вещь - " + | ||
| "не найден пользователь с id: " + userId)); | ||
| Item item = itemMapper.toItem(itemDto); | ||
| item.setOwner(user); | ||
| itemRepository.create(item); | ||
|
|
||
| return itemMapper.toItemDto(item); | ||
| } | ||
|
|
||
| @Override | ||
| public ItemDto update(ItemDto itemDto, Long id, Long userId) { | ||
| Item item = itemRepository.findById(id) | ||
| .orElseThrow(() -> new ShareItException.NotFoundException("Не найдена вещь с id: " + id)); | ||
|
|
||
| if (!item.getOwner().getId().equals(userId)) { | ||
| throw new ShareItException.NotFoundException("Невозможно обновить вещь - у пользователя с id: " + userId + " нет такой вещи"); | ||
| } | ||
|
|
||
| Item updatedItem = itemMapper.updateItemFields(item, itemDto); | ||
| return itemMapper.toItemDto(itemRepository.update(updatedItem)); | ||
| } | ||
|
|
||
| @Override | ||
| public void delete(Long id) { | ||
| getById(id); | ||
| itemRepository.delete(id); | ||
| } | ||
|
|
||
| @Override | ||
| public List<ItemDto> search(String text) { | ||
| if (text.isBlank()) { | ||
| return new ArrayList<>(); | ||
| } | ||
|
|
||
| String searchText = text.toLowerCase(); | ||
| return itemRepository.findAll().stream() | ||
| .filter(item -> item.getAvailable() && | ||
| (item.getName().toLowerCase().contains(searchText) || | ||
| item.getDescription().toLowerCase().contains(searchText))) | ||
| .map(itemMapper::toItemDto) | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } | ||
12 changes: 0 additions & 12 deletions
12
src/main/java/ru/practicum/shareit/item/ItemController.java
This file was deleted.
Oops, something went wrong.
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,20 @@ | ||
| package ru.practicum.shareit.item; | ||
|
|
||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.Mapping; | ||
| import org.mapstruct.MappingTarget; | ||
| import org.mapstruct.NullValuePropertyMappingStrategy; | ||
| import ru.practicum.shareit.item.dto.ItemDto; | ||
| import ru.practicum.shareit.item.model.Item; | ||
|
|
||
|
|
||
| @Mapper(componentModel = "spring", nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE) | ||
| public interface ItemMapper { | ||
|
|
||
| @Mapping(target = "request", expression = "java(item.getRequest() != null ? item.getRequest() : null)") | ||
| ItemDto toItemDto(Item item); | ||
|
|
||
| Item toItem(ItemDto itemDto); | ||
|
|
||
| Item updateItemFields(@MappingTarget Item targetItem, ItemDto sourceItemDto); | ||
| } |
59 changes: 59 additions & 0 deletions
59
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,59 @@ | ||
| package ru.practicum.shareit.item.controller; | ||
|
|
||
| import jakarta.validation.Valid; | ||
| import org.springframework.web.bind.annotation.DeleteMapping; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PatchMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestHeader; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import ru.practicum.shareit.item.dto.ItemDto; | ||
| import ru.practicum.shareit.item.service.ItemService; | ||
| import ru.practicum.shareit.item.util.Constants; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/items") | ||
| public class ItemController { | ||
| private final ItemService itemService; | ||
|
|
||
| public ItemController(ItemService itemService) { | ||
| this.itemService = itemService; | ||
| } | ||
|
|
||
| @GetMapping | ||
| public List<ItemDto> getAll(@RequestHeader(Constants.USER_ID_HEADER) Long userId) { | ||
| return itemService.getAll(userId); | ||
| } | ||
|
|
||
| @GetMapping("/{id}") | ||
| public ItemDto getById(@PathVariable Long id) { | ||
| return itemService.getById(id); | ||
| } | ||
|
|
||
| @PostMapping | ||
| public ItemDto create(@RequestHeader(Constants.USER_ID_HEADER) Long userId, @Valid @RequestBody ItemDto itemDto) { | ||
| return itemService.create(itemDto, userId); | ||
| } | ||
|
|
||
| @PatchMapping("/{id}") | ||
| public ItemDto update(@RequestBody ItemDto itemDto, @PathVariable Long id, | ||
| @RequestHeader(Constants.USER_ID_HEADER) Long userId) { | ||
| return itemService.update(itemDto, id, userId); | ||
| } | ||
|
|
||
| @DeleteMapping("/{id}") | ||
| public void delete(@PathVariable Long id) { | ||
| itemService.delete(id); | ||
| } | ||
|
|
||
| @GetMapping("/search") | ||
| public List<ItemDto> search(@RequestParam String text) { | ||
| return itemService.search(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.
Обновление полей лучше вынести в метод в маппере, так как он занимается перекладыванием полей из одного объекта в другой
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.
Вынес метод Обновление полей в маппер, после внедрения MapStruct, генерируется автоматически.