package mingzuozhibi.action;

import mingzuozhibi.persist.disc.DiscShelf;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DiscComingController extends BaseController {

    @Deprecated
    @Transactional
    @GetMapping(value = "/api/newdiscs", produces = MEDIA_TYPE)
    public String findAllDeprecated(@RequestParam(defaultValue = "1") int page,
                                    @RequestParam(defaultValue = "20") int pageSize) {
        return findAll(page, pageSize);
    }

    @Transactional
    @GetMapping(value = "/api/discComing", produces = MEDIA_TYPE)
    public String findAll(@RequestParam(defaultValue = "1") int page,
                          @RequestParam(defaultValue = "20") int pageSize) {
        // 校验
        if (pageSize > 20 && noneMatchBasicRole()) {
            return errorMessage("设置pageSize大于20需要更多权限");
        }

        // dataObj
        @SuppressWarnings("unchecked")
        List<DiscShelf> discShelves = dao.create(DiscShelf.class)
                .addOrder(Order.desc("id"))
                .setFirstResult((page - 1) * pageSize)
                .setMaxResults(pageSize)
                .list();

        JSONArray dataObj = new JSONArray();
        discShelves.forEach(discShelf -> {
            dataObj.put(discShelf.toJSON());
        });

        // pageObj
        Long totalElements = (Long) dao.create(DiscShelf.class)
                .setProjection(Projections.rowCount())
                .uniqueResult();

        JSONObject pageObj = new JSONObject();
        pageObj.put("pageSize", pageSize);
        pageObj.put("currentPage", page);
        pageObj.put("totalElements", totalElements);

        return objectResult(dataObj, pageObj);
    }

    private boolean noneMatchBasicRole() {
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        return authentication.getAuthorities().stream()
                .map(GrantedAuthority::getAuthority)
                .noneMatch("ROLE_BASIC"::equals);
    }

}

@@ -811,321 +811,8 @@
  %7B%0A%0A
-    @Deprecated%0A    @Transactional%0A    @GetMapping(value = %22/api/newdiscs%22, produces = MEDIA_TYPE)%0A    public String findAllDeprecated(@RequestParam(defaultValue = %221%22) int page,%0A                                    @RequestParam(defaultValue = %2220%22) int pageSize) %7B%0A        return findAll(page, pageSize);%0A    %7D%0A%0A
     

Remove Deprecated Api