|
18 | 18 |
|
19 | 19 | package com.navercorp.spring.data.jdbc.plus.sql.guide.board; |
20 | 20 |
|
21 | | -import java.util.ArrayList; |
22 | | -import java.util.HashMap; |
23 | | -import java.util.HashSet; |
| 21 | +import static java.util.Comparator.comparing; |
| 22 | +import static java.util.stream.Collectors.toCollection; |
| 23 | + |
| 24 | +import java.util.LinkedHashSet; |
24 | 25 | import java.util.List; |
25 | 26 | import java.util.Map; |
26 | 27 | import java.util.Set; |
|
34 | 35 | import org.springframework.data.relational.core.mapping.Table; |
35 | 36 |
|
36 | 37 | import lombok.Builder; |
37 | | -import lombok.Getter; |
38 | | -import lombok.Value; |
39 | | -import lombok.With; |
40 | 38 |
|
41 | 39 | import com.navercorp.spring.jdbc.plus.commons.annotations.SqlTableAlias; |
42 | 40 |
|
43 | 41 | /** |
44 | 42 | * @author Myeonghyeon Lee |
45 | 43 | */ |
| 44 | +@Builder(toBuilder = true) |
46 | 45 | @Table("n_board") |
47 | | -@Getter |
48 | | -@Builder |
49 | | -public class Board { |
| 46 | +public record Board( |
50 | 47 | @Id |
51 | | - private Long id; |
| 48 | + Long id, |
52 | 49 |
|
53 | | - private String name; |
| 50 | + String name, |
54 | 51 |
|
55 | 52 | @MappedCollection(idColumn = "board_id") |
56 | | - @Builder.Default |
57 | | - private Set<Label> labels = new HashSet<>(); |
| 53 | + @Nullable Set<Label> labels, |
58 | 54 |
|
59 | 55 | @MappedCollection(idColumn = "board_id", keyColumn = "board_index") |
60 | | - @Builder.Default |
61 | | - private List<Post> posts = new ArrayList<>(); |
| 56 | + @Nullable List<Post> posts, |
62 | 57 |
|
63 | 58 | @SqlTableAlias("b_audit") |
64 | 59 | @Column("board_id") |
65 | | - private @Nullable Audit audit; |
| 60 | + @Nullable Audit audit, |
66 | 61 |
|
67 | 62 | @Embedded.Nullable(prefix = "board_") |
68 | | - private @Nullable Memo memo; |
| 63 | + @Nullable Memo memo, |
69 | 64 |
|
70 | 65 | @MappedCollection(idColumn = "board_id", keyColumn = "config_key") |
71 | | - @Builder.Default |
72 | | - private @Nullable Map<String, Config> configMap = new HashMap<>(); |
| 66 | + @Nullable Map<String, Config> configMap |
| 67 | +) { |
| 68 | + public Board { |
| 69 | + labels = labels == null ? Set.of() : labels; |
| 70 | + posts = posts == null ? List.of() : posts; |
| 71 | + configMap = configMap == null ? Map.of() : configMap; |
| 72 | + } |
| 73 | + |
| 74 | + public Board sort() { |
| 75 | + return toBuilder() |
| 76 | + .labels(labels.stream() |
| 77 | + .sorted(comparing(label -> label.id().title())) |
| 78 | + .collect(toCollection(LinkedHashSet::new))) |
| 79 | + .posts(posts.stream() |
| 80 | + .map(Post::sort) |
| 81 | + .sorted(comparing(Post::id)) |
| 82 | + .toList()) |
| 83 | + .audit(audit != null ? audit.sort() : null) |
| 84 | + .build(); |
| 85 | + } |
73 | 86 |
|
| 87 | + @Builder(toBuilder = true) |
74 | 88 | @Table("n_label") |
75 | | - @Getter |
76 | | - @Builder |
77 | | - public static class Label implements Persistable<LabelId> { |
| 89 | + public record Label( |
78 | 90 | @Id |
79 | 91 | @Nullable |
80 | 92 | @Embedded.Empty |
81 | | - private LabelId id; |
| 93 | + LabelId id, |
82 | 94 |
|
83 | | - private String name; |
| 95 | + String name |
| 96 | + ) implements Persistable<LabelId> { |
| 97 | + @Override |
| 98 | + public @Nullable LabelId getId() { |
| 99 | + return id; |
| 100 | + } |
84 | 101 |
|
85 | 102 | @Override |
86 | 103 | public boolean isNew() { |
87 | 104 | return false; |
88 | 105 | } |
89 | 106 | } |
90 | 107 |
|
91 | | - @Value |
92 | | - @Builder |
93 | | - public static class LabelId { |
| 108 | + @Builder(toBuilder = true) |
| 109 | + public record LabelId( |
94 | 110 | @Column("title") |
95 | | - String title; |
| 111 | + String title, |
96 | 112 |
|
97 | 113 | @Column("project_name") |
98 | | - String projectName; |
| 114 | + String projectName |
| 115 | + ) { |
99 | 116 | } |
100 | 117 |
|
| 118 | + @Builder(toBuilder = true) |
101 | 119 | @Table("n_post") |
102 | | - @Getter |
103 | | - @Builder |
104 | | - public static class Post { |
| 120 | + public record Post( |
105 | 121 | @Id |
106 | | - private Long id; |
| 122 | + Long id, |
107 | 123 |
|
108 | | - private Long postNo; |
| 124 | + Long postNo, |
109 | 125 |
|
110 | | - private String title; |
| 126 | + String title, |
111 | 127 |
|
112 | | - private String content; |
| 128 | + String content, |
113 | 129 |
|
114 | 130 | @MappedCollection(idColumn = "post_id") |
115 | | - @Builder.Default |
116 | | - private Set<Tag> tags = new HashSet<>(); |
| 131 | + @Nullable Set<Tag> tags, |
117 | 132 |
|
118 | 133 | @MappedCollection(idColumn = "post_id", keyColumn = "post_index") |
119 | | - @Builder.Default |
120 | | - private List<Comment> comments = new ArrayList<>(); |
| 134 | + @Nullable List<Comment> comments, |
121 | 135 |
|
122 | 136 | @Column("post_id") |
123 | | - private @Nullable Audit audit; |
| 137 | + @Nullable Audit audit, |
124 | 138 |
|
125 | 139 | @Embedded.Nullable |
126 | | - private @Nullable Memo memo; |
| 140 | + @Nullable Memo memo, |
127 | 141 |
|
128 | 142 | @MappedCollection(idColumn = "post_id", keyColumn = "config_key") |
129 | | - @Builder.Default |
130 | | - private @Nullable Map<String, Config> configMap = new HashMap<>(); |
| 143 | + @Nullable Map<String, Config> configMap |
| 144 | + ) { |
| 145 | + public Post { |
| 146 | + tags = tags == null ? Set.of() : tags; |
| 147 | + comments = comments == null ? List.of() : comments; |
| 148 | + configMap = configMap == null ? Map.of() : configMap; |
| 149 | + } |
| 150 | + |
| 151 | + public Post sort() { |
| 152 | + return toBuilder() |
| 153 | + .tags(tags.stream() |
| 154 | + .sorted(comparing(Tag::id)) |
| 155 | + .collect(toCollection(LinkedHashSet::new))) |
| 156 | + .comments(comments.stream() |
| 157 | + .map(Comment::sort) |
| 158 | + .sorted(comparing(Comment::id)) |
| 159 | + .toList()) |
| 160 | + .audit(audit != null ? audit.sort() : null) |
| 161 | + .build(); |
| 162 | + } |
131 | 163 | } |
132 | 164 |
|
| 165 | + @Builder(toBuilder = true) |
133 | 166 | @Table("n_tag") |
134 | | - @Value |
135 | | - @Builder |
136 | | - public static class Tag { |
| 167 | + public record Tag( |
137 | 168 | @Id |
138 | | - @With |
139 | | - Long id; |
| 169 | + Long id, |
140 | 170 |
|
141 | | - String content; |
| 171 | + String content |
| 172 | + ) { |
142 | 173 | } |
143 | 174 |
|
| 175 | + @Builder(toBuilder = true) |
144 | 176 | @Table("n_comment") |
145 | | - @Getter |
146 | | - @Builder |
147 | | - public static class Comment { |
| 177 | + public record Comment( |
148 | 178 | @Id |
149 | | - private Long id; |
| 179 | + Long id, |
150 | 180 |
|
151 | | - private String content; |
| 181 | + String content, |
152 | 182 |
|
153 | 183 | @Column("comment_id") |
154 | | - private @Nullable Audit audit; |
| 184 | + @Nullable Audit audit |
| 185 | + ) { |
| 186 | + public Comment sort() { |
| 187 | + return toBuilder() |
| 188 | + .audit(audit != null ? audit.sort() : null) |
| 189 | + .build(); |
| 190 | + } |
155 | 191 | } |
156 | 192 |
|
| 193 | + @Builder(toBuilder = true) |
157 | 194 | @Table("n_audit") |
158 | | - @Getter |
159 | | - @Builder |
160 | | - public static class Audit { |
| 195 | + public record Audit( |
161 | 196 | @Id |
162 | | - private Long id; |
| 197 | + Long id, |
163 | 198 |
|
164 | | - private String name; |
| 199 | + String name, |
165 | 200 |
|
166 | 201 | @Embedded.Nullable |
167 | | - private @Nullable Memo memo; |
| 202 | + @Nullable Memo memo, |
168 | 203 |
|
169 | 204 | @Column("audit_id") |
170 | | - private @Nullable AuditSecret secret; |
| 205 | + @Nullable AuditSecret secret |
| 206 | + ) { |
| 207 | + public Audit sort() { |
| 208 | + return this; |
| 209 | + } |
171 | 210 | } |
172 | 211 |
|
173 | | - @Value |
174 | | - @Builder |
175 | | - public static class Memo { |
176 | | - String memo; |
| 212 | + @Builder(toBuilder = true) |
| 213 | + public record Memo( |
| 214 | + String memo |
| 215 | + ) { |
177 | 216 | } |
178 | 217 |
|
| 218 | + @Builder(toBuilder = true) |
179 | 219 | @Table("n_audit_secret") |
180 | | - @Value |
181 | | - @Builder |
182 | | - public static class AuditSecret { |
| 220 | + public record AuditSecret( |
183 | 221 | @Id |
184 | | - @With |
185 | | - Long id; |
| 222 | + Long id, |
186 | 223 |
|
187 | | - String secret; |
| 224 | + String secret |
| 225 | + ) { |
188 | 226 | } |
189 | 227 |
|
| 228 | + @Builder(toBuilder = true) |
190 | 229 | @Table("n_config") |
191 | | - @Value |
192 | | - @Builder |
193 | | - public static class Config { |
| 230 | + public record Config( |
194 | 231 | @Id |
195 | | - @With |
196 | | - Long id; |
| 232 | + Long id, |
197 | 233 |
|
198 | | - String configKey; |
| 234 | + String configKey, |
199 | 235 |
|
200 | | - String configValue; |
| 236 | + String configValue |
| 237 | + ) { |
201 | 238 | } |
202 | 239 | } |
0 commit comments