-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPostEditBlock.java
More file actions
90 lines (76 loc) · 2.59 KB
/
Copy pathPostEditBlock.java
File metadata and controls
90 lines (76 loc) · 2.59 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 rsp.app.posts.components;
import rsp.app.posts.entities.Post;
import rsp.app.posts.services.PostService;
import rsp.component.ComponentView;
import rsp.component.Lookup;
import rsp.compositions.schema.DataSchema;
import rsp.compositions.block.EditBlock;
import rsp.compositions.block.FormMutationResult;
import rsp.compositions.block.PathParam;
import rsp.compositions.ui.EditView;
import java.util.Map;
import java.util.Objects;
/**
* Block for editing an existing post.
* <p>
* If shown via SHOW event, receives the post ID via show data.
* If routed via URL, loads the post by ID from the URL path (e.g., /posts/123).
* <p>
* For creating new posts, use {@link PostCreateBlock}.
*/
public class PostEditBlock extends EditBlock<Post> {
private static final PathParam<String> POST_ID = new PathParam<>(1, String.class, null);
private final PostService postService;
public PostEditBlock(PostService postService,
ComponentView<EditView.EditViewState, EditView.EditIntent> view) {
super(view);
this.postService = Objects.requireNonNull(postService);
}
@Override
public String title() {
return "Edit Post";
}
@Override
protected String resolveIdFromPath(Lookup lookup) {
return POST_ID.resolve(lookup);
}
@Override
public Post item(String postId) {
if (postId == null) {
return null;
}
return postService.find(postId).orElse(null);
}
@Override
public DataSchema schema() {
return CrudSchemas.POSTS;
}
@Override
public boolean save(Map<String, Object> fieldValues) {
String id = resolveId();
if (id == null || id.isEmpty()) {
return false; // Cannot save without ID
}
return postService.updateResult(id, post(id, fieldValues)).succeeded();
}
@Override
protected FormMutationResult saveResult(Map<String, Object> fieldValues) {
String id = resolveId();
if (id == null || id.isBlank()) return FormMutationResult.notFound("The post ID is missing.");
return postService.updateResult(id, post(id, fieldValues));
}
@Override
public boolean delete(String id) {
if (id == null || id.isEmpty()) {
return false;
}
return postService.delete(id);
}
@Override
protected FormMutationResult deleteResult(String id) {
return postService.deleteResult(id);
}
private Post post(String id, Map<String, Object> values) {
return new Post(id, (String) values.get("title"), (String) values.get("content"));
}
}