拆解 controller 代码 #26

Merged
baladiwei merged 1 commits from baladiwei/gitlink-notification-system:master into master 2021-09-08 16:18:31 +08:00
1 changed files with 55 additions and 42 deletions

View File

@ -28,59 +28,72 @@ public class NotificationController {
private Logger logger = LogManager.getLogger(NotificationController.class);
/**
* http://{domain}:{port}/gns/notification/list/
* http://{domain}:{port}/gns/notification/count
*
* @param type
* @param receiver
* @return
*/
@GetMapping(value = "/{platform}/{type}")
public ResponseData index(@PathVariable(name = "platform", required = true) String platform,
@PathVariable(name = "type", required = true) String type,
@GetMapping(value = "/{platform}/list")
public ResponseData list(@PathVariable(name = "platform", required = true) String platform,
@RequestParam(name = "receiver", required = true) Integer receiver,
@RequestParam(name = "page", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(name = "size", required = false, defaultValue = "20") Integer pageSize) {
//参数合法性验证
ResponseData jsonFailResult = validateParams(platform, receiver);
if (jsonFailResult != null) return jsonFailResult;
//TODO: 获取约定的必要参数
//调用业务逻辑获取数据结果
List<SysNotification> foundList = notificationService.getUnreadNotificationByType(platform, receiver, pageSize);
//拼装约定的 json 格式并返回
return DataPacketUtil.jsonSuccessResult(foundList);
}//end of method
@GetMapping(value = "/{platform}/count")
public ResponseData count(@PathVariable(name = "platform", required = true) String platform,
@RequestParam(name = "receiver", required = true) Integer receiver,
@RequestParam(name = "page", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(name = "size", required = false, defaultValue = "20") Integer pageSize) {
//参数合法性验证
ResponseData jsonFailResult = validateParams(platform, receiver);
if (jsonFailResult != null) return jsonFailResult;
//TODO: 获取必要的参数
//调用业务逻辑获取数据结果
Integer foundCount = notificationService.getUnreadNotificationCount(platform, receiver);
//拼装约定的 json 格式并返回
return DataPacketUtil.jsonSuccessResult(foundCount);
}
//////////////////////////////// 私有方法 ////////////////////////////////
/**
* 验证 platform receiver 的合法性
*
* @param platform
* @param receiver
* @return
*/
private ResponseData validateParams(String platform, Integer receiver) {
//验证 {platform} 参数合法性以判断请求来源
if (!platform.trim().toLowerCase().equals(PLATFORM_CODE_GITLINK)) {
logger.debug("------>输入参数 {platform} 的值 {" + platform + "} 无效");
return DataPacketUtil.jsonFailResult("参数 {platform} 非法");
logger.debug("\t 输入参数 {platform} 的值 {" + platform + "} 无效");
return DataPacketUtil.jsonFailResult("{platform} 参数非法");
}
//验证 {receiver} 参数合法性
if (receiver <= 0 || receiver >= Integer.MAX_VALUE) {
logger.debug("------>输入参数 {receiver} 的值 {" + receiver + "} 超出约定范围");
return DataPacketUtil.jsonFailResult("参数 {receiver} 非法");
logger.debug("\t 输入参数 {receiver} 的值 {" + receiver + "} 超出约定范围");
return DataPacketUtil.jsonFailResult("{receiver} 参数非法");
}
//判断 {type} 参数合法性正确的值只能是 listcount
if (type.trim().toLowerCase().equals("list")) {
//用户请求的是 list
logger.debug("------>{type} 参数是 list");
//TODO: 获取约定的必要参数
//调用业务逻辑获取数据结果
List<SysNotification> foundList = notificationService.getUnreadNotificationByType(platform, receiver, pageSize);
//拼装约定的 json 格式并返回
return DataPacketUtil.jsonSuccessResult(foundList);
} else if (type.trim().toLowerCase().equals("count")) {
//用户请求的是数字
logger.debug("------>{type} 参数是 count");
//TODO: 获取必要的参数
//调用业务逻辑获取数据结果
Integer foundCount = notificationService.getUnreadNotificationCount(platform,receiver);
//拼装约定的 json 格式并返回
return DataPacketUtil.jsonSuccessResult(foundCount);
} else {
logger.debug("------>输入参数 {type} 的值 {" + type + "} 无效");
return DataPacketUtil.jsonFailResult("参数 {type} 非法");
}//end of if-else
return null;
}//end of method
}//end of class