Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notice receiver rule support get by pages #17

Merged
merged 1 commit into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand Down Expand Up @@ -85,8 +88,12 @@ public ResponseEntity<Message<Void>> deleteNoticeReceiver(
@GetMapping(path = "/receivers")
@Operation(summary = "Get a list of message notification recipients based on query filter items",
description = "根据查询过滤项获取消息通知接收人列表")
public ResponseEntity<Message<List<NoticeReceiver>>> getReceivers(
@Parameter(description = "en: Recipient name,zh: 接收人名称,模糊查询", example = "tom") @RequestParam(required = false) final String name) {
public ResponseEntity<Message<Page<NoticeReceiver>>> getReceivers(
@Parameter(description = "en: Recipient name,zh: 接收人名称,模糊查询", example = "tom") @RequestParam(required = false) final String name,
@Parameter(description = "en: Sort Field,default id,zh: 排序字段,默认更新时间", example = "name") @RequestParam(defaultValue = "gmtUpdate") final String sort,
@Parameter(description = "en: Sort by,zh: 排序方式,asc:升序,desc:降序", example = "desc") @RequestParam(defaultValue = "desc") final String order,
@Parameter(description = "en: List current page,zh: 列表当前分页", example = "0") @RequestParam(defaultValue = "0") int pageIndex,
@Parameter(description = "en: Number of list pagination,zh: 列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) {
Specification<NoticeReceiver> specification = (root, query, criteriaBuilder) -> {
Predicate predicate = criteriaBuilder.conjunction();
if (name != null && !"".equals(name)) {
Expand All @@ -95,8 +102,10 @@ public ResponseEntity<Message<List<NoticeReceiver>>> getReceivers(
}
return predicate;
};
List<NoticeReceiver> receivers = noticeConfigService.getNoticeReceivers(specification);
Message<List<NoticeReceiver>> message = new Message<>(receivers);
Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp);
Page<NoticeReceiver> receivers = noticeConfigService.getNoticeReceivers(specification, pageRequest);
Message<Page<NoticeReceiver>> message = new Message<>(receivers);
return ResponseEntity.ok(message);
}

Expand Down Expand Up @@ -131,8 +140,12 @@ public ResponseEntity<Message<Void>> deleteNoticeRule(
@GetMapping(path = "/rules")
@Operation(summary = "Get a list of message notification policies based on query filter items",
description = "根据查询过滤项获取消息通知策略列表")
public ResponseEntity<Message<List<NoticeRule>>> getRules(
@Parameter(description = "en: Recipient name,zh: 接收人名称,模糊查询", example = "rule1") @RequestParam(required = false) final String name) {
public ResponseEntity<Message<Page<NoticeRule>>> getRules(
@Parameter(description = "en: Recipient name,zh: 接收人名称,模糊查询", example = "rule1") @RequestParam(required = false) final String name,
@Parameter(description = "en: Sort Field,default id,zh: 排序字段,默认更新时间", example = "name") @RequestParam(defaultValue = "gmtUpdate") final String sort,
@Parameter(description = "en: Sort by,zh: 排序方式,asc:升序,desc:降序", example = "desc") @RequestParam(defaultValue = "desc") final String order,
@Parameter(description = "en: List current page,zh: 列表当前分页", example = "0") @RequestParam(defaultValue = "0") int pageIndex,
@Parameter(description = "en: Number of list pagination,zh: 列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) {
Specification<NoticeRule> specification = (root, query, criteriaBuilder) -> {
Predicate predicate = criteriaBuilder.conjunction();
if (name != null && !"".equals(name)) {
Expand All @@ -141,8 +154,10 @@ public ResponseEntity<Message<List<NoticeRule>>> getRules(
}
return predicate;
};
List<NoticeRule> receiverPage = noticeConfigService.getNoticeRules(specification);
Message<List<NoticeRule>> message = new Message<>(receiverPage);
Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp);
Page<NoticeRule> receiverPage = noticeConfigService.getNoticeRules(specification, pageRequest);
Message<Page<NoticeRule>> message = new Message<>(receiverPage);
return ResponseEntity.ok(message);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.zmops.open.common.entity.alerter.Alert;
import com.zmops.open.common.entity.manager.NoticeReceiver;
import com.zmops.open.common.entity.manager.NoticeRule;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;

import java.util.List;
Expand All @@ -40,7 +42,7 @@ public interface NoticeConfigService {
* @param specification Query conditions 查询条件
* @return Search result 查询结果
*/
List<NoticeReceiver> getNoticeReceivers(Specification<NoticeReceiver> specification);
Page<NoticeReceiver> getNoticeReceivers(Specification<NoticeReceiver> specification, PageRequest pageRequest);

/**
* Dynamic conditional query
Expand All @@ -49,7 +51,7 @@ public interface NoticeConfigService {
* @param specification Query conditions 查询条件
* @return Search result 查询结果
*/
List<NoticeRule> getNoticeRules(Specification<NoticeRule> specification);
Page<NoticeRule> getNoticeRules(Specification<NoticeRule> specification, PageRequest pageRequest);

/**
* Add a notification recipient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -64,13 +66,13 @@ public class NoticeConfigServiceImpl implements NoticeConfigService {
private DispatcherAlarm dispatcherAlarm;

@Override
public List<NoticeReceiver> getNoticeReceivers(Specification<NoticeReceiver> specification) {
return noticeReceiverDao.findAll(specification);
public Page<NoticeReceiver> getNoticeReceivers(Specification<NoticeReceiver> specification, PageRequest pageRequest) {
return noticeReceiverDao.findAll(specification, pageRequest);
}

@Override
public List<NoticeRule> getNoticeRules(Specification<NoticeRule> specification) {
return noticeRuleDao.findAll(specification);
public Page<NoticeRule> getNoticeRules(Specification<NoticeRule> specification, PageRequest pageRequest) {
return noticeRuleDao.findAll(specification, pageRequest);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,6 @@ class NoticeConfigServiceTest {
void setUp() {
}

@Test
void getNoticeReceivers() {
final Specification<NoticeReceiver> specification = mock(Specification.class);
noticeConfigService.getNoticeReceivers(specification);
verify(noticeReceiverDao, times(1)).findAll(specification);
}

@Test
void getNoticeRules() {
final Specification<NoticeRule> specification = mock(Specification.class);
noticeConfigService.getNoticeRules(specification);
verify(noticeRuleDao, times(1)).findAll(specification);
}

@Test
void addReceiver() {
final NoticeReceiver noticeReceiver = mock(NoticeReceiver.class);
Expand Down