|
1 | 1 | package ru.javaops.topjava.user.model; |
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.annotation.JsonProperty; |
| 4 | +import io.swagger.v3.oas.annotations.media.Schema; |
4 | 5 | import jakarta.persistence.*; |
5 | 6 | import jakarta.validation.constraints.Email; |
6 | 7 | import jakarta.validation.constraints.NotBlank; |
|
11 | 12 | import lombok.NoArgsConstructor; |
12 | 13 | import lombok.Setter; |
13 | 14 | import org.jspecify.annotations.NonNull; |
| 15 | +import org.hibernate.annotations.OnDelete; |
| 16 | +import org.hibernate.annotations.OnDeleteAction; |
| 17 | +import org.hibernate.validator.constraints.Range; |
14 | 18 | import ru.javaops.topjava.common.HasIdAndEmail; |
15 | 19 | import ru.javaops.topjava.common.model.NamedEntity; |
16 | 20 | import ru.javaops.topjava.common.validation.NoHtml; |
17 | 21 |
|
18 | 22 | import java.util.*; |
19 | 23 |
|
| 24 | +import static ru.javaops.topjava.user.util.UsersUtil.DEFAULT_CALORIES_PER_DAY; |
| 25 | + |
20 | 26 | @Entity |
21 | 27 | @Table(name = "users") |
22 | 28 | @Getter |
@@ -55,18 +61,30 @@ public class User extends NamedEntity implements HasIdAndEmail { |
55 | 61 | @ElementCollection(fetch = FetchType.EAGER) |
56 | 62 | private Set<Role> roles = EnumSet.noneOf(Role.class); |
57 | 63 |
|
| 64 | + @Column(name = "calories_per_day", nullable = false, columnDefinition = "int default 2000") |
| 65 | + @Range(min = 10, max = 10000) |
| 66 | + private int caloriesPerDay = DEFAULT_CALORIES_PER_DAY; |
| 67 | + |
| 68 | + @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")//, cascade = CascadeType.REMOVE, orphanRemoval = true) |
| 69 | + @OrderBy("dateTime DESC") |
| 70 | + @OnDelete(action = OnDeleteAction.CASCADE) //https://stackoverflow.com/a/44988100/548473 |
| 71 | + @Schema(hidden = true) |
| 72 | + private List<Meal> meals; |
| 73 | + |
58 | 74 | public User(User u) { |
59 | | - this(u.id, u.name, u.email, u.password, u.enabled, u.registered, u.roles); |
| 75 | + this(u.id, u.name, u.email, u.password, u.caloriesPerDay, u.enabled, u.registered, u.roles); |
| 76 | + this.meals = List.copyOf(u.meals); |
60 | 77 | } |
61 | 78 |
|
62 | | - public User(Integer id, String name, String email, String password, Role... roles) { |
63 | | - this(id, name, email, password, true, new Date(), Arrays.asList(roles)); |
| 79 | + public User(Integer id, String name, String email, String password, int caloriesPerDay, Role... roles) { |
| 80 | + this(id, name, email, password, caloriesPerDay, true, new Date(), Arrays.asList(roles)); |
64 | 81 | } |
65 | 82 |
|
66 | | - public User(Integer id, String name, String email, String password, boolean enabled, Date registered, @NonNull Collection<Role> roles) { |
| 83 | + public User(Integer id, String name, String email, String password, int caloriesPerDay, boolean enabled, Date registered, @NonNull Collection<Role> roles) { |
67 | 84 | super(id, name); |
68 | 85 | this.email = email; |
69 | 86 | this.password = password; |
| 87 | + this.caloriesPerDay = caloriesPerDay; |
70 | 88 | this.enabled = enabled; |
71 | 89 | this.registered = registered; |
72 | 90 | setRoles(roles); |
|
0 commit comments