This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BE-#92: Added create, addEntry, removeEntry and checkEntry functional…
…ity for shopping list widget
- Loading branch information
1 parent
d07ce12
commit 56eb5d7
Showing
11 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
...main/java/com/dhbw/get2gether/backend/widget/adapter/in/ShoppingListWidgetController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.dhbw.get2gether.backend.widget.adapter.in; | ||
|
||
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.ShoppingListCreateCommand; | ||
import com.dhbw.get2gether.backend.widget.model.shoppinglist.ShoppingListWidget; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/event/{eventId}/widgets/shopping-list") | ||
public class ShoppingListWidgetController { | ||
|
||
private final ShoppingListWidgetService service; | ||
|
||
ShoppingListWidgetController(ShoppingListWidgetService shoppingListWidgetService) { | ||
this.service = shoppingListWidgetService; | ||
} | ||
|
||
@PostMapping("/") | ||
public Event createShoppingListWidget( | ||
@AuthenticationPrincipal OAuth2User principal, | ||
@PathVariable String eventId, | ||
@RequestBody ShoppingListCreateCommand createCommand | ||
) { | ||
return service.createShoppingListWidget(principal, eventId, createCommand); | ||
} | ||
|
||
@PostMapping("/{widgetId}/entries") | ||
public ShoppingListWidget addEntry( | ||
@AuthenticationPrincipal OAuth2User principal, | ||
@PathVariable String eventId, | ||
@PathVariable String widgetId, | ||
@RequestBody EntryAddCommand addCommand | ||
) { | ||
return service.addEntry(principal, eventId, widgetId, addCommand); | ||
} | ||
|
||
@DeleteMapping("/{widgetId}/entries/{entryId}") | ||
public ShoppingListWidget removeEntry( | ||
@AuthenticationPrincipal OAuth2User principal, | ||
@PathVariable String eventId, | ||
@PathVariable String widgetId, | ||
@PathVariable String entryId | ||
) { | ||
return service.removeEntry(principal, eventId, widgetId, entryId); | ||
} | ||
|
||
@PutMapping("/{widgetId}/entries/{entryId}") | ||
public ShoppingListWidget checkEntry( | ||
@AuthenticationPrincipal OAuth2User principal, | ||
@PathVariable String eventId, | ||
@PathVariable String widgetId, | ||
@PathVariable String entryId | ||
) { | ||
return service.checkEntry(principal, eventId, widgetId, entryId); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...c/main/java/com/dhbw/get2gether/backend/widget/application/ShoppingListWidgetService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.dhbw.get2gether.backend.widget.application; | ||
|
||
import com.dhbw.get2gether.backend.event.application.EventService; | ||
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.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 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 ShoppingListWidgetService extends AbstractWidgetService { | ||
|
||
private final ShoppingListMapper mapper; | ||
private final UserService userService; | ||
|
||
ShoppingListWidgetService(EventService eventService, ShoppingListMapper mapper, UserService userService) { | ||
super(eventService); | ||
this.mapper = mapper; | ||
this.userService = userService; | ||
} | ||
|
||
@PreAuthorize("hasRole('USER')") | ||
public Event createShoppingListWidget(AuthenticatedPrincipal principal, String eventId, ShoppingListCreateCommand createCommand) { | ||
Event event = getEventById(principal, eventId); | ||
ShoppingListWidget widget = mapper.mapToShoppingListWidget(createCommand).toBuilder() | ||
.id(UUID.randomUUID().toString()) | ||
.creationDate(LocalDateTime.now()) | ||
.entries(List.of()) | ||
.build(); | ||
return addWidget(principal, event, widget); | ||
} | ||
|
||
@PreAuthorize("hasRole('USER')") | ||
public ShoppingListWidget addEntry(AuthenticatedPrincipal principal, String eventId, String widgetId, EntryAddCommand addCommand) { | ||
Event event = getEventById(principal, eventId); | ||
ShoppingListWidget widget = getWidgetFromEvent(event, widgetId); | ||
|
||
Entry entry = mapper.mapToEntry(addCommand).toBuilder() | ||
.id(UUID.randomUUID().toString()) | ||
.checked(false) | ||
.creatorId(userService.getUserByPrincipal(principal).getId()) | ||
.build(); | ||
|
||
widget.addEntry(entry); | ||
return updateAndGetWidget(principal, event, widget); | ||
} | ||
|
||
@PreAuthorize("hasRole('USER')") | ||
public ShoppingListWidget removeEntry(AuthenticatedPrincipal principal, String eventId, String widgetId, String entryId) { | ||
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")); | ||
if (!widget.removeEntry(entry)) { | ||
throw new IllegalStateException("Failed to remove entry from event."); | ||
} | ||
return updateAndGetWidget(principal, event, widget); | ||
} | ||
|
||
@PreAuthorize("hasRole('USER')") | ||
public ShoppingListWidget checkEntry(AuthenticatedPrincipal principal, String eventId, String widgetId, String entryId) { | ||
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()); | ||
return updateAndGetWidget(principal, event, widget); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...c/main/java/com/dhbw/get2gether/backend/widget/application/mapper/ShoppingListMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
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 org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
|
||
@Mapper(componentModel = "spring") | ||
public interface ShoppingListMapper { | ||
|
||
@Mapping(target = "id", ignore = true) | ||
@Mapping(target = "entries", ignore = true) | ||
ShoppingListWidget mapToShoppingListWidget(ShoppingListCreateCommand createCommand); | ||
|
||
@Mapping(target = "id", ignore = true) | ||
@Mapping(target = "checked", ignore = true) | ||
@Mapping(target = "creatorId", ignore = true) | ||
@Mapping(target = "buyerId", ignore = true) | ||
Entry mapToEntry(EntryAddCommand command); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
backend/src/main/java/com/dhbw/get2gether/backend/widget/model/shoppinglist/Entry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.dhbw.get2gether.backend.widget.model.shoppinglist; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Builder(toBuilder = true) | ||
@Getter | ||
@AllArgsConstructor | ||
public class Entry { | ||
private String id; | ||
private boolean checked; | ||
private String creatorId; | ||
private String description; | ||
private String amount; | ||
private String buyerId; | ||
|
||
public void check(String buyerId) { | ||
this.checked = true; | ||
this.buyerId = buyerId; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
.../src/main/java/com/dhbw/get2gether/backend/widget/model/shoppinglist/EntryAddCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.dhbw.get2gether.backend.widget.model.shoppinglist; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Builder(toBuilder = true) | ||
@Getter | ||
@AllArgsConstructor | ||
public class EntryAddCommand { | ||
private String description; | ||
private String amount; | ||
} |
11 changes: 11 additions & 0 deletions
11
...java/com/dhbw/get2gether/backend/widget/model/shoppinglist/ShoppingListCreateCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.dhbw.get2gether.backend.widget.model.shoppinglist; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Builder(toBuilder = true) | ||
@Getter | ||
@AllArgsConstructor | ||
public class ShoppingListCreateCommand { | ||
} |
34 changes: 34 additions & 0 deletions
34
...c/main/java/com/dhbw/get2gether/backend/widget/model/shoppinglist/ShoppingListWidget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.dhbw.get2gether.backend.widget.model.shoppinglist; | ||
|
||
import com.dhbw.get2gether.backend.widget.model.Widget; | ||
import com.dhbw.get2gether.backend.widget.model.WidgetType; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Builder(toBuilder = true) | ||
@Getter | ||
@AllArgsConstructor | ||
public class ShoppingListWidget extends Widget { | ||
private final String id; | ||
private final LocalDateTime creationDate; | ||
private List<Entry> entries = new ArrayList<>(); | ||
|
||
@Override | ||
public WidgetType getWidgetType() { | ||
return WidgetType.SHOPPING_LIST; | ||
} | ||
|
||
public void addEntry(Entry entry) { | ||
entries.add(entry); | ||
} | ||
|
||
public boolean removeEntry(Entry entry) { | ||
return entries.remove(entry); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters