diff --git a/luoo_common/src/main/java/enums/VerifyRegexEnum.java b/luoo_common/src/main/java/enums/VerifyRegexEnum.java index f36acda..d1f9b4b 100644 --- a/luoo_common/src/main/java/enums/VerifyRegexEnum.java +++ b/luoo_common/src/main/java/enums/VerifyRegexEnum.java @@ -2,6 +2,7 @@ package enums; public enum VerifyRegexEnum { NO("", "不校验"), + RANDOM_SONG_LIMIT("[1-9]|[12][0-9]|30", "歌曲随机数限制,[1~30]"), MOBILE("(1[0-9])\\d{9}$", "手机号码"), MOBILE_CHECK_CODE("\\d{6}$", "手机短信验证号码"), PASSWORD("^(?=.*\\d)(?=.*[a-zA-Z])[\\da-zA-Z~!@#$%^&*_]{8,}$", "只能是数字,字母,特殊字符 8-18位"); diff --git a/luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java b/luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java index 965a3c1..d34876c 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java @@ -26,6 +26,7 @@ public class BaseExceptionHandler { @ResponseBody public Result error(BizException e) { log.info("业务错误:{}", e.getMessage()); - return Result.failed(StatusCode.MUSIC_COMMON_FAILED, e.getMessage()); + StatusCode statusCode = null == e.getCodeEnum() ? StatusCode.MUSIC_COMMON_FAILED : e.getCodeEnum(); + return Result.failed(statusCode, e.getMessage()); } } diff --git a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java index 234ea34..2eed240 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java @@ -16,6 +16,7 @@ import com.luoo.music.pojo.Article; import com.luoo.music.service.ArticleService; import annotation.GlobalInterceptor; +import annotation.VerifyParam; import api.PageResult; import api.Result; import constants.Constants; @@ -38,7 +39,7 @@ public class JournalController { @GetMapping("/list") @GlobalInterceptor(checkLogin = true) public Result> page(@RequestHeader(value = "token", required = false) String token, - JournalQueryReq queryReq){ + @VerifyParam JournalQueryReq queryReq){ Page
pageList = articleService.queryPage(queryReq); List list=pageList.stream().map(a->getArticleRespDTO(a)).collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()), list) ); diff --git a/luoo_music/src/main/java/com/luoo/music/controller/SongController.java b/luoo_music/src/main/java/com/luoo/music/controller/SongController.java index 3c6e728..91f0806 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/SongController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/SongController.java @@ -6,9 +6,13 @@ import com.luoo.music.pojo.Song; import com.luoo.music.service.SongService; import annotation.GlobalInterceptor; +import annotation.VerifyParam; import api.PageResult; import api.Result; import constants.Constants; +import enums.VerifyRegexEnum; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; @@ -49,18 +53,19 @@ public class SongController { return Result.success(results); } - /* - * @ApiOperation(value = "2.随机播放歌曲",notes="雀乐FM") - * - * @GetMapping("/random/{limit}") - * - * @GlobalInterceptor(checkLogin = true) public Result> - * getByJournalNo(@RequestHeader(value = "token", required = false) String - * token) { List songs = songService.random(); List results = - * songs.stream().map(s -> getSongRespDTO(s)).collect(Collectors.toList()); - * return Result.success(results); } - */ - + @ApiOperation(value = "2.随机播放歌曲", notes = "雀乐FM") + @ApiImplicitParams({ + @ApiImplicitParam(name = "limit", value = "随机歌曲数,最少1首,最多30首", required = false) + }) + @GetMapping("/random/{limit}") + @GlobalInterceptor(checkLogin = true) + public Result> random(@RequestHeader(value = "token", required = false) String token, + @PathVariable @VerifyParam(required=true,regex=VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) { + List songs = songService.random(limit); + List results = songs.stream().map(s -> getSongRespDTO(s)).collect(Collectors.toList()); + return Result.success(results); + } + private SongRespDTO getSongRespDTO(Song song) { SongRespDTO songRespDTO = new SongRespDTO(); songRespDTO.setJournalNo(song.getVolid()); diff --git a/luoo_music/src/main/java/com/luoo/music/dao/SongDao.java b/luoo_music/src/main/java/com/luoo/music/dao/SongDao.java index 9c18efa..7025889 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/SongDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/SongDao.java @@ -3,10 +3,14 @@ package com.luoo.music.dao; import com.luoo.music.pojo.Song; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Query; import java.util.List; public interface SongDao extends JpaRepository, JpaSpecificationExecutor { public List findSongsByVolid(String volid); + + @Query(value = "select * from tb_song order by rand() limit ?1 ", nativeQuery = true) + List random(int limit); } diff --git a/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java b/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java index 9fc12c4..02d8897 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java @@ -1,6 +1,8 @@ package com.luoo.music.dto.request; import com.fasterxml.jackson.annotation.JsonFormat; + +import annotation.VerifyParam; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -17,12 +19,14 @@ import java.time.LocalDateTime; @ApiModel public class JournalQueryReq implements Serializable { private static final long serialVersionUID = -1198060864891902188L; - @ApiModelProperty(value = "风格") + @ApiModelProperty(value = "筛选条件:风格") private String style; - @ApiModelProperty(value = "语言") + @ApiModelProperty(value = "筛选条件:语言") private String language; - @ApiModelProperty(value = "分页: 页码", example = "1") - private int pageNum = 1; + @VerifyParam(required=true) + @ApiModelProperty(value = "分页: 页码,以1开始,即获取最新的几期", example = "1") + private Integer pageNum = 1; + @VerifyParam(required=true) @ApiModelProperty(value = "分页: 每页数量", example = "10") - private int pageSize = 10; + private Integer pageSize = 10; } diff --git a/luoo_music/src/main/java/com/luoo/music/service/SongService.java b/luoo_music/src/main/java/com/luoo/music/service/SongService.java index ea0615f..3cec232 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/SongService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/SongService.java @@ -145,4 +145,8 @@ public class SongService { public List findByVolid(String volid){ return songDao.findSongsByVolid(volid); } + + public List random(Integer limit) { + return songDao.random(limit); + } }