|
| 1 | +package ru.practicum.shareit.item.service; |
| 2 | + |
| 3 | +import lombok.RequiredArgsConstructor; |
| 4 | +import org.springframework.stereotype.Service; |
| 5 | +import ru.practicum.shareit.item.ItemMapper; |
| 6 | +import ru.practicum.shareit.item.dao.ItemRepository; |
| 7 | +import ru.practicum.shareit.item.dto.ItemDto; |
| 8 | +import ru.practicum.shareit.item.model.Item; |
| 9 | +import ru.practicum.shareit.user.dao.UserRepository; |
| 10 | +import ru.practicum.shareit.validation.NotFoundException; |
| 11 | + |
| 12 | +import java.util.Collection; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.Locale; |
| 15 | +import java.util.Optional; |
| 16 | + |
| 17 | +@Service |
| 18 | +@RequiredArgsConstructor |
| 19 | +public class ItemServiceImpl implements ItemService { |
| 20 | + |
| 21 | + private final ItemRepository itemRepository; |
| 22 | + private final UserRepository userRepository; |
| 23 | + |
| 24 | + @Override |
| 25 | + public ItemDto getItem(Long itemId) { |
| 26 | + Optional<Item> maybeItem = itemRepository.getItem(itemId); |
| 27 | + |
| 28 | + if (maybeItem.isPresent()) { |
| 29 | + return ItemMapper.toItemDto(maybeItem.get()); |
| 30 | + } |
| 31 | + |
| 32 | + throw new NotFoundException("Предмет с id " + itemId + " не найден"); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public Collection<ItemDto> searchItems(String text) { |
| 37 | + if (text == null || text.isBlank()) { |
| 38 | + return Collections.emptyList(); |
| 39 | + } |
| 40 | + |
| 41 | + text = text.toLowerCase(Locale.ROOT); |
| 42 | + |
| 43 | + return itemRepository.searchItems(text) |
| 44 | + .stream() |
| 45 | + .map(ItemMapper::toItemDto) |
| 46 | + .toList(); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Collection<ItemDto> getItems(Long userId) { |
| 51 | + throwIfUserNotFound(userId); |
| 52 | + |
| 53 | + Collection<Item> items = itemRepository.getItems(userId); |
| 54 | + |
| 55 | + return items.stream() |
| 56 | + .map(ItemMapper::toItemDto) |
| 57 | + .toList(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public ItemDto addItem(Long userId, ItemDto itemDto) { |
| 62 | + throwIfUserNotFound(userId); |
| 63 | + |
| 64 | + Item item = ItemMapper.toItem(itemDto); |
| 65 | + item.setOwnerId(userId); |
| 66 | + |
| 67 | + return ItemMapper.toItemDto(itemRepository.addItem(item)); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public ItemDto updateItem(Long userId, Long itemId, ItemDto itemDto) { |
| 72 | + throwIfUserNotFound(userId); |
| 73 | + |
| 74 | + Item item = ItemMapper.toItem(itemDto); |
| 75 | + item.setOwnerId(userId); |
| 76 | + item.setId(itemId); |
| 77 | + |
| 78 | + Optional<Item> maybeItem = itemRepository.updateItem(item); |
| 79 | + |
| 80 | + if (maybeItem.isPresent()) { |
| 81 | + return ItemMapper.toItemDto(maybeItem.get()); |
| 82 | + } |
| 83 | + |
| 84 | + throw new NotFoundException("Предмет с id " + itemId + " не найден"); |
| 85 | + } |
| 86 | + |
| 87 | + private void throwIfUserNotFound(Long userId) { |
| 88 | + if (!userRepository.contains(userId)) { |
| 89 | + throw new NotFoundException("Пользователь с id " + userId + " не найден"); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments