-
Notifications
You must be signed in to change notification settings - Fork 0
add update transaction and paginated fetch endpoints #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ab64ae7
add update transaction and paginated fetch endpoints
adelekeemmanuel b268dcf
use DTOs, filters and JPA specifications for transactions
adelekeemmanuel 8eaaf02
refactor transactions to use DTOs and inline specifications
adelekeemmanuel 251cb14
Merge branch 'main' into feature/update-and-fetch-transactions
adelekeemmanuel ac2c03d
Resolved all conflict
adelekeemmanuel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/main/java/com/SkillsForge/expensetracker/app/filter/TransactionFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.SkillsForge.expensetracker.app.filter; | ||
|
|
||
| import com.SkillsForge.expensetracker.app.enums.TransactionCategory; | ||
| import com.SkillsForge.expensetracker.app.enums.TransactionType; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| public class TransactionFilter { | ||
| private TransactionCategory category; | ||
| private TransactionType type; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/com/SkillsForge/expensetracker/dto/TransactionUpdateRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.SkillsForge.expensetracker.dto; | ||
|
|
||
| import com.SkillsForge.expensetracker.app.enums.TransactionCategory; | ||
| import com.SkillsForge.expensetracker.app.enums.TransactionType; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| import java.time.LocalDate; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| public class TransactionUpdateRequest { | ||
|
|
||
| private String description; | ||
| private TransactionCategory category; | ||
| private TransactionType type; | ||
| private Long amount; | ||
| private LocalDate date; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 12 additions & 1 deletion
13
src/main/java/com/SkillsForge/expensetracker/service/TransactionService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,21 @@ | ||
| package com.SkillsForge.expensetracker.service; | ||
|
|
||
| import com.SkillsForge.expensetracker.dto.CreateTransactionRequest; | ||
| import com.SkillsForge.expensetracker.dto.TransactionDto; | ||
| import com.SkillsForge.expensetracker.dto.TransactionUpdateRequest; | ||
| import com.SkillsForge.expensetracker.app.filter.TransactionFilter; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
|
|
||
| import com.SkillsForge.expensetracker.dto.CreateTransactionRequest; | ||
|
|
||
| public interface TransactionService { | ||
| TransactionDto createTransaction(CreateTransactionRequest request); | ||
|
|
||
| TransactionDto getTransactionById(Long id); | ||
|
|
||
| TransactionDto updateTransaction(Long id, TransactionUpdateRequest request); | ||
|
|
||
| Page<TransactionDto> getAllTransactions(TransactionFilter filter, Pageable pageable); | ||
|
|
||
|
|
||
| } |
117 changes: 86 additions & 31 deletions
117
src/main/java/com/SkillsForge/expensetracker/service/TransactionServiceImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,49 +1,104 @@ | ||
| package com.SkillsForge.expensetracker.service; | ||
|
|
||
| import com.SkillsForge.expensetracker.app.filter.TransactionFilter; | ||
| import com.SkillsForge.expensetracker.dto.CreateTransactionRequest; | ||
| import com.SkillsForge.expensetracker.dto.TransactionDto; | ||
| import com.SkillsForge.expensetracker.dto.TransactionUpdateRequest; | ||
| import com.SkillsForge.expensetracker.exception.ResourceNotFoundException; | ||
| import com.SkillsForge.expensetracker.persistence.entity.Transaction; | ||
| import com.SkillsForge.expensetracker.persistence.repository.TransactionRepository; | ||
| import java.time.LocalDateTime; | ||
| import jakarta.persistence.criteria.Predicate; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.jpa.domain.Specification; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| @Service | ||
| @Slf4j | ||
| @RequiredArgsConstructor | ||
| public class TransactionServiceImpl implements TransactionService { | ||
|
|
||
| private final TransactionRepository transactionRepository; | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public TransactionDto createTransaction(CreateTransactionRequest request) { | ||
| LocalDateTime now = LocalDateTime.now(); | ||
|
|
||
| Transaction transaction = new Transaction(request); | ||
| transaction.setCreatedAt(now); | ||
| transaction.setUpdatedAt(now); | ||
|
|
||
| Transaction savedTransaction = transactionRepository.save(transaction); | ||
| log.info("Transaction created successfully with ID: {}", savedTransaction.getId()); | ||
| return TransactionDto.fromEntity(savedTransaction); | ||
| } | ||
|
|
||
| @Override | ||
| @Transactional(readOnly = true) | ||
| public TransactionDto getTransactionById(Long id) { | ||
| log.info("Fetching transaction with ID: {}", id); | ||
|
|
||
| return TransactionDto.fromEntity( | ||
| transactionRepository | ||
| .findById(id) | ||
| .orElseThrow( | ||
| () -> { | ||
| log.error("Transaction not found with ID: {}", id); | ||
| return new ResourceNotFoundException("Transaction not found with ID: " + id); | ||
| })); | ||
| } | ||
| private final TransactionRepository transactionRepository; | ||
|
|
||
| // ================= CREATE ================= | ||
| @Override | ||
| @Transactional | ||
| public TransactionDto createTransaction(CreateTransactionRequest request) { | ||
| LocalDateTime now = LocalDateTime.now(); | ||
|
|
||
| Transaction transaction = new Transaction(request); | ||
| transaction.setCreatedAt(now); | ||
| transaction.setUpdatedAt(now); | ||
|
|
||
| Transaction saved = transactionRepository.save(transaction); | ||
| log.info("Transaction created successfully with ID: {}", saved.getId()); | ||
|
|
||
| return TransactionDto.fromEntity(saved); | ||
| } | ||
|
|
||
| // ================= GET BY ID ================= | ||
| @Override | ||
| @Transactional(readOnly = true) | ||
| public TransactionDto getTransactionById(Long id) { | ||
| log.info("Fetching transaction with ID: {}", id); | ||
|
|
||
| Transaction transaction = transactionRepository.findById(id) | ||
| .orElseThrow(() -> | ||
| new ResourceNotFoundException("Transaction not found with ID: " + id)); | ||
|
|
||
| return TransactionDto.fromEntity(transaction); | ||
| } | ||
|
|
||
| // ================= UPDATE ================= | ||
| @Override | ||
| @Transactional | ||
| public TransactionDto updateTransaction(Long id, TransactionUpdateRequest request) { | ||
|
|
||
| Transaction existing = transactionRepository.findById(id) | ||
| .orElseThrow(() -> | ||
| new ResourceNotFoundException("Transaction not found with ID: " + id)); | ||
|
|
||
| existing.setDescription(request.getDescription()); | ||
| existing.setCategory(request.getCategory()); | ||
| existing.setType(request.getType()); | ||
| existing.setAmount(request.getAmount()); | ||
| existing.setDate(request.getDate()); | ||
| existing.setUpdatedAt(LocalDateTime.now()); | ||
|
|
||
| Transaction saved = transactionRepository.save(existing); | ||
| log.info("Transaction updated successfully with ID: {}", saved.getId()); | ||
|
|
||
| return TransactionDto.fromEntity(saved); | ||
| } | ||
|
|
||
| // ================= GET ALL (FILTERED) ================= | ||
| @Override | ||
| @Transactional(readOnly = true) | ||
| public Page<TransactionDto> getAllTransactions(TransactionFilter filter, Pageable pageable) { | ||
|
|
||
| Specification<Transaction> spec = (root, query, cb) -> { | ||
| List<Predicate> predicates = new ArrayList<>(); | ||
|
|
||
| if (filter.getCategory() != null) { | ||
| predicates.add(cb.equal(root.get("category"), filter.getCategory())); | ||
| } | ||
|
|
||
| if (filter.getType() != null) { | ||
| predicates.add(cb.equal(root.get("type"), filter.getType())); | ||
| } | ||
|
|
||
| return cb.and(predicates.toArray(new Predicate[0])); | ||
| }; | ||
|
|
||
| return transactionRepository | ||
| .findAll(spec, pageable) | ||
| .map(TransactionDto::fromEntity); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.