-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathItemServiceImpl.java
More file actions
88 lines (74 loc) · 3.11 KB
/
Copy pathItemServiceImpl.java
File metadata and controls
88 lines (74 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package ru.practicum.shareit.item.service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
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.user.repository.UserRepository;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
@RequiredArgsConstructor
public class ItemServiceImpl implements ItemService {
private final ItemRepository itemRepository;
private final UserRepository userRepository;
@Override
public List<ItemDto> findAllByUser(Long userId) {
log.info("Получение всех вещей пользователя {}", userId);
return itemRepository.findAllByOwnerId(userId).stream()
.map(ItemMapper::toItemDto)
.collect(Collectors.toList());
}
@Override
public ItemDto findById(Long id) {
log.info("Получение вещи с id {}", id);
Item item = itemRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Вещь не найдена"));
return ItemMapper.toItemDto(item);
}
@Override
public ItemDto create(Long userId, ItemDto itemDto) {
log.info("Создание вещи для пользователя {}", userId);
userRepository.findById(userId)
.orElseThrow(() -> new RuntimeException("Пользователь не найден"));
Item item = ItemMapper.toItem(itemDto);
item.setOwnerId(userId);
Item saved = itemRepository.save(item);
return ItemMapper.toItemDto(saved);
}
@Override
public ItemDto update(Long userId, Long itemId, ItemDto itemDto) {
log.info("Обновление вещи {} для пользователя {}", itemId, userId);
Item existing = itemRepository.findById(itemId)
.orElseThrow(() -> new RuntimeException("Вещь не найдена"));
if (!userId.equals(existing.getOwnerId())) {
throw new RuntimeException("Только владелец может редактировать вещь");
}
if (itemDto.getName() != null) {
existing.setName(itemDto.getName());
}
if (itemDto.getDescription() != null) {
existing.setDescription(itemDto.getDescription());
}
if (itemDto.getAvailable() != null) {
existing.setAvailable(itemDto.getAvailable());
}
Item updated = itemRepository.update(existing);
return ItemMapper.toItemDto(updated);
}
@Override
public void delete(Long id) {
log.info("Удаление вещи {}", id);
itemRepository.delete(id);
}
@Override
public List<ItemDto> search(String text) {
log.info("Поиск вещей по тексту: {}", text);
return itemRepository.search(text).stream()
.map(ItemMapper::toItemDto)
.collect(Collectors.toList());
}
}