-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPostService.java
More file actions
202 lines (174 loc) · 8.02 KB
/
Copy pathPostService.java
File metadata and controls
202 lines (174 loc) · 8.02 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package rsp.app.posts.services;
import rsp.app.posts.entities.Post;
import rsp.component.ContextKey;
import rsp.compositions.block.DeleteResult;
import rsp.compositions.block.FormMutationResult;
import rsp.compositions.block.ListPage;
import rsp.compositions.block.ListQuery;
import rsp.compositions.block.SortDirection;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class PostService {
/**
* Post service for CRUD operations on posts.
* Stored as: PostService.class → PostService instance
*/
public static final ContextKey.ClassKey<PostService> POST_SERVICE =
new ContextKey.ClassKey<>(PostService.class);
private final Map<String, Post> posts = new ConcurrentHashMap<>();
private final AtomicInteger idGenerator = new AtomicInteger(1);
private volatile Consumer<String> deleteListener = _ -> { };
public PostService() {
// Pre-populate with dummy data
for (int i = 1; i <= 25; i++) {
create(new Post(null, "Post Title " + i, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " + i));
}
}
public List<Post> findAll(final int page, final int pageSize, final String sort) {
return findAll(new ListQuery(Math.max(1, page), pageSize,
new rsp.compositions.block.SortSpec("title",
SortDirection.parse(sort, SortDirection.ASC)), "", Map.of())).items();
}
public ListPage<Post> findAll(final ListQuery query) {
Comparator<Post> comparator = comparator(query);
List<Post> matching = posts.values().stream()
.filter(post -> matchesSearch(post, query.search()))
.filter(post -> matchesFilters(post, query.filters()))
.sorted(comparator)
.collect(Collectors.toList());
long requestedOffset = (long) (query.page() - 1) * query.pageSize();
int from = (int) Math.min(requestedOffset, matching.size());
int to = Math.min(from + query.pageSize(), matching.size());
return new ListPage<>(matching.subList(from, to), matching.size());
}
public Optional<Post> find(final String id) {
return Optional.ofNullable(posts.get(id));
}
/** Return the bounded demo data set in deterministic label order for reference selectors. */
public List<Post> findAllForSelection() {
return posts.values().stream()
.sorted(Comparator.comparing(Post::title, String.CASE_INSENSITIVE_ORDER)
.thenComparingInt(post -> numericId(post.id())))
.toList();
}
public boolean exists(final String id) {
return id != null && posts.containsKey(id);
}
/** Register demo-domain cleanup performed after a post is deleted. */
public PostService onDelete(Consumer<String> listener) {
this.deleteListener = Objects.requireNonNull(listener, "listener");
return this;
}
public String create(final Post post) {
FormMutationResult result = createResult(post);
if (!result.succeeded()) throw new IllegalArgumentException(result.message());
return result.entityId();
}
public FormMutationResult createResult(final Post post) {
FormMutationResult invalid = validate(post);
if (invalid != null) return invalid;
String id = String.valueOf(idGenerator.getAndIncrement());
Post newPost = new Post(id, post.title(), Objects.requireNonNullElse(post.content(), ""));
posts.put(id, newPost);
return FormMutationResult.saved(id, "Post created.");
}
public boolean update(final String id, final Post post) {
return updateResult(id, post).succeeded();
}
public FormMutationResult updateResult(final String id, final Post post) {
if (!exists(id)) return FormMutationResult.notFound("The post no longer exists.");
FormMutationResult invalid = validate(post);
if (invalid != null) return invalid;
posts.put(id, new Post(id, post.title(), Objects.requireNonNullElse(post.content(), "")));
return FormMutationResult.saved(id, "Post updated.");
}
public boolean delete(final String id) {
if (id == null || posts.remove(id) == null) return false;
deleteListener.accept(id);
return true;
}
public FormMutationResult deleteResult(final String id) {
return delete(id)
? FormMutationResult.saved(id, "Post deleted.")
: FormMutationResult.notFound("The post no longer exists.");
}
/**
* Find a post by its title (case-insensitive).
*
* @param title the title to search for
* @return the matching post, or empty if not found
*/
public Optional<Post> findByTitle(final String title) {
return posts.values().stream()
.filter(p -> p.title().equalsIgnoreCase(title))
.findFirst();
}
public int bulkDelete(final Set<String> ids) {
return deleteAll(ids).deletedIds().size();
}
public DeleteResult deleteAll(final Set<String> ids) {
Set<String> deleted = new LinkedHashSet<>();
Set<String> failed = new LinkedHashSet<>();
for (String id : ids) {
if (delete(id)) {
deleted.add(id);
} else {
failed.add(id);
}
}
return new DeleteResult(deleted, failed);
}
private Comparator<Post> comparator(ListQuery query) {
String field = query.sort() == null ? "title" : query.sort().field();
Comparator<Post> comparator = switch (field) {
case "id" -> Comparator.comparingInt(post -> numericId(post.id()));
case "title" -> Comparator.comparing(Post::title, String.CASE_INSENSITIVE_ORDER);
case "content" -> Comparator.comparing(Post::content, String.CASE_INSENSITIVE_ORDER);
default -> throw new IllegalArgumentException("Unsupported post sort field: " + field);
};
if (query.sort() != null && query.sort().direction() == SortDirection.DESC) {
comparator = comparator.reversed();
}
return comparator.thenComparing(Post::id, String.CASE_INSENSITIVE_ORDER);
}
private static boolean matchesSearch(Post post, String search) {
if (search == null || search.isBlank()) return true;
String needle = search.toLowerCase(Locale.ROOT);
return post.id().toLowerCase(Locale.ROOT).contains(needle)
|| post.title().toLowerCase(Locale.ROOT).contains(needle)
|| post.content().toLowerCase(Locale.ROOT).contains(needle);
}
private static boolean matchesFilters(Post post, Map<String, String> filters) {
return contains(post.title(), filters.get("title"))
&& contains(post.content(), filters.get("content"));
}
private static boolean contains(String value, String filter) {
return filter == null || filter.isBlank()
|| value.toLowerCase(Locale.ROOT).contains(filter.toLowerCase(Locale.ROOT));
}
private static int numericId(String id) {
try {
return Integer.parseInt(id);
} catch (NumberFormatException ignored) {
return Integer.MAX_VALUE;
}
}
private static FormMutationResult validate(Post post) {
Map<String, List<String>> errors = new LinkedHashMap<>();
if (post == null) {
return FormMutationResult.failure("Post data is required.");
}
if (post.title() == null || post.title().isBlank()) {
errors.put("title", List.of("Title is required"));
} else if (post.title().length() > 200) {
errors.put("title", List.of("Title must be at most 200 characters"));
}
if (post.content() != null && post.content().length() > 10_000) {
errors.put("content", List.of("Content must be at most 10000 characters"));
}
return errors.isEmpty() ? null : FormMutationResult.invalid(errors);
}
}