-
Notifications
You must be signed in to change notification settings - Fork 1
/
Villain.java
84 lines (69 loc) · 1.81 KB
/
Villain.java
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
package io.quarkus.sample.superheroes.villain;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Random;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.Size;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
/**
* JPA entity class for a Villain. Re-used in the API layer.
*/
@Entity
public class Villain extends PanacheEntity {
@NotNull
@Size(min = 3, max = 50)
public String name;
public String otherName;
@NotNull
@Positive
public Integer level;
public String picture;
@Column(columnDefinition = "TEXT")
public String powers;
public static Optional<Villain> findRandom() {
var countVillains = count();
if (countVillains > 0) {
var randomVillain = new Random().nextInt((int) countVillains);
return findAll().page(randomVillain, 1).firstResultOptional();
}
return Optional.empty();
}
public static List<Villain> listAllWhereNameLike(String name) {
return (name != null) ?
list("LOWER(name) LIKE CONCAT('%', ?1, '%')", name.toLowerCase()) :
List.of();
}
@Override
/* prettier-ignore */
public String toString() {
return (
"Villain{" +
"id=" + this.id +
", name='" + this.name + '\'' +
", otherName='" + this.otherName + '\'' +
", level=" + this.level +
", picture='" + this.picture + '\'' +
", powers='" + this.powers + '\'' +
'}'
);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Villain villain = (Villain) o;
return this.id.equals(villain.id);
}
@Override
public int hashCode() {
return Objects.hash(this.id);
}
}