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 create, addEntry, removeEntry and checkEntry functional…
Browse files Browse the repository at this point in the history
…ity for shopping list widget
  • Loading branch information
Flugschnitzel committed Apr 29, 2024
1 parent d07ce12 commit 56eb5d7
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 0 deletions.
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public MapWidget addLocation(AuthenticatedPrincipal principal, String eventId, S
.id(UUID.randomUUID().toString())
.build();

//todo Wird der check hier benötigt nach dem eben eine UUID generiert wurde?
if (widget.getLocations().stream().anyMatch(l -> Objects.equals(l.getId(), location.getId()))) {
throw new IllegalStateException("Location with id " + location.getId() + " already exists");
}
Expand Down
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);
}

}
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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.time.LocalDateTime;

public abstract class Widget {
//todo Lohnt es sich hier nicht schon id und creationDate zu deklarieren?
public abstract String getId();
public abstract LocalDateTime getCreationDate();
public abstract WidgetType getWidgetType();
Expand Down
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;
}
}
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;
}
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 {
}
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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<button (click)="updateUserColor('LIGHT')">Lightmode</button>
<button (click)="updateUserColor('DARK')">Darkmode</button>
<button (click)="updateUserColor('MODERN')">Modernmode</button>
<button (click)="createShoppingListWidget()">CreateShoppingListWidget</button>
<button (click)="addShoppingListEntry()">AddItem</button>
<button (click)="removeShoppingListEntry()">RemoveItem</button>
<button (click)="checkShoppingListEntry()">CheckEntry</button>
<!-- TODO: remove above -->

<!-- TODO: add new component here for event banner -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {HttpClient} from "@angular/common/http";
styleUrl: './dashboard-content.component.scss'
})
export class DashboardContentComponent {
eventId: String = "c36cdf09-3e5b-4caf-8cf9-7b3c1a6dcdc4";
widgetId: String = "8f5ffd8c-4e61-483b-8810-443fa854e48d";
entryId: String = "5ea359e2-f536-47a5-ba6a-006337367270";

constructor(
public userService: UserService,
Expand Down Expand Up @@ -63,4 +66,31 @@ export class DashboardContentComponent {
console.log(result);
})
}

createShoppingListWidget(){
this.http.post(`http://localhost:8080/event/${this.eventId}/widgets/shopping-list/`, {},{withCredentials: true}).subscribe(result =>{
console.log(result);
})
}

addShoppingListEntry(){
this.http.post(`http://localhost:8080/event/${this.eventId}/widgets/shopping-list/${this.widgetId}/entries`, {
"description": "Brot",
"amount": "2 Leibe"
},{withCredentials: true}).subscribe(result =>{
console.log(result);
})
}

removeShoppingListEntry(){
this.http.delete(`http://localhost:8080/event/${this.eventId}/widgets/shopping-list/${this.widgetId}/entries/${this.entryId}`, {withCredentials: true}).subscribe(result =>{
console.log(result);
})
}

checkShoppingListEntry(){
this.http.put(`http://localhost:8080/event/${this.eventId}/widgets/shopping-list/${this.widgetId}/entries/${this.entryId}`, {}, {withCredentials: true}).subscribe(result =>{
console.log(result);
})
}
}

0 comments on commit 56eb5d7

Please sign in to comment.