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

Commit

Permalink
feat: Add attribute attackedFiles to transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
tklein1801 committed Jan 24, 2024
1 parent d18eab5 commit e3e116c
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 48 deletions.
17 changes: 6 additions & 11 deletions src/main/java/de/budgetbuddy/backend/transaction/Transaction.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.budgetbuddy.backend.transaction;

import de.budgetbuddy.backend.category.Category;
import de.budgetbuddy.backend.paymentMethod.PaymentMethod;
import de.budgetbuddy.backend.transaction.file.TransactionFile;
import de.budgetbuddy.backend.user.User;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
Expand All @@ -10,6 +12,7 @@
import org.hibernate.annotations.ColumnDefault;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Entity
Expand Down Expand Up @@ -48,23 +51,15 @@ public class Transaction {
@Column(name = "transfer_amount", nullable = false)
private Double transferAmount;

@OneToMany(mappedBy = "transaction", cascade = CascadeType.ALL)
private List<TransactionFile> attachedFiles;

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

public Transaction() {}

public Transaction(User owner, de.budgetbuddy.backend.category.Category category, PaymentMethod paymentMethod, Date processedAt, String receiver, String description, Double transferAmount) {
this.owner = owner;
this.category = category;
this.paymentMethod = paymentMethod;
this.processedAt = processedAt;
this.receiver = receiver;
this.description = description;
this.transferAmount = transferAmount;
this.createdAt = new Date();
}

public Transaction.Delete toDelete() {
return new Transaction.Delete(this.id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import de.budgetbuddy.backend.ApiResponse;
import de.budgetbuddy.backend.auth.AuthorizationInterceptor;
import de.budgetbuddy.backend.category.Category;
import de.budgetbuddy.backend.category.CategoryRepository;
import de.budgetbuddy.backend.paymentMethod.PaymentMethod;
import de.budgetbuddy.backend.paymentMethod.PaymentMethodRepository;
Expand All @@ -29,7 +30,11 @@ public class TransactionController {
private final TransactionRepository transactionRepository;
private final TransactionService transactionService;

public TransactionController(UserRepository userRepository, CategoryRepository categoryRepository, PaymentMethodRepository paymentMethodRepository, SubscriptionRepository subscriptionRepository, TransactionRepository transactionRepository) {
public TransactionController(UserRepository userRepository,
CategoryRepository categoryRepository,
PaymentMethodRepository paymentMethodRepository,
SubscriptionRepository subscriptionRepository,
TransactionRepository transactionRepository) {
this.userRepository = userRepository;
this.categoryRepository = categoryRepository;
this.paymentMethodRepository = paymentMethodRepository;
Expand Down Expand Up @@ -63,35 +68,40 @@ public ResponseEntity<ApiResponse<List<Transaction>>> createTransaction(@Request
&& !sessionUser.getRole().isGreaterOrEqualThan(RolePermission.SERVICE_ACCOUNT)) {
return ResponseEntity
.status(HttpStatus.CONFLICT)
.body(new ApiResponse<>(HttpStatus.CONFLICT.value(), "You don't have the permissions to create transactions for a different user"));
.body(new ApiResponse<>(
HttpStatus.CONFLICT.value(),
"You don't have the permissions to create transactions for a different user"));
}

Optional<de.budgetbuddy.backend.category.Category> optCategory = categoryRepository
Optional<Category> optCategory = categoryRepository
.findByIdAndOwner(transactionAttrs.getCategoryId(), transactionOwner);
if (optCategory.isEmpty()) {
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(new ApiResponse<>(HttpStatus.NOT_FOUND.value(), "Provided category not found"));
.body(new ApiResponse<>(
HttpStatus.NOT_FOUND.value(),
"Provided category not found"));
}

Optional<PaymentMethod> optPaymentMethod = paymentMethodRepository.findByIdAndOwner(transactionAttrs.getPaymentMethodId(), transactionOwner);
Optional<PaymentMethod> optPaymentMethod = paymentMethodRepository
.findByIdAndOwner(transactionAttrs.getPaymentMethodId(), transactionOwner);
if (optPaymentMethod.isEmpty()) {
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(new ApiResponse<>(HttpStatus.NOT_FOUND.value(), "Provided payment-method not found"));
.body(new ApiResponse<>(
HttpStatus.NOT_FOUND.value(),
"Provided payment-method not found"));
}

Transaction transaction = new Transaction(
transactionOwner,
optCategory.get(),
optPaymentMethod.get(),
transactionAttrs.getProcessedAt(),
transactionAttrs.getReceiver(),
transactionAttrs.getDescription(),
transactionAttrs.getTransferAmount()
);

transactions.add(transaction);
transactions.add(Transaction.builder()
.owner(transactionOwner)
.category(optCategory.get())
.paymentMethod(optPaymentMethod.get())
.processedAt(transactionAttrs.getProcessedAt())
.receiver(transactionAttrs.getReceiver())
.description(transactionAttrs.getDescription())
.transferAmount(transactionAttrs.getTransferAmount())
.build());
}

return ResponseEntity
Expand All @@ -100,7 +110,8 @@ public ResponseEntity<ApiResponse<List<Transaction>>> createTransaction(@Request
}

@GetMapping
public ResponseEntity<ApiResponse<List<Transaction>>> getTransactionsByUuid(@RequestParam UUID uuid, HttpSession session) throws JsonProcessingException {
public ResponseEntity<ApiResponse<List<Transaction>>> getTransactionsByUuid(@RequestParam UUID uuid,
HttpSession session) throws JsonProcessingException {
Optional<User> user = userRepository.findById(uuid);
if (user.isEmpty()) {
return ResponseEntity
Expand Down Expand Up @@ -129,7 +140,8 @@ public ResponseEntity<ApiResponse<List<Transaction>>> getTransactionsByUuid(@Req
}

@PutMapping
public ResponseEntity<ApiResponse<Transaction>> updateTransaction(@RequestBody Transaction.Update payload, HttpSession session) throws JsonProcessingException {
public ResponseEntity<ApiResponse<Transaction>> updateTransaction(@RequestBody Transaction.Update payload,
HttpSession session) throws JsonProcessingException {
Optional<Transaction> optTransaction = transactionRepository.findById(payload.getTransactionId());
if (optTransaction.isEmpty()) {
return ResponseEntity
Expand All @@ -145,38 +157,44 @@ public ResponseEntity<ApiResponse<Transaction>> updateTransaction(@RequestBody T
} else if (!optSessionUser.get().getUuid().equals(transactionOwner.getUuid())) {
return ResponseEntity
.status(HttpStatus.CONFLICT)
.body(new ApiResponse<>(HttpStatus.CONFLICT.value(), "You don't own this transaction"));
.body(new ApiResponse<>(
HttpStatus.CONFLICT.value(),
"You don't own this transaction"));
}

Optional<de.budgetbuddy.backend.category.Category> optCategory = categoryRepository.findByIdAndOwner(payload.getCategoryId(), transactionOwner);
Optional<Category> optCategory = categoryRepository
.findByIdAndOwner(payload.getCategoryId(), transactionOwner);
if (optCategory.isEmpty()) {
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(new ApiResponse<>(HttpStatus.NOT_FOUND.value(), "Provided category not found"));
.body(new ApiResponse<>(
HttpStatus.NOT_FOUND.value(),
"Provided category not found"));
}

Optional<PaymentMethod> optPaymentMethod = paymentMethodRepository.findByIdAndOwner(payload.getPaymentMethodId(), transactionOwner);
Optional<PaymentMethod> optPaymentMethod = paymentMethodRepository
.findByIdAndOwner(payload.getPaymentMethodId(), transactionOwner);
if (optPaymentMethod.isEmpty()) {
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(new ApiResponse<>(HttpStatus.NOT_FOUND.value(), "Provided payment-method not found"));
.body(new ApiResponse<>(
HttpStatus.NOT_FOUND.value(),
"Provided payment-method not found"));
}

Transaction updatedTransaction = new Transaction(
transaction.getId(),
transactionOwner,
optCategory.get(),
optPaymentMethod.get(),
payload.getProcessedAt(),
payload.getReceiver(),
payload.getDescription(),
payload.getTransferAmount(),
transaction.getCreatedAt()
);

return ResponseEntity
.status(HttpStatus.OK)
.body(new ApiResponse<>(transactionRepository.save(updatedTransaction)));
.body(new ApiResponse<>(transactionRepository.save(Transaction.builder()
.id(transaction.getId())
.owner(transactionOwner)
.category(optCategory.get())
.paymentMethod(optPaymentMethod.get())
.processedAt(payload.getProcessedAt())
.receiver(payload.getReceiver())
.description(payload.getDescription())
.transferAmount(payload.getTransferAmount())
.createdAt(transaction.getCreatedAt())
.build())));
}

@DeleteMapping
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package de.budgetbuddy.backend.transaction.file;

import com.fasterxml.jackson.annotation.JsonIgnore;
import de.budgetbuddy.backend.transaction.Transaction;
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;
import java.util.UUID;

@Entity
@Table(name = "transaction_file", schema = "public")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TransactionFile {

@Id
@GeneratedValue
@Column(columnDefinition = "uuid", updatable = false, nullable = false)
private UUID uuid;

@JsonIgnore
@ManyToOne
@JoinColumn(name = "owner")
private User owner;

@JsonIgnore
@ManyToOne
@JoinColumn(name = "transaction")
private Transaction transaction;

@Column(name = "file_name")
private String fileName;

@Column(name = "file_size")
private int fileSize;

@Column(name = "mimetype", length = 20)
private String mimeType;

@Column(name = "location", length = 100)
private String location;

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package de.budgetbuddy.backend.transaction.file;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.UUID;

public interface TransactionFileRepository extends JpaRepository<TransactionFile, UUID> {
}

0 comments on commit e3e116c

Please sign in to comment.