forked from yandex-praktikum/java-shareit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemServiceImpl.java
More file actions
97 lines (79 loc) · 3.28 KB
/
ItemServiceImpl.java
File metadata and controls
97 lines (79 loc) · 3.28 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
89
90
91
92
93
94
95
96
97
package ru.practicum.shareit.item;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import ru.practicum.shareit.exception.NotFoundException;
import ru.practicum.shareit.item.dto.ItemDto;
import ru.practicum.shareit.item.model.Item;
import ru.practicum.shareit.user.User;
import ru.practicum.shareit.user.UserService;
import ru.practicum.shareit.user.UserMapper;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
@Service
@RequiredArgsConstructor
public class ItemServiceImpl implements ItemService {
private final Map<Long, Item> repository = new HashMap<>();
private final UserService userService;
private Long lastGeneratedId = 1L;
@Override
public List<ItemDto> search(String searchStr) {
if (searchStr == null || searchStr.isBlank()) {
return Collections.emptyList();
}
String lowerCaseQuery = searchStr.toLowerCase();
return repository.values().stream()
.filter(Item::getAvailable)
.filter(item -> item.getName().toLowerCase().contains(lowerCaseQuery)
|| item.getDescription().toLowerCase().contains(lowerCaseQuery))
.map(ItemMapper::toItemDto)
.collect(Collectors.toList());
}
@Override
public ItemDto getById(Long id) {
Item found = repository.get(id);
if (found == null) {
throw new NotFoundException("Объект с идентификатором " + id + " не существует");
}
return ItemMapper.toItemDto(found);
}
@Override
public List<ItemDto> getByOwner(Long ownerId) {
userService.getById(ownerId);
return repository.values().stream()
.filter(obj -> obj.getOwner().getId().equals(ownerId))
.map(ItemMapper::toItemDto)
.collect(Collectors.toList());
}
@Override
public ItemDto update(Long userId, Long itemId, ItemDto dto) {
Item target = repository.get(itemId);
if (Objects.isNull(target)) {
throw new NotFoundException("Вещь с id " + itemId + " не найдена");
}
if (!Objects.equals(target.getOwner().getId(), userId)) {
throw new NotFoundException("Доступ запрещен: редактировать может только владелец");
}
if (dto.getName() != null && !dto.getName().trim().isEmpty()) {
target.setName(dto.getName());
}
if (dto.getDescription() != null && !dto.getDescription().trim().isEmpty()) {
target.setDescription(dto.getDescription());
}
if (dto.getAvailable() != null) {
target.setAvailable(dto.getAvailable());
}
return ItemMapper.toItemDto(target);
}
@Override
public ItemDto create(Long userId, ItemDto itemDto) {
User creator = UserMapper.toUser(userService.getById(userId));
Item newEntry = ItemMapper.toItem(itemDto);
newEntry.setId(lastGeneratedId++);
newEntry.setOwner(creator);
repository.put(newEntry.getId(), newEntry);
log.info("Сохранена новая вещь с ID: {}", newEntry.getId());
return ItemMapper.toItemDto(newEntry);
}
}