From aa433207c066e42f970ccd3e11845eaa716c3777 Mon Sep 17 00:00:00 2001 From: Lars <29163322+Drumber@users.noreply.github.com> Date: Tue, 21 May 2024 18:40:02 +0200 Subject: [PATCH] BE-#94: Code-QA --- .../ExpenseSplitWidgetService.java | 18 +++++++------ .../expensesplit/ExpenseSplitWidget.java | 27 +++++++++---------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/backend/src/main/java/com/dhbw/get2gether/backend/widget/application/ExpenseSplitWidgetService.java b/backend/src/main/java/com/dhbw/get2gether/backend/widget/application/ExpenseSplitWidgetService.java index ee067a6..c0d60c5 100644 --- a/backend/src/main/java/com/dhbw/get2gether/backend/widget/application/ExpenseSplitWidgetService.java +++ b/backend/src/main/java/com/dhbw/get2gether/backend/widget/application/ExpenseSplitWidgetService.java @@ -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"); } @@ -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); @@ -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); } diff --git a/backend/src/main/java/com/dhbw/get2gether/backend/widget/model/expensesplit/ExpenseSplitWidget.java b/backend/src/main/java/com/dhbw/get2gether/backend/widget/model/expensesplit/ExpenseSplitWidget.java index ebb870f..0b75f5d 100644 --- a/backend/src/main/java/com/dhbw/get2gether/backend/widget/model/expensesplit/ExpenseSplitWidget.java +++ b/backend/src/main/java/com/dhbw/get2gether/backend/widget/model/expensesplit/ExpenseSplitWidget.java @@ -43,23 +43,23 @@ public boolean replaceEntry(ExpenseEntry oldEntry, ExpenseEntry newEntry) { return true; } - public List calculateDebtsForUserId(String userId){ + public List calculateDebtsForUserId(String userId) { List 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 = 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); } } } @@ -67,18 +67,17 @@ public List calculateDebtsForUserId(String userId){ } // This method extracts the logic if the debtor is already in the list or not - private List addDebt(List debts, String debtorId, double debtAmount){ + private List addDebt(List debts, String debtorId, double debtAmount) { Optional 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;