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

Commit

Permalink
feat: Add ability to link files to transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
tklein1801 committed Feb 4, 2024
1 parent 7864d43 commit 581599d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
2 changes: 0 additions & 2 deletions src/main/java/de/budgetbuddy/backend/log/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class Logger {
private static Logger instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public static class Update {
private String receiver;
private String description;
private Double transferAmount;
private ArrayList<TransactionFile.Create> attachedFiles = new ArrayList<>();
}

@Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import de.budgetbuddy.backend.paymentMethod.PaymentMethod;
import de.budgetbuddy.backend.paymentMethod.PaymentMethodRepository;
import de.budgetbuddy.backend.subscription.SubscriptionRepository;
import de.budgetbuddy.backend.transaction.file.TransactionFile;
import de.budgetbuddy.backend.transaction.file.TransactionFileRepository;
import de.budgetbuddy.backend.user.User;
import de.budgetbuddy.backend.user.UserRepository;
import de.budgetbuddy.backend.user.role.RolePermission;
Expand All @@ -28,18 +30,21 @@ public class TransactionController {
private final PaymentMethodRepository paymentMethodRepository;
private final SubscriptionRepository subscriptionRepository;
private final TransactionRepository transactionRepository;
private final TransactionFileRepository transactionFileRepository;
private final TransactionService transactionService;

public TransactionController(UserRepository userRepository,
CategoryRepository categoryRepository,
PaymentMethodRepository paymentMethodRepository,
SubscriptionRepository subscriptionRepository,
TransactionRepository transactionRepository) {
TransactionRepository transactionRepository,
TransactionFileRepository transactionFileRepository) {
this.userRepository = userRepository;
this.categoryRepository = categoryRepository;
this.paymentMethodRepository = paymentMethodRepository;
this.subscriptionRepository = subscriptionRepository;
this.transactionRepository = transactionRepository;
this.transactionFileRepository = transactionFileRepository;
this.transactionService = new TransactionService(transactionRepository);
}

Expand Down Expand Up @@ -185,6 +190,21 @@ public ResponseEntity<ApiResponse<Transaction>> updateTransaction(@RequestBody T
"Provided payment-method not found"));
}

List<TransactionFile> transactionFiles = new ArrayList<>();
if (!payload.getAttachedFiles().isEmpty()) {
for (TransactionFile.Create file : payload.getAttachedFiles()) {
transactionFiles.add(TransactionFile.builder()
.owner(transactionOwner)
.transaction(transaction)
.fileName(file.getFileName())
.fileSize(file.getFileSize())
.mimeType(file.getMimeType())
.location(file.getFileUrl())
.build());
}
transactionFileRepository.saveAll(transactionFiles);
}

return ResponseEntity
.status(HttpStatus.OK)
.body(new ApiResponse<>(transactionRepository.save(Transaction.builder()
Expand All @@ -196,7 +216,7 @@ public ResponseEntity<ApiResponse<Transaction>> updateTransaction(@RequestBody T
.receiver(payload.getReceiver())
.description(payload.getDescription())
.transferAmount(payload.getTransferAmount())
.attachedFiles(transaction.getAttachedFiles())
.attachedFiles(transactionFiles)
.createdAt(transaction.getCreatedAt())
.build())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,17 @@ public class TransactionFile {
@Column(name = "location", length = 100)
private String location;

@Builder.Default
@Column(name = "created_at", nullable = false, updatable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
@ColumnDefault("CURRENT_TIMESTAMP")
private Date createdAt = new Date();

@Data
public static class Create {
private String fileName;
private int fileSize;
private String mimeType;
private String fileUrl;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import de.budgetbuddy.backend.paymentMethod.PaymentMethod;
import de.budgetbuddy.backend.paymentMethod.PaymentMethodRepository;
import de.budgetbuddy.backend.subscription.SubscriptionRepository;
import de.budgetbuddy.backend.transaction.file.TransactionFileRepository;
import de.budgetbuddy.backend.user.User;
import de.budgetbuddy.backend.user.UserRepository;
import de.budgetbuddy.backend.user.role.Role;
Expand Down Expand Up @@ -37,6 +38,7 @@ public class TransactionControllerTests {
private final CategoryRepository categoryRepository;
private final PaymentMethodRepository paymentMethodRepository;
private final TransactionRepository transactionRepository;
private final TransactionFileRepository transactionFileRepository;
private final TransactionService transactionService;
private final TransactionController transactionController;
private final ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
Expand All @@ -48,7 +50,14 @@ public class TransactionControllerTests {
this.paymentMethodRepository = Mockito.mock(PaymentMethodRepository.class);
SubscriptionRepository subscriptionRepository = Mockito.mock(SubscriptionRepository.class);
this.transactionRepository = Mockito.mock(TransactionRepository.class);
this.transactionController = new TransactionController(userRepository, categoryRepository, paymentMethodRepository, subscriptionRepository, transactionRepository);
this.transactionFileRepository = Mockito.mock(TransactionFileRepository.class);
this.transactionController = new TransactionController(
userRepository,
categoryRepository,
paymentMethodRepository,
subscriptionRepository,
transactionRepository,
transactionFileRepository);
this.transactionService = new TransactionService(transactionRepository);
}

Expand Down

0 comments on commit 581599d

Please sign in to comment.