From ec58f867e4ed76fecfdaaa8f21e8ada9321ea9d5 Mon Sep 17 00:00:00 2001 From: Gary Date: Fri, 19 Jan 2024 00:06:38 +0800 Subject: [PATCH] 1.enable collect interface --- luoo_common/pom.xml | 4 +++ .../src/main/java/mongodb}/UserCollect.java | 18 +++-------- .../com/luoo/music/client/UserClient.java | 5 ++- .../music/controller/JournalController.java | 26 +++++++++++---- .../luoo/music/controller/SongController.java | 32 ++++++++++++------- .../com/luoo/music/dao/UserCollectDao.java | 10 ++++++ .../luoo/music/dto/response/SongRespDTO.java | 2 ++ .../controller/UserCollectController.java | 2 ++ .../com/luoo/user/dao/UserCollectDao.java | 8 ++--- .../luoo/user/service/UserCollectService.java | 6 +--- .../luoo/user/service/UserInfoService.java | 1 - 11 files changed, 69 insertions(+), 45 deletions(-) rename {luoo_user/src/main/java/com/luoo/user/pojo => luoo_common/src/main/java/mongodb}/UserCollect.java (66%) create mode 100644 luoo_music/src/main/java/com/luoo/music/dao/UserCollectDao.java diff --git a/luoo_common/pom.xml b/luoo_common/pom.xml index c4a4784..eeec0f8 100644 --- a/luoo_common/pom.xml +++ b/luoo_common/pom.xml @@ -29,5 +29,9 @@ com.github.xiaoymin swagger-bootstrap-ui + + org.springframework.boot + spring-boot-starter-data-mongodb + diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/UserCollect.java b/luoo_common/src/main/java/mongodb/UserCollect.java similarity index 66% rename from luoo_user/src/main/java/com/luoo/user/pojo/UserCollect.java rename to luoo_common/src/main/java/mongodb/UserCollect.java index b09d5e2..8c365bd 100644 --- a/luoo_user/src/main/java/com/luoo/user/pojo/UserCollect.java +++ b/luoo_common/src/main/java/mongodb/UserCollect.java @@ -1,31 +1,22 @@ -package com.luoo.user.pojo; - -import com.fasterxml.jackson.annotation.JsonIgnore; +package mongodb; import lombok.Getter; import lombok.Setter; import java.util.Date; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.Table; - import com.fasterxml.jackson.annotation.JsonFormat; + +import org.springframework.data.annotation.Id; import org.springframework.format.annotation.DateTimeFormat; import java.io.Serializable; - /** * 用户收藏 */ @Getter @Setter -@Entity -@Table(name="tb_user_collect") public class UserCollect implements Serializable { private static final long serialVersionUID = 1L; @@ -33,8 +24,7 @@ public class UserCollect implements Serializable { * 收藏ID */ @Id - @GeneratedValue(strategy = GenerationType.SEQUENCE) - private Integer collectId; + private String collectId; /** * 用户ID diff --git a/luoo_music/src/main/java/com/luoo/music/client/UserClient.java b/luoo_music/src/main/java/com/luoo/music/client/UserClient.java index 6a7099d..336beac 100644 --- a/luoo_music/src/main/java/com/luoo/music/client/UserClient.java +++ b/luoo_music/src/main/java/com/luoo/music/client/UserClient.java @@ -2,12 +2,11 @@ package com.luoo.music.client; import api.Result; import org.springframework.cloud.openfeign.FeignClient; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.GetMapping; @FeignClient("luoo-user") public interface UserClient { - @RequestMapping(value = "/admin",method= RequestMethod.GET) + @GetMapping("/admin") public Result findAll(); } 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 2e9d191..161dca6 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 @@ -1,17 +1,15 @@ package com.luoo.music.controller; import java.util.Date; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; -import com.luoo.music.client.UserClient; +import com.luoo.music.dao.UserCollectDao; import com.luoo.music.dto.request.JournalQueryReq; import com.luoo.music.dto.response.JournalRespDTO; -import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; +import org.springframework.util.StopWatch; import org.springframework.web.bind.annotation.*; import com.luoo.music.pojo.Article; import com.luoo.music.service.ArticleService; @@ -22,8 +20,10 @@ import api.PageResult; import api.Result; import constants.Constants; import dto.UserLoginDto; +import enums.CollectTypeEnum; import enums.DateTimePatternEnum; import io.swagger.annotations.ApiOperation; +import mongodb.UserCollect; import util.DateUtil; import util.JwtUtil; @@ -36,7 +36,8 @@ import util.JwtUtil; @CrossOrigin @RequestMapping("/journal") public class JournalController { - + @Autowired + private UserCollectDao userCollectDao; @Autowired private ArticleService articleService; @Autowired @@ -57,14 +58,25 @@ public class JournalController { List list=pageList.stream().map(a->getArticleRespDTO(a,user)).collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()), list) ); } - private JournalRespDTO getArticleRespDTO(Article article, UserLoginDto user) { + private JournalRespDTO getArticleRespDTO(Article article, UserLoginDto userLoginDto) { + StopWatch sw=new StopWatch(); + sw.start("stage1"); JournalRespDTO journalRespDTO=new JournalRespDTO(); journalRespDTO.setId(article.getId()); journalRespDTO.setJournalNo(article.getVolid()); journalRespDTO.setTitle(article.getTitle()); journalRespDTO.setImage(Constants.MUSIC_RESOURCE_PREFIX+article.getImage()); journalRespDTO.setDate(getEditDate(article)); - //TODO: 根据userId查询是否已收藏 + sw.stop(); + System.out.println(sw.prettyPrint()); + sw.start("stage2"); + //根据userId查询是否已收藏 + if(null!=userLoginDto) { + long userCollectCount=userCollectDao.countByUserIdAndObjectIdAndCollectType(userLoginDto.getUserId(), journalRespDTO.getId(), CollectTypeEnum.JOURNAL.getType()); + journalRespDTO.setHaveCollect(userCollectCount>0); + } + sw.stop(); + System.out.println(sw.prettyPrint()); return journalRespDTO; } private String getEditDate(Article article) { 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 2415217..9fad81d 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 @@ -1,5 +1,7 @@ package com.luoo.music.controller; +import com.luoo.music.client.UserClient; +import com.luoo.music.dao.UserCollectDao; import com.luoo.music.dto.response.SongRespDTO; import com.luoo.music.pojo.Article; import com.luoo.music.pojo.Song; @@ -12,10 +14,12 @@ import api.PageResult; import api.Result; import constants.Constants; import dto.UserLoginDto; +import enums.CollectTypeEnum; import enums.VerifyRegexEnum; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; +import mongodb.UserCollect; import util.JwtUtil; import org.springframework.beans.factory.annotation.Autowired; @@ -32,13 +36,15 @@ import java.util.stream.Collectors; @CrossOrigin @RequestMapping("/song") public class SongController { - + @Autowired + private UserCollectDao userCollectDao; @Autowired private SongService songService; @Autowired private ArticleService articleService; @Autowired private JwtUtil jwtUtil; + /** * 查询全部数据 * @@ -49,17 +55,17 @@ public class SongController { return Result.success(songService.findAll()); } - @ApiOperation(value = "1.根据期刊号查询歌曲信息",notes="若为游客,期刊号须在最新10期内") + @ApiOperation(value = "1.根据期刊号查询歌曲信息", notes = "若为游客,期刊号须在最新10期内") @GetMapping("/getByJournalNo/{journalNo}") @GlobalInterceptor public Result> getByJournalNo(@RequestHeader(value = "token", required = false) String token, @PathVariable String journalNo) { - UserLoginDto user=jwtUtil.getUserLoginDto(token); - if(null==user&&!isLatest10(journalNo)) { + UserLoginDto user = jwtUtil.getUserLoginDto(token); + if (null == user && !isLatest10(journalNo)) { return Result.unauthorized(null); } List songs = songService.findByVolid(journalNo); - List results = songs.stream().map(s -> getSongRespDTO(s)).collect(Collectors.toList()); + List results = songs.stream().map(s -> getSongRespDTO(s, user)).collect(Collectors.toList()); return Result.success(results); } @@ -68,18 +74,17 @@ public class SongController { } @ApiOperation(value = "2.随机播放歌曲", notes = "雀乐FM") - @ApiImplicitParams({ - @ApiImplicitParam(name = "limit", value = "随机歌曲数,最少1首,最多30首", required = false) - }) + @ApiImplicitParams({ @ApiImplicitParam(name = "limit", value = "随机歌曲数,最少1首,最多30首", required = false) }) @GetMapping("/random/{limit}") @GlobalInterceptor - public Result> random(@PathVariable @VerifyParam(required=true,regex=VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) { + public Result> random( + @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()); + List results = songs.stream().map(s -> getSongRespDTO(s, null)).collect(Collectors.toList()); return Result.success(results); } - private SongRespDTO getSongRespDTO(Song song) { + private SongRespDTO getSongRespDTO(Song song, UserLoginDto userLoginDto) { SongRespDTO songRespDTO = new SongRespDTO(); songRespDTO.setJournalNo(song.getVolid()); songRespDTO.setAlbum(song.getAlbum()); @@ -90,6 +95,11 @@ public class SongController { songRespDTO.setPic(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".jpg")); songRespDTO.setLrc(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".lyric")); songRespDTO.setSongNo(song.getSongno()); + // 根据userId查询是否已收藏 + if (null != userLoginDto) { + long userCollectCount=userCollectDao.countByUserIdAndObjectIdAndCollectType(userLoginDto.getUserId(), songRespDTO.getId(), CollectTypeEnum.SONG.getType()); + songRespDTO.setHaveCollect(userCollectCount>0); + } return songRespDTO; } diff --git a/luoo_music/src/main/java/com/luoo/music/dao/UserCollectDao.java b/luoo_music/src/main/java/com/luoo/music/dao/UserCollectDao.java new file mode 100644 index 0000000..50e64f9 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dao/UserCollectDao.java @@ -0,0 +1,10 @@ +package com.luoo.music.dao; + + +import org.springframework.data.mongodb.repository.MongoRepository; + +import mongodb.UserCollect; + +public interface UserCollectDao extends MongoRepository { + public long countByUserIdAndObjectIdAndCollectType(String userId, String objectId, Integer collectType); +} diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/SongRespDTO.java b/luoo_music/src/main/java/com/luoo/music/dto/response/SongRespDTO.java index 2ee2a7d..54a6e59 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/response/SongRespDTO.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/SongRespDTO.java @@ -23,4 +23,6 @@ public class SongRespDTO { private String journalNo; @ApiModelProperty(value = "歌曲号") private Integer songNo; + @ApiModelProperty(value = "已收藏") + private boolean haveCollect; } diff --git a/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java b/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java index 803de31..bbc37a0 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java @@ -1,7 +1,9 @@ package com.luoo.user.controller; +import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; diff --git a/luoo_user/src/main/java/com/luoo/user/dao/UserCollectDao.java b/luoo_user/src/main/java/com/luoo/user/dao/UserCollectDao.java index 5dfe209..b16061a 100644 --- a/luoo_user/src/main/java/com/luoo/user/dao/UserCollectDao.java +++ b/luoo_user/src/main/java/com/luoo/user/dao/UserCollectDao.java @@ -1,11 +1,11 @@ package com.luoo.user.dao; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.JpaSpecificationExecutor; -import com.luoo.user.pojo.UserCollect; +import org.springframework.data.mongodb.repository.MongoRepository; -public interface UserCollectDao extends JpaRepository, JpaSpecificationExecutor { +import mongodb.UserCollect; + +public interface UserCollectDao extends MongoRepository { public UserCollect findByUserIdAndObjectIdAndCollectType(String userId, String objectId, Integer collectType); public long deleteByUserIdAndObjectIdAndCollectType(String userId, String objectId, Integer collectType); } diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserCollectService.java b/luoo_user/src/main/java/com/luoo/user/service/UserCollectService.java index 34426df..d1c7d10 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserCollectService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserCollectService.java @@ -7,16 +7,13 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.luoo.user.dao.UserCollectDao; -import com.luoo.user.dao.UserInfoDao; -import com.luoo.user.pojo.UserCollect; import api.StatusCode; import enums.CollectTypeEnum; import exception.BizException; - +import mongodb.UserCollect; @Service -@Transactional public class UserCollectService { @Autowired private UserCollectDao userCollectDao; @@ -41,5 +38,4 @@ public class UserCollectService { Integer collectType) { userCollectDao.deleteByUserIdAndObjectIdAndCollectType(userId, objectId, collectType); } - } diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java index c04a02d..906a011 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java @@ -39,7 +39,6 @@ import util.JwtUtil; * */ @Service -@Transactional public class UserInfoService { @Autowired private UserInfoDao userInfoDao;