Skip to content

Commit

Permalink
BE-#94: Code-QA
Browse files Browse the repository at this point in the history
  • Loading branch information
Drumber committed May 21, 2024
1 parent f3ba98f commit aa43320
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Event createExpenseSplitWidget(AuthenticatedPrincipal principal, String e

@PreAuthorize("hasRole('USER')")
public ExpenseSplitWidgetDto addEntry(AuthenticatedPrincipal principal, String eventId, String widgetId, ExpenseEntryAddCommand addCommand) {
if(addCommand.getInvolvedUsers().isEmpty()) {
if (addCommand.getInvolvedUsers().isEmpty()) {
throw new IllegalArgumentException("At least one user must be involved in the expense entry");
}

Expand All @@ -57,7 +57,7 @@ public ExpenseSplitWidgetDto addEntry(AuthenticatedPrincipal principal, String e
.involvedUsers(addCommand.getInvolvedUsers().stream().map(user ->
UserWithPercentage.builder()
.userId(user)
.percentage((double) 1 /addCommand.getInvolvedUsers().size())
.percentage(1.0 / addCommand.getInvolvedUsers().size())
.build()).toList())
.build();
widget.addEntry(entry);
Expand All @@ -79,27 +79,29 @@ public ExpenseSplitWidgetDto removeEntry(AuthenticatedPrincipal principal, Strin

@PreAuthorize("hasRole('USER')")
public ExpenseSplitWidgetDto updateEntry(AuthenticatedPrincipal principal, String eventId, String widgetId, String entryId, ExpenseEntryUpdateCommand updateCommand) {
if(updateCommand.getInvolvedUsers().isEmpty()) {
if (updateCommand.getInvolvedUsers().isEmpty()) {
throw new IllegalArgumentException("At least one user must be involved in the expense entry");
}

Event event = getEventById(principal, eventId);
ExpenseSplitWidget widget = getWidgetFromEvent(event, widgetId);
ExpenseEntry original_entry = widget.getEntries().stream()
ExpenseEntry originalEntry = widget.getEntries().stream()
.filter(l -> Objects.equals(l.getId(), entryId)).findFirst()
.orElseThrow(() -> new EntityNotFoundException("Entry not found"));

ExpenseEntry updatedEntry = mapper.mapToEntry(updateCommand).toBuilder()
.id(original_entry.getId())
.creatorId(original_entry.getCreatorId())
.id(originalEntry.getId())
.creatorId(originalEntry.getCreatorId())
.involvedUsers(updateCommand.getInvolvedUsers().stream().map(user ->
UserWithPercentage.builder()
.userId(user)
.percentage((double) 1 /updateCommand.getInvolvedUsers().size())
.percentage(1.0 / updateCommand.getInvolvedUsers().size())
.build()).toList())
.build();

widget.replaceEntry(original_entry, updatedEntry);
if (!widget.replaceEntry(originalEntry, updatedEntry)) {
throw new IllegalStateException("Failed to replace entry from expense split widget");
}
return mapToDto(updateAndGetWidget(principal, event, widget), event.getParticipantIds(), principal);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,42 +43,41 @@ public boolean replaceEntry(ExpenseEntry oldEntry, ExpenseEntry newEntry) {
return true;
}

public List<Debt> calculateDebtsForUserId(String userId){
public List<Debt> calculateDebtsForUserId(String userId) {
List<Debt> debts = new ArrayList<>();
for(ExpenseEntry expenseEntry : entries){
if(expenseEntry.getCreatorId().equals(userId)){
for (ExpenseEntry expenseEntry : entries) {
if (expenseEntry.getCreatorId().equals(userId)) {
// Add every involved user except the buyer to List of debt
for(UserWithPercentage userWithPercentage: expenseEntry.getInvolvedUsers()){
if(!Objects.equals(userWithPercentage.getUserId(), userId)){
debts = addDebt(debts, userWithPercentage.getUserId(), expenseEntry.getPrice()*userWithPercentage.getPercentage());
for (UserWithPercentage userWithPercentage : expenseEntry.getInvolvedUsers()) {
if (!Objects.equals(userWithPercentage.getUserId(), userId)) {
debts = addDebt(debts, userWithPercentage.getUserId(), expenseEntry.getPrice() * userWithPercentage.getPercentage());
}
}
} else {
Optional<UserWithPercentage> userWithPercentage = expenseEntry.getInvolvedUsers().stream()
.filter(user -> user.getUserId().equals(userId)).findFirst();
if(userWithPercentage.isPresent()){
if (userWithPercentage.isPresent()) {
// Add the Debt of the user to the Debt List
// In this case the user for whom we calculate all debts is a involved user and not the payer
debts = addDebt(debts, expenseEntry.getCreatorId(), expenseEntry.getPrice()*userWithPercentage.get().getPercentage()*-1.0);
// In this case the user for whom we calculate all debts is an involved user and not the payer
debts = addDebt(debts, expenseEntry.getCreatorId(), expenseEntry.getPrice() * userWithPercentage.get().getPercentage() * -1.0);
}
}
}
return debts;
}

// This method extracts the logic if the debtor is already in the list or not
private List<Debt> addDebt(List<Debt> debts, String debtorId, double debtAmount){
private List<Debt> addDebt(List<Debt> debts, String debtorId, double debtAmount) {
Optional<Debt> optionalDebt = debts.stream()
.filter(debt -> debt.getUserId().equals(debtorId)).findFirst();

if(optionalDebt.isPresent()){
if (optionalDebt.isPresent()) {
// Calculate new debtAmount if already exists
Debt newDebt = new Debt(debtorId, optionalDebt.get().getDebtAmount()+debtAmount);
Debt newDebt = new Debt(debtorId, optionalDebt.get().getDebtAmount() + debtAmount);
int index = debts.indexOf(optionalDebt.get());
debts.set(index, newDebt);

} else {
// Add new debt if not exists already
// Add new debt if not already exists
debts.add(new Debt(debtorId, debtAmount));
}
return debts;
Expand Down

0 comments on commit aa43320

Please sign in to comment.