Skip to content

Commit

Permalink
refactor: Update PostController and PostServiceImpl to handle empty s…
Browse files Browse the repository at this point in the history
…earch parameter

Refactor the PostController and PostServiceImpl classes to handle the case when the search parameter is empty. This change sets the default value of the search parameter to an empty string in the request mapping annotations, ensuring consistent behavior when searching for posts. It improves code readability and maintains a clear separation of concerns between the controller and service layers.
  • Loading branch information
devaaravmishra committed Jul 6, 2024
1 parent 5e5f29c commit 5f25cf0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public ResponseEntity<PostResponse> createPost(@Valid @ModelAttribute PostReques
public ResponseEntity<Page<PostResponse>> getAllPosts(@RequestParam(defaultValue = "10") Integer limit,
@RequestParam(defaultValue = "0") Integer offset,
@RequestParam(defaultValue = "createdAt,desc") String sort_by,
@RequestParam(required = false) String search) {
@RequestParam(required = false, defaultValue = "") String search) {

List<Sort.Order> orders = SortValidator.validateSort(sort_by, ALLOWED_SORT_PROPERTIES);
Pageable pageable = createPageRequestUsing(offset, limit, Sort.by(orders));
Expand Down Expand Up @@ -94,7 +94,7 @@ public ResponseEntity<Page<PostResponse>> getPostsByCommunity(@PathVariable Long
@RequestParam(defaultValue= "10") Integer limit,
@RequestParam(defaultValue = "0") Integer offset,
@RequestParam(defaultValue = "createdAt,desc") String sort_by,
@RequestParam(required = false) String search) {
@RequestParam(required = false, defaultValue = "") String search) {
List<Sort.Order> orders = SortValidator.validateSort(sort_by, ALLOWED_SORT_PROPERTIES);
Pageable pageable = createPageRequestUsing(offset, limit, Sort.by(orders));

Expand All @@ -116,7 +116,7 @@ public ResponseEntity<Page<PostResponse>> getPostsByAuthor(@PathVariable Long au
@RequestParam(defaultValue= "10") Integer limit,
@RequestParam(defaultValue = "0") Integer offset,
@RequestParam(defaultValue = "createdAt,desc") String sort_by,
@RequestParam(required = false) String search) {
@RequestParam(required = false, defaultValue = "") String search) {
List<Sort.Order> orders = SortValidator.validateSort(sort_by, ALLOWED_SORT_PROPERTIES);
Pageable pageable = createPageRequestUsing(offset, limit, Sort.by(orders));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import jakarta.persistence.EntityNotFoundException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
Expand Down Expand Up @@ -100,10 +99,10 @@ private String saveFile(MultipartFile file) {
public Page<Post> findAll(String search, Pageable pageable) {
try {
Specification<Post> spec = Specification.where(null);

search = search.trim();
if (StringUtils.isEmpty(search)) {
spec = spec.and(PostSpecification.containsTextInTitleOrContentOrCommunity(search.trim()));
search = removeAllWhitespace(search);
if (!search.isEmpty()) {
spec = spec.and(PostSpecification.containsTextInTitleOrContentOrCommunity(search));
}

return postRepository.findAll(spec, pageable);
Expand All @@ -112,16 +111,16 @@ public Page<Post> findAll(String search, Pageable pageable) {
throw new RippleReachException("Error while retrieving all posts!");
}
}

@Override
public Page<Post> findAllByCommunity(Long communityId, String search, Pageable pageable) {
try {
Specification<Post> spec = Specification.where((root, query, cb) ->
cb.equal(root.get("community").get("id"), communityId)
);

search = search.trim();
if (StringUtils.isEmpty(search)) {
search = removeAllWhitespace(search);
if (!search.isEmpty()) {
spec = spec.and(PostSpecification.containsTextInTitleOrContentOrCommunity(search));
}

Expand All @@ -139,8 +138,8 @@ public Page<Post> findAllByAuthor(Long authorId, String search, Pageable pageabl
cb.equal(root.get("author").get("id"), authorId)
);

search = search.trim();
if (StringUtils.isEmpty(search)) {
search = removeAllWhitespace(search);
if (!search.isEmpty()) {
spec = spec.and(PostSpecification.containsTextInTitleOrContentOrCommunity(search));
}

Expand Down Expand Up @@ -227,4 +226,8 @@ public void decrementUpvotes(Long postId) {
post.setTotalUpvotes(post.getTotalUpvotes() - 1);
postRepository.save(post);
}

private static String removeAllWhitespace(String search) {
return search.replaceAll("[\\p{Z}\\s\"']", "");
}
}

0 comments on commit 5f25cf0

Please sign in to comment.