Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
BE-#94: Mapping for ExpenseSplitWidget works
Browse files Browse the repository at this point in the history
  • Loading branch information
Flugschnitzel committed May 21, 2024
1 parent 2955e4b commit 7ce20a2
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public Event getSingleEvent(AuthenticatedPrincipal principal, String eventId) {
@PreAuthorize("hasRole('GUEST')")
public EventDetailDto getSingleEventDto(AuthenticatedPrincipal principal, String eventId) {
Event event = getSingleEvent(principal, eventId);
return eventMapper.toEventDetailDto(event, userService.getSimpleUsersById(event.getParticipantIds()));
User user = userService.getUserByPrincipal(principal);
return eventMapper.toEventDetailDto(event, userService.getSimpleUsersById(event.getParticipantIds()), user.getId());
}

@PreAuthorize("hasRole('USER')")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public interface EventMapper {
@Mapping(target = "participantIds", ignore = true)
Event updateEvent(@MappingTarget Event event, EventWidgetUpdateCommand eventWidgetUpdateCommand);

default EventDetailDto toEventDetailDto(Event event, @Context List<SimpleUserDto> participants) {
return _toEventDetailDto(event, participants, participants);
default EventDetailDto toEventDetailDto(Event event, @Context List<SimpleUserDto> participants, @Context String userId) {
return _toEventDetailDto(event, participants, participants, userId);
}

/**
Expand All @@ -48,5 +48,5 @@ default EventDetailDto toEventDetailDto(Event event, @Context List<SimpleUserDto
* This is a helper method to map an Event to an EventDro with a list of participants while also providing the participants list as mapping context.
* Mapstruct will use the {@code WidgetMapper} to map Widgets to IWidgets.
*/
EventDetailDto _toEventDetailDto(Event event, List<SimpleUserDto> participants, @Context List<SimpleUserDto> participantsContext);
EventDetailDto _toEventDetailDto(Event event, List<SimpleUserDto> participants, @Context List<SimpleUserDto> participantsContext, @Context String userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.dhbw.get2gether.backend.event.model.Event;
import com.dhbw.get2gether.backend.exceptions.EntityNotFoundException;
import com.dhbw.get2gether.backend.user.application.UserService;
import com.dhbw.get2gether.backend.user.application.mapper.UserMapper;
import com.dhbw.get2gether.backend.user.model.SimpleUserDto;
import com.dhbw.get2gether.backend.user.model.User;
import com.dhbw.get2gether.backend.widget.application.mapper.ExpenseSplitMapper;
Expand Down Expand Up @@ -62,7 +61,7 @@ public ExpenseSplitWidgetDto addEntry(AuthenticatedPrincipal principal, String e
.build()).toList())
.build();
widget.addEntry(entry);
return mapToDto(updateAndGetWidget(principal, event, widget), event.getParticipantIds());
return mapToDto(updateAndGetWidget(principal, event, widget), event.getParticipantIds(), principal);
}

@PreAuthorize("hasRole('USER')")
Expand All @@ -75,7 +74,7 @@ public ExpenseSplitWidgetDto removeEntry(AuthenticatedPrincipal principal, Strin
if (!widget.removeEntry(entry)) {
throw new IllegalStateException("Failed to remove entry from shopping list widget");
}
return mapToDto(updateAndGetWidget(principal, event, widget), event.getParticipantIds());
return mapToDto(updateAndGetWidget(principal, event, widget), event.getParticipantIds(), principal);
}

@PreAuthorize("hasRole('USER')")
Expand All @@ -101,11 +100,13 @@ public ExpenseSplitWidgetDto updateEntry(AuthenticatedPrincipal principal, Strin
.build();

widget.replaceEntry(original_entry, updatedEntry);
return mapToDto(updateAndGetWidget(principal, event, widget), event.getParticipantIds());
return mapToDto(updateAndGetWidget(principal, event, widget), event.getParticipantIds(), principal);
}

private ExpenseSplitWidgetDto mapToDto(ExpenseSplitWidget widget, List<String> participantIds) {
private ExpenseSplitWidgetDto mapToDto(ExpenseSplitWidget widget, List<String> participantIds, AuthenticatedPrincipal principal) {
List<SimpleUserDto> simpleUserDtos = userService.getSimpleUsersById(participantIds);
return widgetMapper.expenseSplitWidgetToExpenseSplitWidgetDto(widget, simpleUserDtos);
User user = userService.getUserByPrincipal(principal);
List<Dept> depts = widget.calculateDeptsForUserId(user.getId());
return widgetMapper.expenseSplitWidgetToExpenseSplitWidgetDto(widget, depts, simpleUserDtos);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ public abstract class WidgetMapper {

// Manual implementation of @SubclassMapping which returns the widget object if no custom mapping is needed.
// @SubclassMapping(source = ExpenseSplitWidget.class, target = ExpenseSplitWidgetDto.class)
public IWidget widgetToIWidget(Widget widget, @Context List<SimpleUserDto> participants) {
public IWidget widgetToIWidget(Widget widget, @Context List<SimpleUserDto> participants, @Context String userId) {
if (widget instanceof ExpenseSplitWidget) {
return expenseSplitWidgetToExpenseSplitWidgetDto((ExpenseSplitWidget) widget, participants);
List<Dept> depts = ((ExpenseSplitWidget) widget).calculateDeptsForUserId(userId);
return expenseSplitWidgetToExpenseSplitWidgetDto((ExpenseSplitWidget) widget, depts, participants);
}
return widget;
}

public abstract ExpenseSplitWidgetDto expenseSplitWidgetToExpenseSplitWidgetDto(ExpenseSplitWidget widget, @Context List<SimpleUserDto> participants);
public abstract ExpenseSplitWidgetDto expenseSplitWidgetToExpenseSplitWidgetDto(ExpenseSplitWidget widget, List<Dept> depts, @Context List<SimpleUserDto> participants);

// Find the userWithPercentage in the list of participants. Return null if the user is not found.
UserWithPercentageDto userWithPercentageToUserWithPercentageDto(UserWithPercentage userWithPercentage, @Context List<SimpleUserDto> participants) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class ExpenseEntryCardComponent {
onEdit = new EventEmitter();

get getInvolvedUserNames(): string[] {
return this.entry.involvedUsers.map(u => [u.user.firstName, u.user.firstName].join(" "));
return this.entry.involvedUsers.map(u => [u.user.firstName, u.user.lastName].join(" "));
}

get isEveryoneInvolved(): boolean {
Expand Down

0 comments on commit 7ce20a2

Please sign in to comment.