-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathBook.java
More file actions
90 lines (69 loc) · 2.06 KB
/
Book.java
File metadata and controls
90 lines (69 loc) · 2.06 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
package example.borrow.domain;
import org.jmolecules.ddd.types.Identifier;
import org.jmolecules.ddd.types.ValueObject;
import java.util.UUID;
import jakarta.persistence.Embedded;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import jakarta.persistence.Version;
import lombok.Getter;
import lombok.NoArgsConstructor;
@SuppressWarnings("JpaDataSourceORMInspection")
@Entity
@NoArgsConstructor
@Table(name = "borrow_books", uniqueConstraints = @UniqueConstraint(columnNames = "barcode"))
@Getter
public class Book {
@EmbeddedId
private BookId id;
@Embedded
private Barcode inventoryNumber;
private String title;
private String isbn;
@Enumerated(EnumType.STRING)
private BookStatus status;
@SuppressWarnings("unused")
@Version
private Long version;
private Book(AddBook addBook) {
this.id = new BookId(UUID.randomUUID());
this.inventoryNumber = addBook.barcode();
this.title = addBook.title();
this.isbn = addBook.isbn();
this.status = BookStatus.AVAILABLE;
}
public static Book addBook(AddBook command) {
return new Book(command);
}
public Book markOnHold() {
this.status = BookStatus.ON_HOLD;
return this;
}
public Book markCheckedOut() {
this.status = BookStatus.ISSUED;
return this;
}
public Book markAvailable() {
this.status = BookStatus.AVAILABLE;
return this;
}
public record BookId(UUID id) implements Identifier {
}
public record Barcode(String barcode) implements ValueObject {
public static Barcode of(String barcode) {
return new Barcode(barcode);
}
}
public enum BookStatus implements ValueObject {
AVAILABLE, ON_HOLD, ISSUED
}
/**
* Command to add a new book
*/
public record AddBook(Barcode barcode, String title, String isbn) {
}
}