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

Commit

Permalink
BE-#92: Added uncheck and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Flugschnitzel committed May 6, 2024
1 parent 0dbf1e0 commit 3c07d07
Show file tree
Hide file tree
Showing 7 changed files with 388 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.dhbw.get2gether.backend.event.model.Event;
import com.dhbw.get2gether.backend.widget.application.ShoppingListWidgetService;
import com.dhbw.get2gether.backend.widget.model.shoppinglist.EntryAddCommand;
import com.dhbw.get2gether.backend.widget.model.shoppinglist.EntryCheckCommand;
import com.dhbw.get2gether.backend.widget.model.shoppinglist.ShoppingListCreateCommand;
import com.dhbw.get2gether.backend.widget.model.shoppinglist.ShoppingListWidget;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
Expand Down Expand Up @@ -53,9 +54,10 @@ public ShoppingListWidget checkEntry(
@AuthenticationPrincipal OAuth2User principal,
@PathVariable String eventId,
@PathVariable String widgetId,
@PathVariable String entryId
) {
return service.checkEntry(principal, eventId, widgetId, entryId);
@PathVariable String entryId,
@RequestBody EntryCheckCommand checkCommand
) {
return service.checkEntry(principal, eventId, widgetId, entryId, checkCommand);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import com.dhbw.get2gether.backend.exceptions.EntityNotFoundException;
import com.dhbw.get2gether.backend.user.application.UserService;
import com.dhbw.get2gether.backend.widget.application.mapper.ShoppingListMapper;
import com.dhbw.get2gether.backend.widget.model.shoppinglist.Entry;
import com.dhbw.get2gether.backend.widget.model.shoppinglist.EntryAddCommand;
import com.dhbw.get2gether.backend.widget.model.shoppinglist.ShoppingListCreateCommand;
import com.dhbw.get2gether.backend.widget.model.shoppinglist.ShoppingListWidget;
import com.dhbw.get2gether.backend.widget.model.shoppinglist.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.AuthenticatedPrincipal;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -68,13 +65,13 @@ public ShoppingListWidget removeEntry(AuthenticatedPrincipal principal, String e
}

@PreAuthorize("hasRole('USER')")
public ShoppingListWidget checkEntry(AuthenticatedPrincipal principal, String eventId, String widgetId, String entryId) {
public ShoppingListWidget checkEntry(AuthenticatedPrincipal principal, String eventId, String widgetId, String entryId, EntryCheckCommand checkCommand) {
Event event = getEventById(principal, eventId);
ShoppingListWidget widget = getWidgetFromEvent(event, widgetId);
Entry entry = widget.getEntries().stream()
.filter(l -> Objects.equals(l.getId(), entryId)).findFirst()
.orElseThrow(() -> new EntityNotFoundException("Entry not found"));
entry.check(userService.getUserByPrincipal(principal).getId());
entry.check(userService.getUserByPrincipal(principal).getId(), mapper.mapEntryCheckCommandToEntryCheck(checkCommand));
return updateAndGetWidget(principal, event, widget);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.dhbw.get2gether.backend.widget.application.mapper;

import com.dhbw.get2gether.backend.widget.model.shoppinglist.Entry;
import com.dhbw.get2gether.backend.widget.model.shoppinglist.EntryAddCommand;
import com.dhbw.get2gether.backend.widget.model.shoppinglist.ShoppingListCreateCommand;
import com.dhbw.get2gether.backend.widget.model.shoppinglist.ShoppingListWidget;
import com.dhbw.get2gether.backend.widget.model.shoppinglist.*;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

@Mapper(componentModel = "spring")
public interface ShoppingListMapper {
ShoppingListMapper MAPPER = Mappers.getMapper(ShoppingListMapper.class);

@Mapping(target = "id", ignore = true)
@Mapping(target = "entries", ignore = true)
Expand All @@ -20,4 +19,8 @@ public interface ShoppingListMapper {
@Mapping(target = "buyerId", ignore = true)
Entry mapToEntry(EntryAddCommand command);

// Verstehe nicht, warum hier source checked ist unt nicht isChecked
@Mapping(target = "isChecked", source = "checked")
EntryCheck mapEntryCheckCommandToEntryCheck(EntryCheckCommand command);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ public class Entry {
private String amount;
private String buyerId;

public void check(String buyerId) {
this.checked = true;
this.buyerId = buyerId;
public void check(String buyerId, EntryCheck entryCheck) {
if(entryCheck.isChecked()) {
this.checked = true;
this.buyerId = buyerId;
} else {
this.checked = false;
// BuyerID soll nicht gelöscht werden, wenn der Eintrag nicht mehr gecheckt ist, aber überschrieben werden können
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.dhbw.get2gether.backend.widget.model.shoppinglist;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Builder(toBuilder = true)
@Getter
@Setter
@AllArgsConstructor
public class EntryCheck {
private boolean isChecked;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.dhbw.get2gether.backend.widget.model.shoppinglist;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Builder(toBuilder = true)
@Getter
@AllArgsConstructor
public class EntryCheckCommand {
private boolean isChecked;
}
Loading

0 comments on commit 3c07d07

Please sign in to comment.