-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCommentEditBlock.java
More file actions
100 lines (85 loc) · 3.1 KB
/
Copy pathCommentEditBlock.java
File metadata and controls
100 lines (85 loc) · 3.1 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
package rsp.app.posts.components;
import rsp.app.posts.entities.Comment;
import rsp.app.posts.services.CommentService;
import rsp.app.posts.services.PostService;
import rsp.component.ComponentView;
import rsp.component.Lookup;
import rsp.compositions.block.EditBlock;
import rsp.compositions.block.FormMutationResult;
import rsp.compositions.block.PathParam;
import rsp.compositions.schema.DataSchema;
import rsp.compositions.schema.FieldChoice;
import rsp.compositions.schema.FieldDef;
import rsp.compositions.ui.EditView;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Block for editing an existing comment.
*/
public class CommentEditBlock extends EditBlock<Comment> {
private static final PathParam<String> COMMENT_ID = new PathParam<>(1, String.class, null);
private final CommentService commentService;
private final PostService postService;
public CommentEditBlock(final CommentService commentService,
final PostService postService,
ComponentView<EditView.EditViewState, EditView.EditIntent> view) {
super(view);
this.commentService = Objects.requireNonNull(commentService);
this.postService = Objects.requireNonNull(postService);
}
@Override
public String title() {
return "Edit Comment";
}
@Override
protected String resolveIdFromPath(Lookup lookup) {
return COMMENT_ID.resolve(lookup);
}
@Override
public Comment item(String commentId) {
if (commentId == null) {
return null;
}
return commentService.find(commentId).orElse(null);
}
@Override
public DataSchema schema() {
return CrudSchemas.COMMENTS;
}
@Override
public boolean save(Map<String, Object> fieldValues) {
String id = resolveId();
if (id == null || id.isEmpty()) {
return false;
}
return commentService.updateResult(id, comment(id, fieldValues)).succeeded();
}
@Override
protected FormMutationResult saveResult(Map<String, Object> fieldValues) {
String id = resolveId();
if (id == null || id.isBlank()) return FormMutationResult.notFound("The comment ID is missing.");
return commentService.updateResult(id, comment(id, fieldValues));
}
@Override
protected List<FieldChoice> fieldChoices(FieldDef field, Lookup lookup) {
if (!"postId".equals(field.name())) return super.fieldChoices(field, lookup);
return postService.findAllForSelection().stream()
.map(post -> new FieldChoice(post.id(), post.title()))
.toList();
}
@Override
public boolean delete(String id) {
if (id == null || id.isEmpty()) {
return false;
}
return commentService.delete(id);
}
@Override
protected FormMutationResult deleteResult(String id) {
return commentService.deleteResult(id);
}
private Comment comment(String id, Map<String, Object> values) {
return new Comment(id, (String) values.get("text"), (String) values.get("postId"));
}
}