diff --git a/api/src/main/java/com/wbrawner/twigs/budget/BudgetController.java b/api/src/main/java/com/wbrawner/twigs/budget/BudgetController.java index e3be1fe..35973f7 100644 --- a/api/src/main/java/com/wbrawner/twigs/budget/BudgetController.java +++ b/api/src/main/java/com/wbrawner/twigs/budget/BudgetController.java @@ -1,5 +1,6 @@ package com.wbrawner.twigs.budget; +import com.wbrawner.twigs.category.CategoryRepository; import com.wbrawner.twigs.permission.Permission; import com.wbrawner.twigs.permission.UserPermission; import com.wbrawner.twigs.permission.UserPermissionRepository; @@ -24,10 +25,11 @@ import java.util.stream.Collectors; import static com.wbrawner.twigs.Utils.getCurrentUser; @RestController -@RequestMapping(value = "/budgets") +@RequestMapping(value = "/api/budgets") @Transactional public class BudgetController { private final BudgetRepository budgetRepository; + private final CategoryRepository categoryRepository; private final TransactionRepository transactionRepository; private final UserRepository userRepository; private final UserPermissionRepository userPermissionsRepository; @@ -35,11 +37,13 @@ public class BudgetController { public BudgetController( BudgetRepository budgetRepository, + CategoryRepository categoryRepository, TransactionRepository transactionRepository, UserRepository userRepository, UserPermissionRepository userPermissionsRepository ) { this.budgetRepository = budgetRepository; + this.categoryRepository = categoryRepository; this.transactionRepository = transactionRepository; this.userRepository = userRepository; this.userPermissionsRepository = userPermissionsRepository; @@ -150,6 +154,7 @@ public class BudgetController { produces = {MediaType.APPLICATION_JSON_VALUE} ) public ResponseEntity updateBudget(@PathVariable String id, @RequestBody BudgetRequest request) { + // TODO: Make sure no changes in ownership are being attempted (except by the owner) return getBudgetWithPermission(id, Permission.MANAGE, (budget) -> { if (request.name != null) { budget.setName(request.name); @@ -171,7 +176,8 @@ public class BudgetController { ) )) )); - } else { + } + if (users.isEmpty()) { users.addAll(userPermissionsRepository.findAllByBudget(budget, null)); } @@ -179,11 +185,14 @@ public class BudgetController { }); } - @DeleteMapping(value = "/{id}", produces = {MediaType.TEXT_PLAIN_VALUE}) + @DeleteMapping(value = "/{id}") public ResponseEntity deleteBudget(@PathVariable String id) { return getBudgetWithPermission(id, Permission.MANAGE, (budget) -> { + categoryRepository.deleteAllByBudget(budget); + transactionRepository.deleteAllByBudget(budget); + userPermissionsRepository.deleteAllByBudget(budget); budgetRepository.delete(budget); - return ResponseEntity.ok().build(); + return ResponseEntity.noContent().build(); }); } diff --git a/api/src/main/java/com/wbrawner/twigs/category/CategoryRepository.java b/api/src/main/java/com/wbrawner/twigs/category/CategoryRepository.java index 657a8bd..085fce2 100644 --- a/api/src/main/java/com/wbrawner/twigs/category/CategoryRepository.java +++ b/api/src/main/java/com/wbrawner/twigs/category/CategoryRepository.java @@ -21,4 +21,6 @@ public interface CategoryRepository extends CrudRepository, Optional findByBudgetAndId(Budget budget, String id); List findAllByBudgetInAndIdIn(List budgets, List ids, Pageable pageable); + + void deleteAllByBudget(Budget budget); } \ No newline at end of file diff --git a/api/src/main/java/com/wbrawner/twigs/permission/UserPermissionRepository.java b/api/src/main/java/com/wbrawner/twigs/permission/UserPermissionRepository.java index 31a2ab5..e1e3e30 100644 --- a/api/src/main/java/com/wbrawner/twigs/permission/UserPermissionRepository.java +++ b/api/src/main/java/com/wbrawner/twigs/permission/UserPermissionRepository.java @@ -20,4 +20,6 @@ public interface UserPermissionRepository extends CrudRepository findAllByUserAndBudget(User user, Budget budget, Pageable pageable); List findAllByUserAndBudget_IdIn(User user, List budgetIds, Pageable pageable); + + void deleteAllByBudget(Budget budget); } \ No newline at end of file diff --git a/api/src/main/java/com/wbrawner/twigs/permission/UserPermissionResponse.java b/api/src/main/java/com/wbrawner/twigs/permission/UserPermissionResponse.java index d6adcb1..f31fca8 100644 --- a/api/src/main/java/com/wbrawner/twigs/permission/UserPermissionResponse.java +++ b/api/src/main/java/com/wbrawner/twigs/permission/UserPermissionResponse.java @@ -1,21 +1,21 @@ package com.wbrawner.twigs.permission; -import com.wbrawner.twigs.user.UserResponse; +import com.wbrawner.twigs.user.User; public class UserPermissionResponse { - private final UserResponse user; + private final String user; private final Permission permission; public UserPermissionResponse(UserPermission userPermission) { - this(new UserResponse(userPermission.getUser()), userPermission.getPermission()); + this(userPermission.getUser(), userPermission.getPermission()); } - public UserPermissionResponse(UserResponse userResponse, Permission permission) { - this.user = userResponse; + public UserPermissionResponse(User user, Permission permission) { + this.user = user.getId(); this.permission = permission; } - public UserResponse getUser() { + public String getUser() { return user; } diff --git a/api/src/main/java/com/wbrawner/twigs/transaction/TransactionRepository.java b/api/src/main/java/com/wbrawner/twigs/transaction/TransactionRepository.java index e798aea..7b21fcf 100644 --- a/api/src/main/java/com/wbrawner/twigs/transaction/TransactionRepository.java +++ b/api/src/main/java/com/wbrawner/twigs/transaction/TransactionRepository.java @@ -44,4 +44,6 @@ public interface TransactionRepository extends CrudRepository