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

Commit

Permalink
Merge pull request #11 from BudgetBuddyDE/delete-multiple-items
Browse files Browse the repository at this point in the history
feat: Support deletion of multiple entries at once
  • Loading branch information
tklein1801 authored Jan 19, 2024
2 parents 28c2a89 + 458f568 commit d18eab5
Show file tree
Hide file tree
Showing 17 changed files with 823 additions and 350 deletions.
Binary file removed .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
HELP.md
.gradle
.DS_Store
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/de/budgetbuddy/backend/budget/Budget.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import de.budgetbuddy.backend.user.User;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnDefault;

import java.util.Date;
Expand All @@ -13,6 +15,7 @@
@Entity
@Table(name = "budget", schema = "public")
@Data
@Builder
@AllArgsConstructor
public class Budget {

Expand Down Expand Up @@ -45,6 +48,10 @@ public Budget(Category category, User owner, Double budget) {
this.createdAt = new Date();
}

public Budget.Delete toDelete() {
return new Budget.Delete(this.id);
}

@Data
public static class Create {
private Long categoryId;
Expand All @@ -60,6 +67,8 @@ public static class Update {
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Delete {
private Long budgetId;
}
Expand Down
57 changes: 37 additions & 20 deletions src/main/java/de/budgetbuddy/backend/budget/BudgetController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import de.budgetbuddy.backend.auth.AuthorizationInterceptor;
import de.budgetbuddy.backend.category.Category;
import de.budgetbuddy.backend.category.CategoryRepository;
import de.budgetbuddy.backend.transaction.Transaction;
import de.budgetbuddy.backend.user.User;
import de.budgetbuddy.backend.user.UserRepository;
import jakarta.servlet.http.HttpSession;
Expand All @@ -13,10 +14,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.*;

@RestController
@RequestMapping("/v1/budget")
Expand Down Expand Up @@ -163,27 +161,46 @@ public ResponseEntity<ApiResponse<Budget>> updateBudget(@RequestBody Budget.Upda
}

@DeleteMapping
public ResponseEntity<ApiResponse<Budget>> deleteBudget(@RequestBody Budget.Delete payload, HttpSession session) throws JsonProcessingException {
Optional<Budget> budget = budgetRepository.findById(payload.getBudgetId());
if (budget.isEmpty()) {
public ResponseEntity<ApiResponse<Map<String, List<?>>>> deleteBudget(
@RequestBody List<Budget.Delete> payloads,
HttpSession session) throws JsonProcessingException {
if (payloads.size() == 0) {
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(new ApiResponse<>(404, "Provided budget doesn't exist"));
.status(HttpStatus.BAD_REQUEST)
.body(new ApiResponse<>(HttpStatus.BAD_REQUEST, "No budgets we're provided"));
}

Optional<User> sessionUser = AuthorizationInterceptor.getSessionUser(session);
if (sessionUser.isEmpty()) {
return AuthorizationInterceptor.noValidSessionResponse();
} else if (!sessionUser.get().getUuid().equals(budget.get().getOwner().getUuid())) {
return ResponseEntity
.status(HttpStatus.CONFLICT)
.body(new ApiResponse<>(HttpStatus.CONFLICT.value(), "You can't delete budgets from different users"));
List<Budget> successfullyDeleted = new ArrayList<>();
List<Budget.Delete> failedToDelete = new ArrayList<>();

for (Budget.Delete payload : payloads) {
Optional<Budget> optBudget = budgetRepository.findById(payload.getBudgetId());
if (optBudget.isEmpty()) {
failedToDelete.add(payload);
} else {
Budget budget = optBudget.get();
User budgetOwner = budget.getOwner();
Optional<User> optSessionUser = AuthorizationInterceptor.getSessionUser(session);

if (optSessionUser.isEmpty()
|| !optSessionUser.get().getUuid().equals(budgetOwner.getUuid())) {
failedToDelete.add(payload);
} else {
budgetRepository.delete(budget);
successfullyDeleted.add(budget);
}
}
}


budgetRepository.deleteById(payload.getBudgetId());
Map<String, List<?>> response = new HashMap<>();
response.put("success", successfullyDeleted);
response.put("failed", failedToDelete);
boolean didAllFail = failedToDelete.size() == payloads.size();
return ResponseEntity
.status(HttpStatus.OK)
.body(new ApiResponse<>(200, budget.get()));
.status(didAllFail ? HttpStatus.BAD_REQUEST : HttpStatus.OK)
.body(new ApiResponse<>(
didAllFail ? HttpStatus.BAD_REQUEST : HttpStatus.OK,
didAllFail ? "All provided budgets we're invalid values" : null,
response));
}
}
9 changes: 9 additions & 0 deletions src/main/java/de/budgetbuddy/backend/category/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import de.budgetbuddy.backend.user.User;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnDefault;

import java.util.Date;
Expand All @@ -12,6 +14,7 @@
@Entity
@Table(name = "category", schema = "public")
@Data
@Builder
@AllArgsConstructor
public class Category {

Expand Down Expand Up @@ -43,6 +46,10 @@ public Category(User owner, String name, String description) {
this.createdAt = new Date();
}

public Category.Delete toDelete() {
return new Category.Delete(this.id);
}

@Data
public static class Create {
public UUID owner;
Expand All @@ -58,6 +65,8 @@ public static class Update {
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Delete {
public Long categoryId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.*;

@RestController
@RequestMapping("/v1/category")
Expand Down Expand Up @@ -113,26 +110,46 @@ public ResponseEntity<ApiResponse<Category>> updateCategory(@RequestBody Categor
}

@DeleteMapping
public ResponseEntity<ApiResponse<Category>> deleteCategory(@RequestBody Category.Delete payload, HttpSession session) throws JsonProcessingException {
Optional<Category> category = categoryRepository.findById(payload.getCategoryId());
if (category.isEmpty()) {
public ResponseEntity<ApiResponse<Map<String, List<?>>>> deleteCategories(
@RequestBody List<Category.Delete> payloads,
HttpSession session) throws JsonProcessingException {
if (payloads.size() == 0) {
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(new ApiResponse<>(404, "Provided category doesn't exist"));
.status(HttpStatus.BAD_REQUEST)
.body(new ApiResponse<>(HttpStatus.BAD_REQUEST, "No categories we're provided"));
}

Optional<User> sessionUser = AuthorizationInterceptor.getSessionUser(session);
if (sessionUser.isEmpty()) {
return AuthorizationInterceptor.noValidSessionResponse();
} else if (!sessionUser.get().getUuid().equals(category.get().getOwner().getUuid())) {
return ResponseEntity
.status(HttpStatus.CONFLICT)
.body(new ApiResponse<>(HttpStatus.CONFLICT.value(), "You can't delete categories from other users"));
List<Category> successfullyDeleted = new ArrayList<>();
List<Category.Delete> failedToDelete = new ArrayList<>();

for (Category.Delete payload : payloads) {
Optional<Category> categoryOptional = categoryRepository.findById(payload.getCategoryId());
if (categoryOptional.isEmpty()) {
failedToDelete.add(payload);
} else {
Category category = categoryOptional.get();
User categoryOwner = category.getOwner();
Optional<User> optSessionUser = AuthorizationInterceptor.getSessionUser(session);

if (optSessionUser.isEmpty()
|| !optSessionUser.get().getUuid().equals(categoryOwner.getUuid())) {
failedToDelete.add(payload);
} else {
categoryRepository.delete(category);
successfullyDeleted.add(category);
}
}
}

categoryRepository.deleteById(payload.getCategoryId());
Map<String, List<?>> response = new HashMap<>();
response.put("success", successfullyDeleted);
response.put("failed", failedToDelete);
boolean didAllFail = failedToDelete.size() == payloads.size();
return ResponseEntity
.status(HttpStatus.OK)
.body(new ApiResponse<>(200, category.get()));
.status(didAllFail ? HttpStatus.BAD_REQUEST : HttpStatus.OK)
.body(new ApiResponse<>(
didAllFail ? HttpStatus.BAD_REQUEST : HttpStatus.OK,
didAllFail ? "All provided categories we're invalid values" : null,
response));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import de.budgetbuddy.backend.user.User;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnDefault;

import java.util.Date;
Expand All @@ -12,6 +14,7 @@
@Entity
@Table(name = "payment_method", schema = "public")
@Data
@Builder
@AllArgsConstructor
public class PaymentMethod {

Expand Down Expand Up @@ -51,6 +54,10 @@ public PaymentMethod(User user, String name, String address, String provider, St
this.createdAt = new Date();
}

public PaymentMethod.Delete toDelete() {
return new PaymentMethod.Delete(this.id);
}

@Data
public static class Create {
public UUID owner;
Expand All @@ -70,6 +77,8 @@ public static class Update {
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Delete {
public Long paymentMethodId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.*;

@RestController
@RequestMapping("/v1/payment-method")
Expand Down Expand Up @@ -108,27 +105,47 @@ public ResponseEntity<ApiResponse<PaymentMethod>> updatePaymentMethod(@RequestBo
}

@DeleteMapping
public ResponseEntity<ApiResponse<PaymentMethod>> deletePaymentMethod(@RequestBody PaymentMethod.Delete payload, HttpSession session) throws JsonProcessingException {
Optional<PaymentMethod> paymentMethod = paymentMethodRepository.findById(payload.getPaymentMethodId());
if (paymentMethod.isEmpty()) {
public ResponseEntity<ApiResponse<Map<String, List<?>>>> deletePaymentMethods(
@RequestBody List<PaymentMethod.Delete> payloads,
HttpSession session) throws JsonProcessingException {
if (payloads.size() == 0) {
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(new ApiResponse<>(404, "Provided payment-method doesn't exist"));
.status(HttpStatus.BAD_REQUEST)
.body(new ApiResponse<>(HttpStatus.BAD_REQUEST, "No payment-methods we're provided"));
}

Optional<User> sessionUser = AuthorizationInterceptor.getSessionUser(session);
if (sessionUser.isEmpty()) {
return AuthorizationInterceptor.noValidSessionResponse();
} else if (!sessionUser.get().getUuid().equals(paymentMethod.get().getOwner().getUuid())) {
return ResponseEntity
.status(HttpStatus.CONFLICT)
.body(new ApiResponse<>(HttpStatus.CONFLICT.value(), "You can't delete payment-methods from other users"));
List<PaymentMethod> successfullyDeleted = new ArrayList<>();
List<PaymentMethod.Delete> failedToDelete = new ArrayList<>();

for (PaymentMethod.Delete payload : payloads) {
Optional<PaymentMethod> optionalPaymentMethod = paymentMethodRepository.findById(payload.getPaymentMethodId());
if (optionalPaymentMethod.isEmpty()) {
failedToDelete.add(payload);
} else {
PaymentMethod paymentMethod = optionalPaymentMethod.get();
User paymentMethodOwner = paymentMethod.getOwner();
Optional<User> optSessionUser = AuthorizationInterceptor.getSessionUser(session);

if (optSessionUser.isEmpty()
|| !optSessionUser.get().getUuid().equals(paymentMethodOwner.getUuid())) {
failedToDelete.add(payload);
} else {
paymentMethodRepository.delete(paymentMethod);
successfullyDeleted.add(paymentMethod);
}
}
}

paymentMethodRepository.deleteById(payload.getPaymentMethodId());
Map<String, List<?>> response = new HashMap<>();
response.put("success", successfullyDeleted);
response.put("failed", failedToDelete);
boolean didAllFail = failedToDelete.size() == payloads.size();
return ResponseEntity
.status(HttpStatus.OK)
.body(new ApiResponse<>(200, paymentMethod.get()));
.status(didAllFail ? HttpStatus.BAD_REQUEST : HttpStatus.OK)
.body(new ApiResponse<>(
didAllFail ? HttpStatus.BAD_REQUEST : HttpStatus.OK,
didAllFail ? "All provided payment-methods we're invalid values" : null,
response));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import de.budgetbuddy.backend.user.User;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnDefault;

import java.util.Date;
Expand All @@ -14,6 +16,7 @@
@Entity
@Table(name = "subscription", schema = "public")
@Data
@Builder
@AllArgsConstructor
public class Subscription {

Expand Down Expand Up @@ -74,6 +77,11 @@ public static boolean isValidExecutionDate(int executeAt) {
return executeAt >= 1 && executeAt <=31;
}

public Subscription.Delete toDelete() {
return new Subscription.Delete(this.id);
}


@Data
public static class Create {
private UUID owner;
Expand All @@ -100,6 +108,8 @@ public static class Update {
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Delete {
private Long subscriptionId;
}
Expand Down
Loading

0 comments on commit d18eab5

Please sign in to comment.