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

[refactor] move code from NoticeConfigController to NoticeConfigService #2416

Merged
merged 4 commits into from
Jul 30, 2024
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 @@ -22,7 +22,6 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.persistence.criteria.Predicate;
import java.util.List;
import java.util.Optional;
import javax.validation.Valid;
Expand All @@ -32,7 +31,6 @@
import org.apache.hertzbeat.common.entity.manager.NoticeTemplate;
import org.apache.hertzbeat.manager.service.NoticeConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -86,17 +84,8 @@ public ResponseEntity<Message<Void>> deleteNoticeReceiver(
description = "Get a list of message notification recipients based on query filter items")
public ResponseEntity<Message<List<NoticeReceiver>>> getReceivers(
@Parameter(description = "en: Recipient name,support fuzzy query", example = "tom") @RequestParam(required = false) final String name) {
Specification<NoticeReceiver> specification = (root, query, criteriaBuilder) -> {
Predicate predicate = criteriaBuilder.conjunction();
if (name != null && !name.isEmpty()) {
Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
predicate = criteriaBuilder.and(predicateName);
}
return predicate;
};
List<NoticeReceiver> receivers = noticeConfigService.getNoticeReceivers(specification);
Message<List<NoticeReceiver>> message = Message.success(receivers);
return ResponseEntity.ok(message);
List<NoticeReceiver> receivers = noticeConfigService.getNoticeReceivers(name);
return ResponseEntity.ok(Message.success(receivers));
}

@PostMapping(path = "/rule")
Expand Down Expand Up @@ -131,17 +120,8 @@ public ResponseEntity<Message<Void>> deleteNoticeRule(
description = "Get a list of message notification policies based on query filter items")
public ResponseEntity<Message<List<NoticeRule>>> getRules(
@Parameter(description = "en: Recipient name", example = "rule1") @RequestParam(required = false) final String name) {
Specification<NoticeRule> specification = (root, query, criteriaBuilder) -> {
Predicate predicate = criteriaBuilder.conjunction();
if (name != null && !name.isEmpty()) {
Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
predicate = criteriaBuilder.and(predicateName);
}
return predicate;
};
List<NoticeRule> receiverPage = noticeConfigService.getNoticeRules(specification);
Message<List<NoticeRule>> message = Message.success(receiverPage);
return ResponseEntity.ok(message);
List<NoticeRule> receiverPage = noticeConfigService.getNoticeRules(name);
return ResponseEntity.ok(Message.success(receiverPage));
}


Expand Down Expand Up @@ -177,18 +157,8 @@ public ResponseEntity<Message<Void>> deleteNoticeTemplate(
description = "Get a list of message notification templates based on query filter items")
public ResponseEntity<Message<List<NoticeTemplate>>> getTemplates(
@Parameter(description = "Template name,support fuzzy query", example = "rule1") @RequestParam(required = false) final String name) {

Specification<NoticeTemplate> specification = (root, query, criteriaBuilder) -> {
Predicate predicate = criteriaBuilder.conjunction();
if (name != null && !"".equals(name)) {
Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
predicate = criteriaBuilder.and(predicateName);
}
return predicate;
};
List<NoticeTemplate> templatePage = noticeConfigService.getNoticeTemplates(specification);
Message<List<NoticeTemplate>> message = Message.success(templatePage);
return ResponseEntity.ok(message);
List<NoticeTemplate> templatePage = noticeConfigService.getNoticeTemplates(name);
return ResponseEntity.ok(Message.success(templatePage));
}

@PostMapping(path = "/receiver/send-test-msg")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.apache.hertzbeat.common.entity.manager.NoticeReceiver;
import org.apache.hertzbeat.common.entity.manager.NoticeRule;
import org.apache.hertzbeat.common.entity.manager.NoticeTemplate;
import org.springframework.data.jpa.domain.Specification;

/**
* Message notification configuration interface
Expand All @@ -32,24 +31,24 @@ public interface NoticeConfigService {

/**
* Dynamic conditional query
* @param specification Query conditions
* @param name Recipient name,support fuzzy query
* @return Search result
*/
List<NoticeReceiver> getNoticeReceivers(Specification<NoticeReceiver> specification);
List<NoticeReceiver> getNoticeReceivers(String name);

/**
* Dynamic conditional query
* @param specification Query conditions
* @param name Template name,support fuzzy query
* @return Search result
*/
List<NoticeTemplate> getNoticeTemplates(Specification<NoticeTemplate> specification);
List<NoticeTemplate> getNoticeTemplates(String name);

/**
* Dynamic conditional query
* @param specification Query conditions
* @param name Recipient name
* @return Search result
*/
List<NoticeRule> getNoticeRules(Specification<NoticeRule> specification);
List<NoticeRule> getNoticeRules(String name);

/**
* Add a notification recipient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.hertzbeat.manager.service.impl;

import jakarta.persistence.criteria.Predicate;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -83,19 +84,43 @@ public class NoticeConfigServiceImpl implements NoticeConfigService, CommandLine


@Override
public List<NoticeReceiver> getNoticeReceivers(Specification<NoticeReceiver> specification) {
public List<NoticeReceiver> getNoticeReceivers(String name) {
Specification<NoticeReceiver> specification = (root, query, criteriaBuilder) -> {
Predicate predicate = criteriaBuilder.conjunction();
if (name != null && !name.isEmpty()) {
Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
predicate = criteriaBuilder.and(predicateName);
}
return predicate;
};
return noticeReceiverDao.findAll(specification);
}

@Override
public List<NoticeTemplate> getNoticeTemplates(Specification<NoticeTemplate> specification) {
public List<NoticeTemplate> getNoticeTemplates(String name) {
Specification<NoticeTemplate> specification = (root, query, criteriaBuilder) -> {
Predicate predicate = criteriaBuilder.conjunction();
if (name != null && !"".equals(name)) {
Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
predicate = criteriaBuilder.and(predicateName);
}
return predicate;
};
List<NoticeTemplate> defaultTemplates = new LinkedList<>(PRESET_TEMPLATE.values());
defaultTemplates.addAll(noticeTemplateDao.findAll(specification));
return defaultTemplates;
}

@Override
public List<NoticeRule> getNoticeRules(Specification<NoticeRule> specification) {
public List<NoticeRule> getNoticeRules(String name) {
Specification<NoticeRule> specification = (root, query, criteriaBuilder) -> {
Predicate predicate = criteriaBuilder.conjunction();
if (name != null && !name.isEmpty()) {
Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
predicate = criteriaBuilder.and(predicateName);
}
return predicate;
};
return noticeRuleDao.findAll(specification);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,20 @@ void setUp() {

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

@Test
void getNoticeTemplates() {
final Specification<NoticeTemplate> specification = mock(Specification.class);
noticeConfigService.getNoticeTemplates(specification);
verify(noticeTemplateDao, times(1)).findAll(specification);
noticeConfigService.getNoticeTemplates(null);
verify(noticeTemplateDao, times(1)).findAll(any(Specification.class));
}

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

@Test
Expand Down
Loading