Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.8 kB
1
Indexable
Never

    @GetMapping("/filteredPosts")
    public String getDataBasedOnFiltersAndSearch(@RequestParam(name="sortValue",defaultValue = "",required = false) String value,
                                                 @RequestParam(name="selectedTags",defaultValue = "",required = false) String[] selectedTags,
                                                 @RequestParam(name="selectedAuthors",defaultValue = "",required = false) String[] selectedAuthors,
                                                 @RequestParam(name = "offSet", defaultValue = "1") int offSet,
                                                 @RequestParam(name = "search", required = false) String mySearch,Model model) {

        Page<Post> posts = null;
        Pageable pageable;
        System.out.println("the sorted Value " + value);
        if(Objects.equals(value, "1")) {
            pageable = PageRequest.of(offSet - 1,3, Sort.by("published_at").ascending());
        }
        else {
            pageable = PageRequest.of(offSet - 1,3, Sort.by("published_at").descending());;
        }

        if((selectedAuthors.length > 0) && (selectedTags.length > 0) ) {
            posts = this.postService.findPostsWithMatchesOFTagsAndAuthors(selectedTags, selectedAuthors,pageable);
            model.addAttribute("tags",selectedTags);
            model.addAttribute("post", selectedAuthors);
            model.addAttribute("blogposts",posts);
            model.addAttribute("totalPages", posts.getTotalPages());
            model.addAttribute("sortedValue", value);
            model.addAttribute("value",1);
            model.addAttribute("totalElements",posts.getTotalElements());
        }
        else if(selectedAuthors.length > 0 && selectedTags.length == 0) {
            posts = this.postService.findPostsWithMatchesOfAuthors(selectedAuthors,pageable);
            model.addAttribute("tags",selectedTags);
            model.addAttribute("posts", selectedAuthors);
            model.addAttribute("blogposts",posts);
            model.addAttribute("totalPages", posts.getTotalPages());
            model.addAttribute("sortedValue", value);
            model.addAttribute("value",2);
            model.addAttribute("totalElements",posts.getTotalElements());
        }
        else if (selectedTags.length > 0 && selectedAuthors.length == 0 ) {
            posts = this.postService.findPostsWithMatchesOfTags(selectedTags, pageable);
            model.addAttribute("tags",selectedTags);
            model.addAttribute("posts", selectedAuthors);
            model.addAttribute("blogposts",posts);
            model.addAttribute("totalPages", posts.getTotalPages());
            model.addAttribute("sortedValue", value);
            model.addAttribute("value",3);
            model.addAttribute("totalElements",posts.getTotalElements());
        }