-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPostEditContract.java
More file actions
93 lines (81 loc) · 2.76 KB
/
PostEditContract.java
File metadata and controls
93 lines (81 loc) · 2.76 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
package rsp.app.posts.components;
import rsp.app.posts.entities.Post;
import rsp.app.posts.services.PostService;
import rsp.component.Lookup;
import rsp.compositions.schema.DataSchema;
import rsp.compositions.contract.EditViewContract;
import rsp.compositions.contract.PathParam;
import rsp.compositions.schema.FieldType;
import rsp.compositions.schema.Widget;
import java.util.Map;
import java.util.Objects;
/**
* PostEditContract - Contract 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 PostCreateContract}.
*/
public class PostEditContract extends EditViewContract<Post> {
private static final PathParam<String> POST_ID = new PathParam<>(1, String.class, null);
private final PostService postService;
public PostEditContract(final Lookup lookup, PostService postService) {
super(lookup);
this.postService = Objects.requireNonNull(postService);
}
@Override
public String title() {
return "Edit Post";
}
@Override
protected String resolveIdFromPath() {
// Extract ID from URL path parameter
// Parent class handles SHOW_DATA automatically
return resolve(POST_ID);
}
@Override
public Post item() {
String postId = resolveId();
// Return null if no ID available (e.g., when pre-instantiated as overlay)
if (postId == null) {
return null;
}
return postService.find(postId).orElse(null);
}
@Override
public DataSchema schema() {
return DataSchema.builder()
.field("id", FieldType.ID)
.hidden()
.field("title", FieldType.STRING)
.label("Post Title")
.required()
.maxLength(200)
.placeholder("Enter post title...")
.field("content", FieldType.TEXT)
.label("Content")
.widget(Widget.TEXTAREA)
.placeholder("Write your post content here...")
.build();
}
@Override
public boolean save(Map<String, Object> fieldValues) {
String id = resolveId();
if (id == null || id.isEmpty()) {
return false; // Cannot save without ID
}
String title = (String) fieldValues.get("title");
String content = (String) fieldValues.get("content");
Post post = new Post(id, title, content);
return postService.update(id, post);
}
@Override
public boolean delete() {
String id = resolveId();
if (id == null || id.isEmpty()) {
return false;
}
return postService.delete(id);
}
}