From 0e6183c9fb5bffc565d446a790bc464574cc0eff Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 10 May 2024 10:10:37 +0200 Subject: [PATCH] BE-#73: Return eventDetailDto with SimpleUser objects --- .../event/adapter/in/EventController.java | 9 ++---- .../event/application/EventService.java | 6 ++++ .../event/application/mapper/EventMapper.java | 5 ++++ .../backend/event/model/EventDetailDto.java | 30 +++++++++++++++++++ .../backend/user/application/UserService.java | 11 +++++++ .../user/application/mapper/UserMapper.java | 7 ++--- .../backend/user/model/SimpleUserDto.java | 17 +++++++++++ 7 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 backend/src/main/java/com/dhbw/get2gether/backend/event/model/EventDetailDto.java create mode 100644 backend/src/main/java/com/dhbw/get2gether/backend/user/model/SimpleUserDto.java diff --git a/backend/src/main/java/com/dhbw/get2gether/backend/event/adapter/in/EventController.java b/backend/src/main/java/com/dhbw/get2gether/backend/event/adapter/in/EventController.java index 6074044..e6e8027 100644 --- a/backend/src/main/java/com/dhbw/get2gether/backend/event/adapter/in/EventController.java +++ b/backend/src/main/java/com/dhbw/get2gether/backend/event/adapter/in/EventController.java @@ -1,10 +1,7 @@ package com.dhbw.get2gether.backend.event.adapter.in; import com.dhbw.get2gether.backend.event.application.EventService; -import com.dhbw.get2gether.backend.event.model.Event; -import com.dhbw.get2gether.backend.event.model.EventCreateCommand; -import com.dhbw.get2gether.backend.event.model.EventOverviewDto; -import com.dhbw.get2gether.backend.event.model.EventUpdateCommand; +import com.dhbw.get2gether.backend.event.model.*; import org.springframework.security.core.AuthenticatedPrincipal; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.oauth2.core.user.OAuth2User; @@ -39,9 +36,9 @@ public List getOwnEvents(@AuthenticationPrincipal OAuth2User p } @GetMapping("/{eventId}") - public Event getSingleEvent( + public EventDetailDto getSingleEvent( @AuthenticationPrincipal AuthenticatedPrincipal principal, @PathVariable String eventId) { - return eventService.getSingleEvent(principal, eventId); + return eventService.getSingleEventDto(principal, eventId); } @GetMapping("/{eventId}/generateInvitationLink") diff --git a/backend/src/main/java/com/dhbw/get2gether/backend/event/application/EventService.java b/backend/src/main/java/com/dhbw/get2gether/backend/event/application/EventService.java index 6843789..24fbad8 100644 --- a/backend/src/main/java/com/dhbw/get2gether/backend/event/application/EventService.java +++ b/backend/src/main/java/com/dhbw/get2gether/backend/event/application/EventService.java @@ -78,6 +78,12 @@ public Event getSingleEvent(AuthenticatedPrincipal principal, String eventId) { return getEventIfUserIsParticipant(principal, eventId); } + @PreAuthorize("hasRole('GUEST')") + public EventDetailDto getSingleEventDto(AuthenticatedPrincipal principal, String eventId) { + Event event = getSingleEvent(principal, eventId); + return eventMapper.toEventDetailDto(event, userService.getSimpleUsersById(event.getParticipantIds())); + } + @PreAuthorize("hasRole('USER')") public void deleteEventById(AuthenticatedPrincipal principal, String eventId) { Event event = getEventIfUserIsParticipant(principal, eventId); diff --git a/backend/src/main/java/com/dhbw/get2gether/backend/event/application/mapper/EventMapper.java b/backend/src/main/java/com/dhbw/get2gether/backend/event/application/mapper/EventMapper.java index 6ef116d..eac01b9 100644 --- a/backend/src/main/java/com/dhbw/get2gether/backend/event/application/mapper/EventMapper.java +++ b/backend/src/main/java/com/dhbw/get2gether/backend/event/application/mapper/EventMapper.java @@ -1,10 +1,13 @@ package com.dhbw.get2gether.backend.event.application.mapper; import com.dhbw.get2gether.backend.event.model.*; +import com.dhbw.get2gether.backend.user.model.SimpleUserDto; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.mapstruct.MappingTarget; +import java.util.List; + @Mapper(componentModel = "spring") public interface EventMapper { @@ -32,4 +35,6 @@ public interface EventMapper { @Mapping(target = "invitationLink", ignore = true) @Mapping(target = "participantIds", ignore = true) Event updateEvent(@MappingTarget Event event, EventWidgetUpdateCommand eventWidgetUpdateCommand); + + EventDetailDto toEventDetailDto(Event event, List participants); } diff --git a/backend/src/main/java/com/dhbw/get2gether/backend/event/model/EventDetailDto.java b/backend/src/main/java/com/dhbw/get2gether/backend/event/model/EventDetailDto.java new file mode 100644 index 0000000..9effd36 --- /dev/null +++ b/backend/src/main/java/com/dhbw/get2gether/backend/event/model/EventDetailDto.java @@ -0,0 +1,30 @@ +package com.dhbw.get2gether.backend.event.model; + +import com.dhbw.get2gether.backend.user.model.SimpleUserDto; +import com.dhbw.get2gether.backend.widget.model.Widget; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +import java.time.LocalDateTime; +import java.util.List; + +@AllArgsConstructor +@Getter +@Setter +@Builder(toBuilder = true) +public class EventDetailDto { + private String id; + + private final LocalDateTime creationDate; + private String name; + private String description; + private String location; + private LocalDateTime date; + private LocalDateTime endDate; + private String invitationLink; + private final String creatorId; + private final List participants; + private final List widgets; +} diff --git a/backend/src/main/java/com/dhbw/get2gether/backend/user/application/UserService.java b/backend/src/main/java/com/dhbw/get2gether/backend/user/application/UserService.java index 0793522..b6ee843 100644 --- a/backend/src/main/java/com/dhbw/get2gether/backend/user/application/UserService.java +++ b/backend/src/main/java/com/dhbw/get2gether/backend/user/application/UserService.java @@ -12,6 +12,8 @@ import org.springframework.stereotype.Service; import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; import java.util.Optional; import java.util.UUID; @@ -57,6 +59,15 @@ public User updateUser(AuthenticatedPrincipal principal, UpdateUserCommand updat return userRepository.save(newUser); } + @PreAuthorize("hasRole('GUEST')") + public List getSimpleUsersById(List userIds) { + List simpleUserDtos = new ArrayList<>(); + for(String userId : userIds) { + userRepository.findById(userId).ifPresent(user -> simpleUserDtos.add(userMapper.mapToSimpleUserDto(user))); + } + return simpleUserDtos; + } + protected User createUser(CreateUserCommand command) { User user = userMapper.mapToUser(command).toBuilder() .id(UUID.randomUUID().toString()) diff --git a/backend/src/main/java/com/dhbw/get2gether/backend/user/application/mapper/UserMapper.java b/backend/src/main/java/com/dhbw/get2gether/backend/user/application/mapper/UserMapper.java index e72a098..0fa66e3 100644 --- a/backend/src/main/java/com/dhbw/get2gether/backend/user/application/mapper/UserMapper.java +++ b/backend/src/main/java/com/dhbw/get2gether/backend/user/application/mapper/UserMapper.java @@ -1,9 +1,6 @@ package com.dhbw.get2gether.backend.user.application.mapper; -import com.dhbw.get2gether.backend.user.model.CreateUserCommand; -import com.dhbw.get2gether.backend.user.model.UpdateUserCommand; -import com.dhbw.get2gether.backend.user.model.SyncUserCommand; -import com.dhbw.get2gether.backend.user.model.User; +import com.dhbw.get2gether.backend.user.model.*; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.mapstruct.MappingTarget; @@ -20,4 +17,6 @@ public interface UserMapper { User mapSyncCommandToUser(@MappingTarget User user, SyncUserCommand command); User mapSettingsCommandToUser(@MappingTarget User user, UpdateUserCommand command); + + SimpleUserDto mapToSimpleUserDto(User user); } diff --git a/backend/src/main/java/com/dhbw/get2gether/backend/user/model/SimpleUserDto.java b/backend/src/main/java/com/dhbw/get2gether/backend/user/model/SimpleUserDto.java new file mode 100644 index 0000000..cabebf2 --- /dev/null +++ b/backend/src/main/java/com/dhbw/get2gether/backend/user/model/SimpleUserDto.java @@ -0,0 +1,17 @@ +package com.dhbw.get2gether.backend.user.model; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +@AllArgsConstructor +@Getter +@Setter +@Builder(toBuilder = true) +public class SimpleUserDto { + private final String id; + private String firstName; + private String lastName; + private String profilePictureUrl; +}