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

Commit

Permalink
BE-#94: Use widgetMapper in ExpenseSplitWidgetService
Browse files Browse the repository at this point in the history
  • Loading branch information
Drumber committed May 17, 2024
1 parent e4e62f6 commit dc6e076
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,31 @@
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;
import com.dhbw.get2gether.backend.widget.application.mapper.WidgetMapper;
import com.dhbw.get2gether.backend.widget.model.expensesplit.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.AuthenticatedPrincipal;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@Service
public class ExpenseSplitWidgetService extends AbstractWidgetService {

private final ExpenseSplitMapper mapper;
private final UserMapper userMapper;
private final WidgetMapper widgetMapper;
private final UserService userService;

ExpenseSplitWidgetService(EventService eventService, UserMapper userMapper, ExpenseSplitMapper mapper, UserService userService) {
ExpenseSplitWidgetService(EventService eventService, WidgetMapper widgetMapper, ExpenseSplitMapper mapper, UserService userService) {
super(eventService);
this.mapper = mapper;
this.userMapper = userMapper;
this.widgetMapper = widgetMapper;
this.userService = userService;
}

Expand Down Expand Up @@ -58,7 +62,7 @@ public ExpenseSplitWidgetDto addEntry(AuthenticatedPrincipal principal, String e
.build()).toList())
.build();
widget.addEntry(entry);
return mapToDto(updateAndGetWidget(principal, event, widget));
return mapToDto(updateAndGetWidget(principal, event, widget), event.getParticipantIds());
}

@PreAuthorize("hasRole('USER')")
Expand All @@ -71,7 +75,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));
return mapToDto(updateAndGetWidget(principal, event, widget), event.getParticipantIds());
}

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

widget.replaceEntry(original_entry, updatedEntry);


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

private ExpenseSplitWidgetDto mapToDto(ExpenseSplitWidget widget) {
return ExpenseSplitWidgetDto.builder()
.id(widget.getId())
.creationDate(widget.getCreationDate())
.entries(widget.getEntries().stream().map(entry -> ExpenseEntryDto.builder()
.id(entry.getId())
.creatorId(entry.getCreatorId())
.description(entry.getDescription())
.price(entry.getPrice())
.involvedUsers(entry.getInvolvedUsers().stream().map(user -> UserWithPercentageDto.builder()
.user(userMapper.mapToSimpleUserDto(userService.getUserById(user.getUserId())))
.percentage(user.getPercentage())
.build()).toList())
.build()).toList())
.build();
private ExpenseSplitWidgetDto mapToDto(ExpenseSplitWidget widget, List<String> participantIds) {
List<SimpleUserDto> simpleUserDtos = userService.getSimpleUsersById(participantIds);
return widgetMapper.expenseSplitWidgetToExpenseSplitWidgetDto(widget, simpleUserDtos);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public IWidget widgetToIWidget(Widget widget, @Context List<SimpleUserDto> parti
return widget;
}

abstract ExpenseSplitWidgetDto expenseSplitWidgetToExpenseSplitWidgetDto(ExpenseSplitWidget widget, @Context List<SimpleUserDto> participants);
public abstract ExpenseSplitWidgetDto expenseSplitWidgetToExpenseSplitWidgetDto(ExpenseSplitWidget widget, @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

0 comments on commit dc6e076

Please sign in to comment.