Skip to content

Commit a458a38

Browse files
authored
notice receiver rule support get by pages (#17)
1 parent b77f840 commit a458a38

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

manager/src/main/java/com/zmops/open/manager/controller/NoticeConfigController.java

+23-8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import io.swagger.v3.oas.annotations.Parameter;
2626
import io.swagger.v3.oas.annotations.tags.Tag;
2727
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.data.domain.Page;
29+
import org.springframework.data.domain.PageRequest;
30+
import org.springframework.data.domain.Sort;
2831
import org.springframework.data.jpa.domain.Specification;
2932
import org.springframework.http.ResponseEntity;
3033
import org.springframework.web.bind.annotation.DeleteMapping;
@@ -85,8 +88,12 @@ public ResponseEntity<Message<Void>> deleteNoticeReceiver(
8588
@GetMapping(path = "/receivers")
8689
@Operation(summary = "Get a list of message notification recipients based on query filter items",
8790
description = "根据查询过滤项获取消息通知接收人列表")
88-
public ResponseEntity<Message<List<NoticeReceiver>>> getReceivers(
89-
@Parameter(description = "en: Recipient name,zh: 接收人名称,模糊查询", example = "tom") @RequestParam(required = false) final String name) {
91+
public ResponseEntity<Message<Page<NoticeReceiver>>> getReceivers(
92+
@Parameter(description = "en: Recipient name,zh: 接收人名称,模糊查询", example = "tom") @RequestParam(required = false) final String name,
93+
@Parameter(description = "en: Sort Field,default id,zh: 排序字段,默认更新时间", example = "name") @RequestParam(defaultValue = "gmtUpdate") final String sort,
94+
@Parameter(description = "en: Sort by,zh: 排序方式,asc:升序,desc:降序", example = "desc") @RequestParam(defaultValue = "desc") final String order,
95+
@Parameter(description = "en: List current page,zh: 列表当前分页", example = "0") @RequestParam(defaultValue = "0") int pageIndex,
96+
@Parameter(description = "en: Number of list pagination,zh: 列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) {
9097
Specification<NoticeReceiver> specification = (root, query, criteriaBuilder) -> {
9198
Predicate predicate = criteriaBuilder.conjunction();
9299
if (name != null && !"".equals(name)) {
@@ -95,8 +102,10 @@ public ResponseEntity<Message<List<NoticeReceiver>>> getReceivers(
95102
}
96103
return predicate;
97104
};
98-
List<NoticeReceiver> receivers = noticeConfigService.getNoticeReceivers(specification);
99-
Message<List<NoticeReceiver>> message = new Message<>(receivers);
105+
Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
106+
PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp);
107+
Page<NoticeReceiver> receivers = noticeConfigService.getNoticeReceivers(specification, pageRequest);
108+
Message<Page<NoticeReceiver>> message = new Message<>(receivers);
100109
return ResponseEntity.ok(message);
101110
}
102111

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

manager/src/main/java/com/zmops/open/manager/service/NoticeConfigService.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.zmops.open.common.entity.alerter.Alert;
2121
import com.zmops.open.common.entity.manager.NoticeReceiver;
2222
import com.zmops.open.common.entity.manager.NoticeRule;
23+
import org.springframework.data.domain.Page;
24+
import org.springframework.data.domain.PageRequest;
2325
import org.springframework.data.jpa.domain.Specification;
2426

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

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

5456
/**
5557
* Add a notification recipient

manager/src/main/java/com/zmops/open/manager/service/impl/NoticeConfigServiceImpl.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import lombok.extern.slf4j.Slf4j;
2929
import org.springframework.beans.factory.annotation.Autowired;
3030
import org.springframework.context.annotation.Lazy;
31+
import org.springframework.data.domain.Page;
32+
import org.springframework.data.domain.PageRequest;
3133
import org.springframework.data.jpa.domain.Specification;
3234
import org.springframework.stereotype.Service;
3335
import org.springframework.transaction.annotation.Transactional;
@@ -64,13 +66,13 @@ public class NoticeConfigServiceImpl implements NoticeConfigService {
6466
private DispatcherAlarm dispatcherAlarm;
6567

6668
@Override
67-
public List<NoticeReceiver> getNoticeReceivers(Specification<NoticeReceiver> specification) {
68-
return noticeReceiverDao.findAll(specification);
69+
public Page<NoticeReceiver> getNoticeReceivers(Specification<NoticeReceiver> specification, PageRequest pageRequest) {
70+
return noticeReceiverDao.findAll(specification, pageRequest);
6971
}
7072

7173
@Override
72-
public List<NoticeRule> getNoticeRules(Specification<NoticeRule> specification) {
73-
return noticeRuleDao.findAll(specification);
74+
public Page<NoticeRule> getNoticeRules(Specification<NoticeRule> specification, PageRequest pageRequest) {
75+
return noticeRuleDao.findAll(specification, pageRequest);
7476
}
7577

7678
@Override

manager/src/test/java/com/zmops/open/manager/service/NoticeConfigServiceTest.java

-14
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,6 @@ class NoticeConfigServiceTest {
5252
void setUp() {
5353
}
5454

55-
@Test
56-
void getNoticeReceivers() {
57-
final Specification<NoticeReceiver> specification = mock(Specification.class);
58-
noticeConfigService.getNoticeReceivers(specification);
59-
verify(noticeReceiverDao, times(1)).findAll(specification);
60-
}
61-
62-
@Test
63-
void getNoticeRules() {
64-
final Specification<NoticeRule> specification = mock(Specification.class);
65-
noticeConfigService.getNoticeRules(specification);
66-
verify(noticeRuleDao, times(1)).findAll(specification);
67-
}
68-
6955
@Test
7056
void addReceiver() {
7157
final NoticeReceiver noticeReceiver = mock(NoticeReceiver.class);

0 commit comments

Comments
 (0)