Created
February 13, 2025 12:53
-
-
Save vNNi/cdb4ef21ed8c733ac29fcafecf547909 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class GroupDTO { | |
| private String groupId; | |
| private String groupName; | |
| private List<UserDTO> participants; | |
| // Getters, Setters e Construtores | |
| } | |
| public class UserDTO { | |
| private Long id; | |
| private String name; | |
| // Getters, Setters e Construtores | |
| } | |
| import org.springframework.http.ResponseEntity; | |
| import org.springframework.web.bind.annotation.GetMapping; | |
| import org.springframework.web.bind.annotation.RequestMapping; | |
| import org.springframework.web.bind.annotation.RestController; | |
| import java.util.List; | |
| import java.util.stream.Collectors; | |
| @RestController | |
| @RequestMapping("/groups") | |
| public class GroupController { | |
| @GetMapping("/{groupId}") | |
| public ResponseEntity<GroupDTO> getGroupWithParticipants(String groupId) { | |
| // Simulando a obtenção do grupo com seus participantes | |
| Group group = getGroupById(groupId); // Esse método é fictício, você obteria o grupo de um repositório ou serviço | |
| // Converte os participantes para UserDTO | |
| List<UserDTO> participantDTOs = group.getUser().stream() | |
| .map(user -> new UserDTO(user.getId(), user.getName())) | |
| .collect(Collectors.toList()); | |
| // Cria o DTO do grupo | |
| GroupDTO groupDTO = new GroupDTO(group.getGroupId(), group.getGroupName(), participantDTOs); | |
| // Retorna o ResponseEntity com o DTO | |
| return ResponseEntity.ok(groupDTO); // A resposta será retornada com status 200 e o corpo como o DTO do grupo | |
| } | |
| private Group getGroupById(String groupId) { | |
| // Método fictício para recuperar um grupo por ID | |
| return new Group(groupId, "name", getSampleUsers()); | |
| } | |
| private List<UserEntity> getSampleUsers() { | |
| return List.of( | |
| new UserEntity(1L, "João"), | |
| new UserEntity(2L, "Maria") | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment