文章目录
1.解决重复注册 1.AuthUserDomainServiceImpl.java 在注册之前从db查询是否已经存在该用户 2.AuthUserService.java 3.AuthUserServiceImpl.java 4.测试
2.用户信息查询 1.接口设计 2.代码实现 1.UserController.java 2.AuthUserDomainService.java 3.AuthUserDomainServiceImpl.java
3.测试
3.用户退出登录
4.上传头像 1.StorageAdapter.java 2.MinioStorageAdapter.java 3.FileService.java 4.FileController.java
5.分类题目数量更新 1.SubjectCategoryDTO.java 2.SubjectCategoryBO.java 3.SubjectCategoryService.java 4.SubjectCategoryServiceImpl.java 5.SubjectCategoryDao.java 6.SubjectCategoryDao.xml
6.分类标签性能优化 1.SubjectCategoryController.java 2.SubjectCategoryDomainService.java 3.SubjectCategoryDomainServiceImpl.java 4.DTO和BO增加标签字段 1.SubjectCategoryDTO.java 2.SubjectCategoryBO.java
5.测试
7.用户权限获取 1.PermissionController.java 2.AuthPermissionDomainService.java 3.AuthPermissionDomainServiceImpl.java 4.测试
1.解决重复注册
1.AuthUserDomainServiceImpl.java 在注册之前从db查询是否已经存在该用户
2.AuthUserService.java
List < AuthUser > queryByConditionCount ( AuthUser authUser) ;
3.AuthUserServiceImpl.java
@Override
public List < AuthUser > queryByConditionCount ( AuthUser authUser) {
return this . authUserDao. queryAllByLimit ( authUser) ;
}
4.测试
1.请求
2.后台,提示已经注册
2.用户信息查询
1.接口设计
2.代码实现
1.UserController.java
@RequestMapping ( "getUserInfo" )
public Result < AuthUserDTO > getUserInfo ( @RequestBody AuthUserDTO authUserDTO) {
try {
if ( log. isInfoEnabled ( ) ) {
log. info ( "UserController getUserInfo AuthUserDTO, authUserDTO:{}" , JSON . toJSONString ( authUserDTO) ) ;
}
Preconditions . checkArgument ( ! StringUtils . isBlank ( authUserDTO. getUserName ( ) ) , "用户名不能为空" ) ;
AuthUserBO authUserBO = AuthUserDTOConverter . INSTANCE . convertDTO2BO ( authUserDTO) ;
AuthUserBO res = authUserDomainService. getUserInfo ( authUserBO) ;
AuthUserDTO authUserDTO1 = AuthUserDTOConverter . INSTANCE . convertBO2DTO ( res) ;
return Result . ok ( authUserDTO1) ;
} catch ( Exception e) {
log. error ( "UserController getUserInfo error:{}" , e. getMessage ( ) , e) ;
return Result . fail ( "获取用户信息失败" ) ;
}
}
2.AuthUserDomainService.java
AuthUserBO getUserInfo ( AuthUserBO authUserBO) ;
3.AuthUserDomainServiceImpl.java
@Override
public AuthUserBO getUserInfo ( AuthUserBO authUserBO) {
AuthUser authUser = AuthUserBOConverter . INSTANCE . convertBO2Entity ( authUserBO) ;
List < AuthUser > authUsers = authUserService. queryByConditionCount ( authUser) ;
AuthUser authUser1 = authUsers. get ( 0 ) ;
AuthUserBO authUserBO1 = AuthUserBOConverter . INSTANCE . convertEntity2BO ( authUser1) ;
return authUserBO1;
}
3.测试
3.用户退出登录
1.UserController.java
@RequestMapping ( "logOut" )
public Result logOut ( @RequestParam ( "userName" ) String userName) {
try {
if ( log. isInfoEnabled ( ) ) {
log. info ( "UserController logOut userName:{}" , userName) ;
}
Preconditions . checkArgument ( ! StringUtils . isBlank ( userName) , "用户名不能为空" ) ;
StpUtil . logout ( userName) ;
return Result . ok ( ) ;
} catch ( Exception e) {
log. error ( "UserController logOut error:{}" , e. getMessage ( ) , e) ;
return Result . fail ( "用户退出失败" ) ;
}
}
4.上传头像
1.StorageAdapter.java
@Override
@SneakyThrows
public String getUrl ( String bucket, String objectName) {
return url + "/" + bucket + "/" + objectName;
}
2.MinioStorageAdapter.java
@Override
@SneakyThrows
public String getUrl ( String bucket, String objectName) {
return url + "/" + bucket + "/" + objectName;
}
3.FileService.java
public String uploadFile ( MultipartFile uploadFile, String bucket, String objectName) {
storageAdapter. uploadFile ( uploadFile, bucket, objectName) ;
objectName = objectName + "/" + uploadFile. getOriginalFilename ( ) ;
return storageAdapter. getUrl ( bucket, objectName) ;
}
4.FileController.java
@RequestMapping ( "/upload" )
public Result upload ( MultipartFile uploadFile, String bucket, String objectName) throws Exception {
String url = fileService. uploadFile ( uploadFile, bucket, objectName) ;
return Result . ok ( url) ;
}
5.分类题目数量更新
1.SubjectCategoryDTO.java
2.SubjectCategoryBO.java
3.SubjectCategoryService.java
Integer querySubjectCount ( Long id) ;
4.SubjectCategoryServiceImpl.java
@Override
public Integer querySubjectCount ( Long id) {
return this . subjectCategoryDao. querySubjectCount ( id) ;
}
5.SubjectCategoryDao.java
Integer querySubjectCount ( @Param ( "id" ) Long id) ;
6.SubjectCategoryDao.xml
< select id = " querySubjectCount" resultType = " java.lang.Integer" >
select count(distinct subject_id)
from subject_mapping
where category_id = #{id}
</ select>
6.分类标签性能优化
1.SubjectCategoryController.java
@PostMapping ( "/queryCategoryAndLabel" )
public Result < List < SubjectCategoryDTO > > queryCategoryAndLabel ( @RequestBody SubjectCategoryDTO subjectCategoryDTO) {
try {
if ( log. isInfoEnabled ( ) ) {
log. info ( "SubjectCategoryController queryCategoryAndLabel SubjectCategoryDTO, subjectCategoryDTO:{}" , JSON . toJSONString ( subjectCategoryDTO) ) ;
}
Preconditions . checkNotNull ( subjectCategoryDTO. getId ( ) , "分类id不能为空" ) ;
SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter . INSTANCE . convertDTOToSubjectCategory ( subjectCategoryDTO) ;
List < SubjectCategoryBO > subjectCategoryBOList = subjectCategoryDomainService. queryCategoryAndLabel ( subjectCategoryBO) ;
List < SubjectCategoryDTO > subjectCategoryDTOList = new LinkedList < > ( ) ;
subjectCategoryBOList. forEach ( subjectCategoryBO1 -> {
SubjectCategoryDTO subjectCategoryDTO1 = new SubjectCategoryDTO ( ) ;
subjectCategoryDTO1. setId ( subjectCategoryBO1. getId ( ) ) ;
subjectCategoryDTO1. setCategoryName ( subjectCategoryBO1. getCategoryName ( ) ) ;
subjectCategoryDTO1. setCategoryType ( subjectCategoryBO1. getCategoryType ( ) ) ;
subjectCategoryDTO1. setImageUrl ( subjectCategoryBO1. getImageUrl ( ) ) ;
subjectCategoryDTO1. setParentId ( subjectCategoryBO1. getParentId ( ) ) ;
subjectCategoryDTO1. setCount ( subjectCategoryBO1. getCount ( ) ) ;
List < SubjectLabelBO > labelBOList = subjectCategoryBO1. getLabelBOList ( ) ;
List < SubjectLabelDTO > subjectLabelDTOList = SubjectLabelDTOConverter . INSTANCE . convertSubjectLabelToDto ( labelBOList) ;
subjectCategoryDTO1. setLabelDTOList ( subjectLabelDTOList) ;
subjectCategoryDTOList. add ( subjectCategoryDTO1) ;
} ) ;
return Result . ok ( subjectCategoryDTOList) ;
} catch ( Exception e) {
log. error ( "SubjectCategoryController queryCategoryAndLabel error:{}" , e. getMessage ( ) , e) ;
return Result . fail ( "查询分类及标签失败" ) ;
}
}
2.SubjectCategoryDomainService.java
List < SubjectCategoryBO > queryCategoryAndLabel ( SubjectCategoryBO subjectCategoryBO) ;
3.SubjectCategoryDomainServiceImpl.java
@Override
public List < SubjectCategoryBO > queryCategoryAndLabel ( SubjectCategoryBO subjectCategoryBO) {
SubjectCategory subjectCategory = new SubjectCategory ( ) ;
subjectCategory. setParentId ( subjectCategoryBO. getParentId ( ) ) ;
subjectCategory. setIsDeleted ( IsDeleteFlagEnum . UN_DELETED . getCode ( ) ) ;
List < SubjectCategory > subjectCategoryList = subjectCategoryService. queryCategory ( subjectCategory) ;
if ( log. isInfoEnabled ( ) ) {
log. info ( "SubjectCategoryDomainServiceImpl queryCategoryAndLabel, subjectCategoryList:{}" , JSON . toJSONString ( subjectCategoryList) ) ;
}
List < SubjectCategoryBO > subjectCategoryBOList = SubjectCategoryConverter . INSTANCE . convertSubjectCategoryToBo ( subjectCategoryList) ;
subjectCategoryBOList. forEach ( category -> {
SubjectMapping subjectMapping = new SubjectMapping ( ) ;
subjectMapping. setCategoryId ( category. getId ( ) ) ;
subjectMapping. setIsDeleted ( IsDeleteFlagEnum . UN_DELETED . getCode ( ) ) ;
List < SubjectMapping > subjectMappings = subjectMappingService. queryLabelId ( subjectMapping) ;
List < Long > labelIdList = subjectMappings. stream ( ) . map ( SubjectMapping :: getLabelId ) . collect ( Collectors . toList ( ) ) ;
List < SubjectLabel > subjectLabelList = subjectLabelService. batchQueryById ( labelIdList) ;
List < SubjectLabelBO > subjectLabelBOList = SubjectLabelConverter . INSTANCE . convertSubjectLabelToBo ( subjectLabelList) ;
category. setLabelBOList ( subjectLabelBOList) ;
} ) ;
return subjectCategoryBOList;
}
4.DTO和BO增加标签字段
1.SubjectCategoryDTO.java
2.SubjectCategoryBO.java
5.测试
1.接口设计
2.测试
7.用户权限获取
1.PermissionController.java
@RequestMapping ( "getPermission" )
public Result < Boolean > getPermission ( String userName) {
try {
if ( log. isInfoEnabled ( ) ) {
log. info ( "PermissionController getPermission userName:{}" , userName) ;
}
Preconditions . checkArgument ( StringUtils . isNotBlank ( userName) , "用户名不能为空" ) ;
return Result . ok ( authPermissionDomainService. getPermission ( userName) ) ;
} catch ( Exception e) {
log. error ( "PermissionController getPermission error:{}" , e. getMessage ( ) , e) ;
return Result . fail ( "获取权限信息失败" ) ;
}
}
2.AuthPermissionDomainService.java
List < String > getPermission ( String userName) ;
3.AuthPermissionDomainServiceImpl.java
@Override
public List < String > getPermission ( String userName) {
String permissionKey = redisUtil. buildKey ( authPermissionPrefix, userName) ;
String permissionValue = redisUtil. get ( permissionKey) ;
if ( permissionValue == null ) {
return Collections . emptyList ( ) ;
}
List < AuthPermission > authPermissionList = new Gson ( ) . fromJson ( permissionValue, new TypeToken < List < AuthPermission > > ( ) {
} . getType ( ) ) ;
List < String > authList = authPermissionList. stream ( ) . map ( AuthPermission :: getPermissionKey ) . collect ( Collectors . toList ( ) ) ;
return authList;
}
4.测试