-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathResearchGroupService.java
More file actions
105 lines (93 loc) · 4.17 KB
/
Copy pathResearchGroupService.java
File metadata and controls
105 lines (93 loc) · 4.17 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
98
99
100
101
102
103
104
105
package de.tum.cit.aet.usermanagement.service;
import java.util.UUID;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import de.tum.cit.aet.core.dto.PageDTO;
import de.tum.cit.aet.core.dto.PageResponseDTO;
import de.tum.cit.aet.core.exception.EntityNotFoundException;
import de.tum.cit.aet.usermanagement.domain.ResearchGroup;
import de.tum.cit.aet.usermanagement.dto.ResearchGroupDTO;
import de.tum.cit.aet.usermanagement.dto.ResearchGroupLargeDTO;
import de.tum.cit.aet.usermanagement.repository.ResearchGroupRepository;
import lombok.RequiredArgsConstructor;
/**
* Service for managing research groups.
*/
@Service
@RequiredArgsConstructor
public class ResearchGroupService {
private final ResearchGroupRepository researchGroupRepository;
/**
* Retrieves a research group by its ID.
*
* @param researchGroupId the ID of the research group
* @return the research group DTO
* @throws EntityNotFoundException if the research group is not found
*/
public ResearchGroupDTO getResearchGroup(UUID researchGroupId) {
ResearchGroup researchGroup = researchGroupRepository.findByIdElseThrow(researchGroupId);
return ResearchGroupDTO.getFromEntity(researchGroup);
}
/**
* Retrieves the details of a research group by its ID.
*
* @param researchGroupId the unique identifier of the research group
* @return a {@link ResearchGroupLargeDTO} containing detailed information about
* the research group
*/
public ResearchGroupLargeDTO getResearchGroupDetails(UUID researchGroupId) {
ResearchGroup researchGroup = researchGroupRepository.findById(researchGroupId)
.orElseThrow(() -> EntityNotFoundException.forId("ResearchGroup", researchGroupId));
return new ResearchGroupLargeDTO(
researchGroup.getDescription(),
researchGroup.getEmail(),
researchGroup.getWebsite(),
researchGroup.getStreet(),
researchGroup.getPostalCode(),
researchGroup.getCity());
}
/**
* Retrieves all research groups.
*
* @param pageDTO the pagination parameters
* @return list of all research group DTOs
*/
public PageResponseDTO<ResearchGroupDTO> getAllResearchGroups(PageDTO pageDTO) {
Pageable pageable = PageRequest.of(pageDTO.pageNumber(), pageDTO.pageSize(), Sort.by(Sort.Direction.ASC, "name"));
Page<ResearchGroup> page = researchGroupRepository.findAll(pageable);
return new PageResponseDTO<>(page.get().map(ResearchGroupDTO::getFromEntity).toList(), page.getTotalElements());
}
/**
* Updates an existing research group.
*
* @param researchGroupId the ID of the research group to update
* @param researchGroupDTO the research group data to update
* @return the updated research group DTO
* @throws EntityNotFoundException if the research group is not found
*/
public ResearchGroupDTO updateResearchGroup(UUID researchGroupId, ResearchGroupDTO researchGroupDTO) {
ResearchGroup researchGroup = researchGroupRepository.findByIdElseThrow(researchGroupId);
updateEntityFromDTO(researchGroup, researchGroupDTO);
ResearchGroup updatedResearchGroup = researchGroupRepository.save(researchGroup);
return ResearchGroupDTO.getFromEntity(updatedResearchGroup);
}
/**
* Updates a ResearchGroup entity with values from the provided DTO.
*/
private void updateEntityFromDTO(ResearchGroup entity, ResearchGroupDTO dto) {
entity.setName(dto.name());
entity.setAbbreviation(dto.abbreviation());
entity.setHead(dto.head());
entity.setEmail(dto.email());
entity.setWebsite(dto.website());
entity.setSchool(dto.school());
entity.setDescription(dto.description());
entity.setStreet(dto.street());
entity.setPostalCode(dto.postalCode());
entity.setCity(dto.city());
entity.setDefaultFieldOfStudies(dto.defaultFieldOfStudies());
}
}