|
2 | 2 |
|
3 | 3 | import com.cf.cfteam.exceptions.codeforces.GroupNotFoundException; |
4 | 4 | import com.cf.cfteam.exceptions.security.UserNotFoundException; |
5 | | -import com.cf.cfteam.utils.codeforces.GroupMapper; |
| 5 | +import com.cf.cfteam.transfer.responses.codeforces.GroupResponse; |
| 6 | +import com.cf.cfteam.utils.codeforces.mappers.GroupMapper; |
6 | 7 | import com.cf.cfteam.models.entities.codeforces.Group; |
7 | 8 | import com.cf.cfteam.models.entities.security.User; |
8 | 9 | import com.cf.cfteam.repositories.jpa.codeforces.GroupRepository; |
9 | 10 | import com.cf.cfteam.repositories.jpa.security.UserRepository; |
10 | 11 | import com.cf.cfteam.transfer.payloads.codeforces.GroupPayload; |
| 12 | +import com.cf.cfteam.utils.codeforces.mappers.TeamMapper; |
11 | 13 | import lombok.RequiredArgsConstructor; |
12 | 14 | import org.springframework.stereotype.Service; |
13 | 15 |
|
| 16 | +import javax.swing.text.html.Option; |
14 | 17 | import java.util.List; |
| 18 | +import java.util.Optional; |
15 | 19 |
|
16 | 20 | @Service |
17 | 21 | @RequiredArgsConstructor |
18 | 22 | public class GroupService { |
19 | 23 |
|
20 | 24 | private final GroupRepository groupRepository; |
21 | 25 | private final UserRepository userRepository; |
| 26 | + private final GroupMapper groupMapper; |
22 | 27 |
|
23 | | - public List<Group> getAllGroupsByUser(Long userId) { |
| 28 | + public List<GroupResponse> getAllGroupsByUser(Long userId) { |
24 | 29 | User user = userRepository.findById(userId) |
25 | 30 | .orElseThrow(() -> new UserNotFoundException(userId)); |
26 | | - return groupRepository.findByUser(user); |
| 31 | + |
| 32 | + List<Group> groups = groupRepository.findByUser(user); |
| 33 | + |
| 34 | + return groups.stream() |
| 35 | + .map(groupMapper::fromEntityToResponse) |
| 36 | + .toList(); |
27 | 37 | } |
28 | 38 |
|
29 | | - public Group getGroupById(Long groupId) { |
30 | | - return groupRepository.findById(groupId) |
| 39 | + public GroupResponse getGroupById(Long groupId) { |
| 40 | + Group group = groupRepository.findById(groupId) |
31 | 41 | .orElseThrow(() -> new GroupNotFoundException(groupId)); |
| 42 | + |
| 43 | + return groupMapper.fromEntityToResponse(group); |
32 | 44 | } |
33 | 45 |
|
34 | | - public Group addGroupToUser(Long userId, GroupPayload groupPayload) { |
| 46 | + public GroupResponse addGroupToUser(Long userId, GroupPayload groupPayload) { |
35 | 47 | User user = userRepository.findById(userId) |
36 | 48 | .orElseThrow(() -> new UserNotFoundException(userId)); |
37 | 49 |
|
38 | | - Group group = GroupMapper.fromPayloadToEntity(groupPayload, user); |
| 50 | + Group group = groupMapper.fromPayloadToEntity(groupPayload, user); |
| 51 | + group = groupRepository.save(group); |
39 | 52 |
|
40 | | - return groupRepository.save(group); |
| 53 | + return groupMapper.fromEntityToResponse(group); |
41 | 54 | } |
42 | 55 |
|
43 | | - public Group updateGroup(Long groupId, GroupPayload groupPayload) { |
| 56 | + public GroupResponse updateGroup(Long groupId, GroupPayload groupPayload) { |
44 | 57 | Group group = groupRepository.findById(groupId) |
45 | 58 | .orElseThrow(() -> new GroupNotFoundException(groupId)); |
46 | 59 |
|
47 | | - Group updatedGroup = GroupMapper.updateEntityFromPayload(group, groupPayload); |
| 60 | + Group updatedGroup = groupMapper.updateEntityFromPayload(group, groupPayload); |
| 61 | + updatedGroup = groupRepository.save(updatedGroup); |
48 | 62 |
|
49 | | - return groupRepository.save(updatedGroup); |
| 63 | + return groupMapper.fromEntityToResponse(updatedGroup); |
50 | 64 | } |
51 | 65 |
|
52 | 66 |
|
|
0 commit comments