序
本文主要研究一下PowerJob的AppInfoController
AppInfoController
tech/powerjob/server/web/controller/AppInfoController.java
@RestController
@RequestMapping("/appInfo")
@RequiredArgsConstructor
public class AppInfoController {private final AppInfoService appInfoService;private final AppInfoRepository appInfoRepository;private static final int MAX_APP_NUM = 200;@PostMapping("/save")public ResultDTO<Void> saveAppInfo(@RequestBody ModifyAppInfoRequest req) {req.valid();AppInfoDO appInfoDO;Long id = req.getId();if (id == null) {appInfoDO = new AppInfoDO();appInfoDO.setGmtCreate(new Date());}else {appInfoDO = appInfoRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("can't find appInfo by id:" + id));// 对比密码if (!Objects.equals(req.getOldPassword(), appInfoDO.getPassword())) {throw new PowerJobException("The password is incorrect.");}}BeanUtils.copyProperties(req, appInfoDO);appInfoDO.setGmtModified(new Date());appInfoRepository.saveAndFlush(appInfoDO);return ResultDTO.success(null);}@PostMapping("/assert")public ResultDTO<Long> assertApp(@RequestBody AppAssertRequest request) {return ResultDTO.success(appInfoService.assertApp(request.getAppName(), request.getPassword()));}@GetMapping("/delete")public ResultDTO<Void> deleteAppInfo(Long appId) {appInfoRepository.deleteById(appId);return ResultDTO.success(null);}@GetMapping("/list")public ResultDTO<List<AppInfoVO>> listAppInfo(@RequestParam(required = false) String condition) {List<AppInfoDO> result;Pageable limit = PageRequest.of(0, MAX_APP_NUM);if (StringUtils.isEmpty(condition)) {result = appInfoRepository.findAll(limit).getContent();}else {result = appInfoRepository.findByAppNameLike("%" + condition + "%", limit).getContent();}return ResultDTO.success(convert(result));}private static List<AppInfoVO> convert(List<AppInfoDO> data) {if (CollectionUtils.isEmpty(data)) {return Lists.newLinkedList();}return data.stream().map(appInfoDO -> {AppInfoVO appInfoVO = new AppInfoVO();BeanUtils.copyProperties(appInfoDO, appInfoVO);return appInfoVO;}).collect(Collectors.toList());}@Dataprivate static class AppInfoVO {private Long id;private String appName;}}
AppInfoController提供了save、assert、delete、list方法
AppInfoService
tech/powerjob/server/core/service/AppInfoService.java
public interface AppInfoService {Long assertApp(String appName, String password);
}
AppInfoService定义了assertApp方法
AppInfoServiceImpl
tech/powerjob/server/core/service/impl/AppInfoServiceImpl.java
@Service
@RequiredArgsConstructor
public class AppInfoServiceImpl implements AppInfoService {private final AppInfoRepository appInfoRepository;/*** 验证应用访问权限* @param appName 应用名称* @param password 密码* @return 应用ID*/@Overridepublic Long assertApp(String appName, String password) {AppInfoDO appInfo = appInfoRepository.findByAppName(appName).orElseThrow(() -> new PowerJobException("can't find appInfo by appName: " + appName));if (Objects.equals(appInfo.getPassword(), password)) {return appInfo.getId();}throw new PowerJobException("password error!");}
}
AppInfoServiceImpl实现了AppInfoService接口,其assertApp是根据appName找到AppInfoDO,然后对比password是否一致
小结
PowerJob的AppInfoController提供了注册、验证等接口,其中assert接口调用的是AppInfoService的assertApp方法,它根据appName找到AppInfoDO,然后对比password是否一致。