如何过滤用户使用QueryService创建的记录?

时间:2018-08-09 09:05:52

标签: jhipster

我的JDL中有一个通知实体:

relationship ManyToOne {
    Notification{user(id) required} to User
}

// SET SERVICE OPTIONS:
service all with serviceImpl

// DTO:
dto all with mapstruct

// FILTERING:
filter *

现在,我需要用户登录后,我们显示他或她收到的通知(而不是其他所有人的通知)。

NotificationResource如下所示:

/**
 * GET  /notifications : get all the notifications.
 *
 * @param pageable the pagination information
 * @param criteria the criterias which the requested entities should match
 * @return the ResponseEntity with status 200 (OK) and the list of notifications in body
 */
@GetMapping("/notifications")
@Timed
public ResponseEntity<List<NotificationDTO>> getAllNotifications(NotificationCriteria criteria, Pageable pageable) {
    log.debug("REST request to get Notifications by criteria: {}", criteria);
    Page<NotificationDTO> page = notificationQueryService.findByCriteria(criteria, pageable);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/notifications");
    return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}

还有NotificationQueryService ...

/**
     * Function to convert NotificationCriteria to a {@link Specification}
     */
    private Specification<Notification> createSpecification(NotificationCriteria criteria) {
        Specification<Notification> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), Notification_.id));
            }
            if (criteria.getCreationDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getCreationDate(), Notification_.creationDate));
            }
            if (criteria.getNotificationDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getNotificationDate(), Notification_.notificationDate));
            }
            if (criteria.getNotificationReason() != null) {
                specification = specification.and(buildSpecification(criteria.getNotificationReason(), Notification_.notificationReason));
            }
            if (criteria.getNotificationText() != null) {
                specification = specification.and(buildStringSpecification(criteria.getNotificationText(), Notification_.notificationText));
            }
            if (criteria.getIsDeliverd() != null) {
                specification = specification.and(buildSpecification(criteria.getIsDeliverd(), Notification_.isDeliverd));
            }
            if (criteria.getUserId() != null) {
                specification = specification.and(buildReferringEntitySpecification(criteria.getUserId(), Notification_.user, User_.id));
            }
        }
        return specification;
    }

PD:我见过这个solution for new parameters,但我听不懂。

如果实体与用户不直接相关(例如,属于属于用户的Blog的帖子),我们想在其中如何从PostQueryService中过滤来自当前用户的帖子。在这种情况下应修改什么?谢谢。

0 个答案:

没有答案