-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCirculationDesk.java
More file actions
102 lines (81 loc) · 3.46 KB
/
CirculationDesk.java
File metadata and controls
102 lines (81 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package example.borrow.application;
import org.springframework.modulith.events.ApplicationModuleListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
import java.util.UUID;
import example.borrow.domain.Book;
import example.borrow.domain.BookRepository;
import example.borrow.domain.Hold;
import example.borrow.domain.HoldRepository;
import example.catalog.BookAddedToCatalog;
@Service
@Transactional
public class CirculationDesk {
private final BookRepository books;
private final HoldRepository holds;
public CirculationDesk(BookRepository books, HoldRepository holds) {
this.books = books;
this.holds = holds;
}
public HoldInformation placeHold(Hold.PlaceHold command) {
books.findAvailableBook(command.inventoryNumber())
.orElseThrow(() -> new IllegalArgumentException("Book not found"));
var hold = Hold.placeHold(command)
.then(holds::save);
return HoldInformation.from(hold);
}
public Optional<HoldInformation> locate(UUID holdId) {
return holds.findById(new Hold.HoldId(holdId))
.map(HoldInformation::from);
}
public HoldInformation checkout(Hold.Checkout command) {
var hold = holds.findById(command.holdId())
.orElseThrow(() -> new IllegalArgumentException("Hold not found!"));
if (!hold.isHeldBy(command.patronId())) {
throw new IllegalArgumentException("Hold belongs to a different patron");
}
return HoldInformation.from(
hold.checkout(command)
.then(holds::save));
}
public HoldInformation checkin(Hold.Checkin command) {
var hold = holds.findById(command.holdId())
.orElseThrow(() -> new IllegalArgumentException("Hold not found!"));
if (!hold.isCheckedOut()) {
throw new IllegalArgumentException("Book is not checked out");
}
if (!hold.isHeldBy(command.patronId())) {
throw new IllegalArgumentException("Hold belongs to a different patron");
}
return HoldInformation.from(
hold.checkin(command)
.then(holds::save));
}
@ApplicationModuleListener
public void handle(Hold.BookCheckedIn event) {
books.findCheckedOutBook(new Book.Barcode(event.inventoryNumber()))
.map(Book::markAvailable)
.map(books::save)
.orElseThrow(() -> new IllegalArgumentException("Book not checked out?"));
}
@ApplicationModuleListener
public void handle(Hold.BookPlacedOnHold event) {
books.findAvailableBook(new Book.Barcode(event.inventoryNumber()))
.map(Book::markOnHold)
.map(books::save)
.orElseThrow(() -> new IllegalArgumentException("Duplicate hold?"));
}
@ApplicationModuleListener
public void handle(Hold.BookCheckedOut event) {
books.findOnHoldBook(new Book.Barcode(event.inventoryNumber()))
.map(Book::markCheckedOut)
.map(books::save)
.orElseThrow(() -> new IllegalArgumentException("Book not on hold?"));
}
@ApplicationModuleListener
public void handle(BookAddedToCatalog event) {
var command = new Book.AddBook(new Book.Barcode(event.inventoryNumber()), event.title(), event.isbn());
books.save(Book.addBook(command));
}
}