Changeset 7888b17


Ignore:
Timestamp:
10/07/21 20:37:55 (3 years ago)
Author:
NikolaCenevski <cenevskinikola@…>
Branches:
master
Children:
b8a8d06
Parents:
ee0e297
Message:

Added post grouping

Location:
src/main
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/it/finki/charitable/controller/DonationPostController.java

    ree0e297 r7888b17  
    44import it.finki.charitable.services.*;
    55import it.finki.charitable.util.FileUploadUtil;
    6 import org.springframework.data.domain.Page;
     6import org.springframework.data.domain.*;
    77import org.springframework.format.annotation.DateTimeFormat;
    88import org.springframework.security.core.context.SecurityContextHolder;
     
    1515import java.io.IOException;
    1616import java.time.LocalDate;
    17 import java.util.ArrayList;
    18 import java.util.Arrays;
    19 import java.util.List;
    20 import java.util.Objects;
     17import java.util.*;
    2118import java.util.stream.Collectors;
    2219
     
    126123                        @RequestParam int page,
    127124                        @RequestParam String sort,
    128                         @RequestParam(required = false,defaultValue = "") String order) {
    129         Page<DonationPost> postList = donationPostService.findPaginated(page,6, sort, order, true);
     125                        @RequestParam(required = false, defaultValue = "desc") String order,
     126                        @RequestParam(required = false, defaultValue = "all") String groupBy) {
     127
     128        Sort s = Sort.by(sort);
     129        s = order.equals("asc") ? s.ascending() : s.descending();
     130        Pageable pageable = PageRequest.of(page - 1, 6, s);
     131        Page<DonationPost> postList;
     132        postList = donationPostService.findPaginated(page, 6, sort, order, true);
     133
     134        if (!groupBy.equals("all")) {
     135            List<DonationPost> allPosts = donationPostService.findAllByApproved(true);
     136
     137            if (sort.equals("title")) {
     138                if (order.equals("asc")) {
     139                    allPosts.sort(Comparator.comparing(DonationPost::getTitle, (s1, s2) -> s1.compareToIgnoreCase(s2)));
     140                } else {
     141                    allPosts.sort(Comparator.comparing(DonationPost::getTitle, (s1, s2) -> s2.compareToIgnoreCase(s1)));
     142                }
     143            } else if (sort.equals("dateDue")) {
     144                if (order.equals("asc")) {
     145                    allPosts.sort(Comparator.comparing(DonationPost::getDateDue));
     146                } else {
     147                    allPosts.sort(Comparator.comparing(DonationPost::getDateDue).reversed());
     148                }
     149            } else if (sort.equals("fundsNeeded")) {
     150                if (order.equals("asc")) {
     151                    allPosts.sort(Comparator.comparing(DonationPost::getFundsNeeded));
     152                } else {
     153                    allPosts.sort(Comparator.comparing(DonationPost::getFundsNeeded).reversed());
     154                }
     155            }
     156
     157            if (groupBy.equals("completed")) {
     158                List<DonationPost> completed = allPosts.stream().filter(post -> {
     159                    double fundsCollected = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum();
     160                    return fundsCollected >= post.getFundsNeeded();
     161                }).collect(Collectors.toList());
     162
     163                int start = (int) pageable.getOffset();
     164                int end = Math.min((start + pageable.getPageSize()), completed.size());
     165                if (start <= end) {
     166                    postList = new PageImpl<>(completed.subList(start, end), pageable, completed.size());
     167                }
     168            } else if (groupBy.equals("expired")) {
     169                List<DonationPost> expired = allPosts.stream().filter(post -> {
     170                    double fundsCollected = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum();
     171                    return LocalDate.now().isAfter(post.getDateDue()) && fundsCollected < post.getFundsNeeded();
     172                }).collect(Collectors.toList());
     173
     174                int start = (int) pageable.getOffset();
     175                int end = Math.min((start + pageable.getPageSize()), expired.size());
     176                if (start <= end) {
     177                    postList = new PageImpl<>(expired.subList(start, end), pageable, expired.size());
     178                }
     179            }
     180        }
     181
    130182        if (postList.getTotalElements() == 0) {
    131183            model.addAttribute("noPosts", true);
     
    193245        DonationPost donationPost = donationPostService.getById(postid);
    194246        ReportPost reportPost = reportPostService.findByDonationPost(donationPost);
    195         if(reportPost == null) {
     247        if (reportPost == null) {
    196248            reportPost = new ReportPost();
    197249            reportPost.setDonationPost(donationPost);
  • src/main/resources/static/js/pagination.js

    ree0e297 r7888b17  
    55let sort = findGetParameter("sort");
    66let order = findGetParameter("order");
     7let group = findGetParameter("groupBy")
    78
    89let sortByInput = document.getElementById("sortBy");
     
    1213orderInput.value = order;
    1314
     15let groupByInput = document.getElementById("groupBy") === null ? document.createElement("input") : document.getElementById("groupBy");
     16groupByInput.value = group;
     17
    1418sortByInput.addEventListener("change", function () {
    15     window.location.href = url + "?page=" + currentPage + "&sort=" + sortByInput.value + "&order=" + orderInput.value;
     19    window.location.href = url + "?page=" + currentPage + "&sort=" + sortByInput.value + "&order=" + orderInput.value + "&groupBy=" + groupByInput.value;
    1620})
    1721
    1822orderInput.addEventListener("change", function() {
    19     window.location.href = url + "?page=" + currentPage + "&sort=" + sortByInput.value + "&order=" + orderInput.value;
     23    window.location.href = url + "?page=" + currentPage + "&sort=" + sortByInput.value + "&order=" + orderInput.value + "&groupBy=" + groupByInput.value;
     24})
     25
     26groupByInput.addEventListener("change", function () {
     27    window.location.href = url + "?page=" + 1 + "&sort=" + sortByInput.value + "&order=" + orderInput.value + "&groupBy=" + groupByInput.value;
    2028})
    2129
     
    2937
    3038    if (page > 1) {
    31         str += '<li class="page-item"><a class="page-link" href="'+url+'?page='+(page-1)+'&sort='+sort+'&order='+order+'">Previous</a></li>';
     39        str += '<li class="page-item"><a class="page-link" href="'+url+'?page='+(page-1)+'&sort='+sort+'&order='+order+'&groupBy='+group+'">Previous</a></li>';
    3240    }
    3341
     
    3543        for (let p = 1; p <= pages; p++) {
    3644            active = page == p ? "active" : "";
    37             str += '<li class="page-item '+active+'"><a class="page-link" href="'+url+'?page='+p+'&sort='+sort+'&order='+order+'">'+ p +'</a></li>';
     45            str += '<li class="page-item '+active+'"><a class="page-link" href="'+url+'?page='+p+'&sort='+sort+'&order='+order+'&groupBy='+group+'">'+ p +'</a></li>';
    3846        }
    3947    }
     
    4149    else {
    4250        if (page > 2) {
    43             str += '<li class="page-item"><a class="page-link" href="'+url+'?page=1&sort='+sort+'&order='+order+'">1</a></li>';
     51            str += '<li class="page-item"><a class="page-link" href="'+url+'?page=1&sort='+sort+'&order='+order+'&groupBy='+group+'">1</a></li>';
    4452            if (page > 3) {
    45                 str += '<li class="page-item"><a class="page-link" href="'+url+'?page='+(page-2)+'&sort='+sort+'&order='+order+'">...</a></li>';
     53                str += '<li class="page-item"><a class="page-link" href="'+url+'?page='+(page-2)+'&sort='+sort+'&order='+order+'&groupBy='+group+'">...</a></li>';
    4654            }
    4755        }
     
    6775            }
    6876            active = page == p ? "active" : "";
    69             str += '<li class="page-item '+active+'"><a class="page-link" href="'+url+'?page='+p+'&sort='+sort+'&order='+order+'">'+ p +'</a></li>';
     77            str += '<li class="page-item '+active+'"><a class="page-link" href="'+url+'?page='+p+'&sort='+sort+'&order='+order+'&groupBy='+group+'">'+ p +'</a></li>';
    7078        }
    7179
    7280        if (page < pages-1) {
    7381            if (page < pages-2) {
    74                 str += '<li class="page-item"><a class="page-link" href="'+url+'?page='+(page+2)+'&sort='+sort+'&order='+order+'">...</a></li>';
     82                str += '<li class="page-item"><a class="page-link" href="'+url+'?page='+(page+2)+'&sort='+sort+'&order='+order+'&groupBy='+group+'">...</a></li>';
    7583            }
    76             str += '<li class="page-item"><a class="page-link" href="'+url+'?page='+pages+'&sort='+sort+'&order='+order+'">'+pages+'</a></li>';
     84            str += '<li class="page-item"><a class="page-link" href="'+url+'?page='+pages+'&sort='+sort+'&order='+order+'&groupBy='+group+'">'+pages+'</a></li>';
    7785        }
    7886    }
    7987
    8088    if (page < pages) {
    81         str += '<li class="page-item"><a class="page-link" href="'+url+'?page='+(page+1)+'&sort='+sort+'&order='+order+'">Next</a></li>';
     89        str += '<li class="page-item"><a class="page-link" href="'+url+'?page='+(page+1)+'&sort='+sort+'&order='+order+'&groupBy='+group+'">Next</a></li>';
    8290    }
    8391    str += '</ul>';
  • src/main/resources/templates/album.html

    ree0e297 r7888b17  
    3535                    </select>
    3636                </div>
     37                <div class="col-md-2">
     38                    <label class="form-label" for="groupBy">Group by</label>
     39                    <select class="input-group-text" id="groupBy">
     40                        <option value="all">All</option>
     41                        <option value="completed">Completed</option>
     42                        <option value="expired">Expired</option>
     43                    </select>
     44                </div>
    3745            </div>
    3846            <br>
  • src/main/resources/templates/common/navbar.html

    ree0e297 r7888b17  
    2323                    </li>
    2424                    <li class="nav-item">
    25                         <a sec:authorize="isAnonymous() or hasAuthority('USER')" th:href="@{/album?page=1&sort=id}" class="nav-link px-2 text-white">Posts</a>
     25                        <a sec:authorize="isAnonymous() or hasAuthority('USER')" th:href="@{/album?page=1&sort=id&order=desc&groupBy=all}" class="nav-link px-2 text-white">Posts</a>
    2626                        <a sec:authorize="hasAuthority('MODERATOR')" th:href="@{/moderator/approval?page=1&sort=id}" class="nav-link px-2 text-white">Posts for approval</a>
    2727                    </li>
  • src/main/resources/templates/post.html

    ree0e297 r7888b17  
    6767                Donate
    6868            </button>
    69             <a sec:authorize="isAnonymous()" type="button" class="btn btn-primary" th:href="@{/login}">
     69            <button sec:authorize="isAnonymous()" type="button" class="btn btn-primary" disabled>
    7070                Donate
    71             </a>
    72             <button type="button" class="btn btn-danger" data-bs-toggle="modal"
     71            </button>
     72            <button sec:authorize="isAuthenticated()" type="button" class="btn btn-danger" data-bs-toggle="modal"
    7373                    data-bs-target="#staticBackdrop1">
    7474                Report
    7575            </button>
     76            <button sec:authorize="isAnonymous()" type="button" class="btn btn-danger" disabled>
     77                Report
     78            </button>
    7679
    7780            <!-- Modal -->
    78             <div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
     81            <div sec:authorize="isAuthenticated()" class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
    7982                <div class="modal-dialog modal-dialog-centered">
    8083                    <div class="modal-content">
     
    136139                </div>
    137140            </div>
    138             <div class="modal fade" id="staticBackdrop1" data-bs-backdrop="static" data-bs-keyboard="false"
     141            <div sec:authorize="isAuthenticated()" class="modal fade" id="staticBackdrop1" data-bs-backdrop="static" data-bs-keyboard="false"
    139142                 tabindex="-1" aria-labelledby="staticBackdropLabel1" aria-hidden="true">
    140143                <div class="modal-dialog modal-dialog-centered">
Note: See TracChangeset for help on using the changeset viewer.