-
Notifications
You must be signed in to change notification settings - Fork 0
Features/loan #10
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
Features/loan #10
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
92dafa3
Refactor Loan entity: use relations instead of redundant ID:s
eafalkens 305af00
Create LoanServices class
eafalkens fcc8dd7
Add isBookLoaned method
eafalkens 3561cfc
Add loanBook method
eafalkens abd5d08
Update isBookLoaned to use Book relation
eafalkens 319e84d
Add getter and setter for loan relation in Book
eafalkens 5f1b6aa
Add returnBook method
eafalkens 74045ae
Add comments
eafalkens 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
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
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
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,76 @@ | ||
| package org.example; | ||
|
|
||
| import jakarta.persistence.EntityManager; | ||
| import java.time.ZonedDateTime; | ||
| import java.util.List; | ||
|
|
||
| public class LoanServices { | ||
|
|
||
| final private EntityManager em; | ||
|
|
||
| public LoanServices(EntityManager em) { | ||
| this.em = em; | ||
| } | ||
|
|
||
| // Kolla om en bok är utlånad | ||
| public boolean isBookLoaned(Long bookId) { | ||
|
|
||
| List<Loan> loans = em.createQuery( | ||
| "SELECT l FROM Loan l WHERE l.book.bookId = :bookId AND l.returnDate IS NULL", | ||
| Loan.class | ||
| ) | ||
| .setParameter("bookId", bookId) | ||
| .getResultList(); | ||
|
|
||
| if (loans.isEmpty()) { | ||
| return false; | ||
| } else { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| // Låna en bok | ||
| public boolean loanBook(Long bookId, Long userId) { | ||
|
|
||
| if (isBookLoaned(bookId)) { | ||
| return false; | ||
| } | ||
|
|
||
| User user = em.find(User.class, userId); | ||
| Book book = em.find(Book.class, bookId); | ||
|
|
||
| Loan loan = new Loan(); | ||
|
|
||
| loan.setUser(user); | ||
| loan.setBook(book); | ||
| loan.setLoanDate(ZonedDateTime.now()); | ||
| loan.setReturnDate(null); | ||
| em.persist(loan); | ||
| return true; | ||
| } | ||
|
|
||
| // Lämna tillbak en bok | ||
| public boolean returnBook(User user, Book book) { | ||
|
|
||
| Loan loan = em.createQuery( | ||
| "SELECT l FROM Loan l WHERE l.user = :user AND l.book = :book AND l.returnDate IS NULL", | ||
| Loan.class | ||
| ) | ||
| .setParameter("user", user) | ||
| .setParameter("book", book) | ||
| .getResultStream() | ||
| .findFirst() | ||
| .orElse(null); | ||
|
|
||
| if (loan == null) { | ||
| return false; | ||
| } | ||
|
|
||
| em.getTransaction().begin(); | ||
|
|
||
| loan.setReturnDate(ZonedDateTime.now()); | ||
| book.setLoan(null); | ||
| em.getTransaction().commit(); | ||
| return true; | ||
| } | ||
|
Comment on lines
+53
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing rollback handling and inconsistent API design.
🔧 Suggested fix with rollback handling public boolean returnBook(User user, Book book) {
Loan loan = em.createQuery(
"SELECT l FROM Loan l WHERE l.user = :user AND l.book = :book AND l.returnDate IS NULL",
Loan.class
)
.setParameter("user", user)
.setParameter("book", book)
.getResultStream()
.findFirst()
.orElse(null);
if (loan == null) {
return false;
}
- em.getTransaction().begin();
-
- loan.setReturnDate(ZonedDateTime.now());
- book.setLoan(null);
- em.getTransaction().commit();
- return true;
+ try {
+ em.getTransaction().begin();
+ loan.setReturnDate(ZonedDateTime.now());
+ book.setLoan(null);
+ em.getTransaction().commit();
+ return true;
+ } catch (Exception e) {
+ if (em.getTransaction().isActive()) {
+ em.getTransaction().rollback();
+ }
+ throw e;
+ }
}🤖 Prompt for AI Agents |
||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: Owning-side not updated, missing transaction and null checks.
Several issues in this method:
Bookis the owning side (@JoinColumn), so you must callbook.setLoan(loan)for the FK to be saved—loan.setBook(book)alone won't persist the association.em.persist()requires an active transaction.em.find()returnsnullif the entity doesn't exist—dereferencing will cause NPE.🐛 Proposed fix
public boolean loanBook(Long bookId, Long userId) { if (isBookLoaned(bookId)) { return false; } User user = em.find(User.class, userId); Book book = em.find(Book.class, bookId); + if (user == null || book == null) { + return false; + } + + em.getTransaction().begin(); + Loan loan = new Loan(); loan.setUser(user); loan.setBook(book); loan.setLoanDate(ZonedDateTime.now()); - loan.setReturnDate(null); + book.setLoan(loan); // Set owning side to persist the FK + em.persist(loan); + em.getTransaction().commit(); return true; }🤖 Prompt for AI Agents