1.enable collect interface

main
Gary 10 months ago
parent 12bf2c7468
commit ec58f867e4

@ -29,5 +29,9 @@
<groupId>com.github.xiaoymin</groupId> <groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId> <artifactId>swagger-bootstrap-ui</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -1,31 +1,22 @@
package com.luoo.user.pojo; package mongodb;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.util.Date; 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 com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.data.annotation.Id;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable; import java.io.Serializable;
/** /**
* *
*/ */
@Getter @Getter
@Setter @Setter
@Entity
@Table(name="tb_user_collect")
public class UserCollect implements Serializable { public class UserCollect implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ -33,8 +24,7 @@ public class UserCollect implements Serializable {
* ID * ID
*/ */
@Id @Id
@GeneratedValue(strategy = GenerationType.SEQUENCE) private String collectId;
private Integer collectId;
/** /**
* ID * ID

@ -2,12 +2,11 @@ package com.luoo.music.client;
import api.Result; import api.Result;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient("luoo-user") @FeignClient("luoo-user")
public interface UserClient { public interface UserClient {
@RequestMapping(value = "/admin",method= RequestMethod.GET) @GetMapping("/admin")
public Result findAll(); public Result findAll();
} }

@ -1,17 +1,15 @@
package com.luoo.music.controller; package com.luoo.music.controller;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; 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.request.JournalQueryReq;
import com.luoo.music.dto.response.JournalRespDTO; import com.luoo.music.dto.response.JournalRespDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import com.luoo.music.pojo.Article; import com.luoo.music.pojo.Article;
import com.luoo.music.service.ArticleService; import com.luoo.music.service.ArticleService;
@ -22,8 +20,10 @@ import api.PageResult;
import api.Result; import api.Result;
import constants.Constants; import constants.Constants;
import dto.UserLoginDto; import dto.UserLoginDto;
import enums.CollectTypeEnum;
import enums.DateTimePatternEnum; import enums.DateTimePatternEnum;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import mongodb.UserCollect;
import util.DateUtil; import util.DateUtil;
import util.JwtUtil; import util.JwtUtil;
@ -36,7 +36,8 @@ import util.JwtUtil;
@CrossOrigin @CrossOrigin
@RequestMapping("/journal") @RequestMapping("/journal")
public class JournalController { public class JournalController {
@Autowired
private UserCollectDao userCollectDao;
@Autowired @Autowired
private ArticleService articleService; private ArticleService articleService;
@Autowired @Autowired
@ -57,14 +58,25 @@ public class JournalController {
List<JournalRespDTO> list=pageList.stream().map(a->getArticleRespDTO(a,user)).collect(Collectors.toList()); List<JournalRespDTO> list=pageList.stream().map(a->getArticleRespDTO(a,user)).collect(Collectors.toList());
return Result.success(new PageResult<JournalRespDTO>(Long.valueOf(list.size()), list) ); return Result.success(new PageResult<JournalRespDTO>(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 journalRespDTO=new JournalRespDTO();
journalRespDTO.setId(article.getId()); journalRespDTO.setId(article.getId());
journalRespDTO.setJournalNo(article.getVolid()); journalRespDTO.setJournalNo(article.getVolid());
journalRespDTO.setTitle(article.getTitle()); journalRespDTO.setTitle(article.getTitle());
journalRespDTO.setImage(Constants.MUSIC_RESOURCE_PREFIX+article.getImage()); journalRespDTO.setImage(Constants.MUSIC_RESOURCE_PREFIX+article.getImage());
journalRespDTO.setDate(getEditDate(article)); 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; return journalRespDTO;
} }
private String getEditDate(Article article) { private String getEditDate(Article article) {

@ -1,5 +1,7 @@
package com.luoo.music.controller; 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.dto.response.SongRespDTO;
import com.luoo.music.pojo.Article; import com.luoo.music.pojo.Article;
import com.luoo.music.pojo.Song; import com.luoo.music.pojo.Song;
@ -12,10 +14,12 @@ import api.PageResult;
import api.Result; import api.Result;
import constants.Constants; import constants.Constants;
import dto.UserLoginDto; import dto.UserLoginDto;
import enums.CollectTypeEnum;
import enums.VerifyRegexEnum; import enums.VerifyRegexEnum;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import mongodb.UserCollect;
import util.JwtUtil; import util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -32,13 +36,15 @@ import java.util.stream.Collectors;
@CrossOrigin @CrossOrigin
@RequestMapping("/song") @RequestMapping("/song")
public class SongController { public class SongController {
@Autowired
private UserCollectDao userCollectDao;
@Autowired @Autowired
private SongService songService; private SongService songService;
@Autowired @Autowired
private ArticleService articleService; private ArticleService articleService;
@Autowired @Autowired
private JwtUtil jwtUtil; private JwtUtil jwtUtil;
/** /**
* *
* *
@ -59,7 +65,7 @@ public class SongController {
return Result.unauthorized(null); return Result.unauthorized(null);
} }
List<Song> songs = songService.findByVolid(journalNo); List<Song> songs = songService.findByVolid(journalNo);
List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s)).collect(Collectors.toList()); List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, user)).collect(Collectors.toList());
return Result.success(results); return Result.success(results);
} }
@ -68,18 +74,17 @@ public class SongController {
} }
@ApiOperation(value = "2.随机播放歌曲", notes = "雀乐FM") @ApiOperation(value = "2.随机播放歌曲", notes = "雀乐FM")
@ApiImplicitParams({ @ApiImplicitParams({ @ApiImplicitParam(name = "limit", value = "随机歌曲数最少1首最多30首", required = false) })
@ApiImplicitParam(name = "limit", value = "随机歌曲数最少1首最多30首", required = false)
})
@GetMapping("/random/{limit}") @GetMapping("/random/{limit}")
@GlobalInterceptor @GlobalInterceptor
public Result<List<SongRespDTO>> random(@PathVariable @VerifyParam(required=true,regex=VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) { public Result<List<SongRespDTO>> random(
@PathVariable @VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) {
List<Song> songs = songService.random(limit); List<Song> songs = songService.random(limit);
List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s)).collect(Collectors.toList()); List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, null)).collect(Collectors.toList());
return Result.success(results); return Result.success(results);
} }
private SongRespDTO getSongRespDTO(Song song) { private SongRespDTO getSongRespDTO(Song song, UserLoginDto userLoginDto) {
SongRespDTO songRespDTO = new SongRespDTO(); SongRespDTO songRespDTO = new SongRespDTO();
songRespDTO.setJournalNo(song.getVolid()); songRespDTO.setJournalNo(song.getVolid());
songRespDTO.setAlbum(song.getAlbum()); songRespDTO.setAlbum(song.getAlbum());
@ -90,6 +95,11 @@ public class SongController {
songRespDTO.setPic(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".jpg")); songRespDTO.setPic(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".jpg"));
songRespDTO.setLrc(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".lyric")); songRespDTO.setLrc(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".lyric"));
songRespDTO.setSongNo(song.getSongno()); 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; return songRespDTO;
} }

@ -0,0 +1,10 @@
package com.luoo.music.dao;
import org.springframework.data.mongodb.repository.MongoRepository;
import mongodb.UserCollect;
public interface UserCollectDao extends MongoRepository<UserCollect, String> {
public long countByUserIdAndObjectIdAndCollectType(String userId, String objectId, Integer collectType);
}

@ -23,4 +23,6 @@ public class SongRespDTO {
private String journalNo; private String journalNo;
@ApiModelProperty(value = "歌曲号") @ApiModelProperty(value = "歌曲号")
private Integer songNo; private Integer songNo;
@ApiModelProperty(value = "已收藏")
private boolean haveCollect;
} }

@ -1,7 +1,9 @@
package com.luoo.user.controller; package com.luoo.user.controller;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping; 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.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;

@ -1,11 +1,11 @@
package com.luoo.user.dao; package com.luoo.user.dao;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import com.luoo.user.pojo.UserCollect;
public interface UserCollectDao extends JpaRepository<UserCollect, Integer>, JpaSpecificationExecutor<UserCollect> { import mongodb.UserCollect;
public interface UserCollectDao extends MongoRepository<UserCollect, String> {
public UserCollect findByUserIdAndObjectIdAndCollectType(String userId, String objectId, Integer collectType); public UserCollect findByUserIdAndObjectIdAndCollectType(String userId, String objectId, Integer collectType);
public long deleteByUserIdAndObjectIdAndCollectType(String userId, String objectId, Integer collectType); public long deleteByUserIdAndObjectIdAndCollectType(String userId, String objectId, Integer collectType);
} }

@ -7,16 +7,13 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import com.luoo.user.dao.UserCollectDao; import com.luoo.user.dao.UserCollectDao;
import com.luoo.user.dao.UserInfoDao;
import com.luoo.user.pojo.UserCollect;
import api.StatusCode; import api.StatusCode;
import enums.CollectTypeEnum; import enums.CollectTypeEnum;
import exception.BizException; import exception.BizException;
import mongodb.UserCollect;
@Service @Service
@Transactional
public class UserCollectService { public class UserCollectService {
@Autowired @Autowired
private UserCollectDao userCollectDao; private UserCollectDao userCollectDao;
@ -41,5 +38,4 @@ public class UserCollectService {
Integer collectType) { Integer collectType) {
userCollectDao.deleteByUserIdAndObjectIdAndCollectType(userId, objectId, collectType); userCollectDao.deleteByUserIdAndObjectIdAndCollectType(userId, objectId, collectType);
} }
} }

@ -39,7 +39,6 @@ import util.JwtUtil;
* *
*/ */
@Service @Service
@Transactional
public class UserInfoService { public class UserInfoService {
@Autowired @Autowired
private UserInfoDao userInfoDao; private UserInfoDao userInfoDao;

Loading…
Cancel
Save