1.enable collect interface

main
Gary 10 months ago
parent 12bf2c7468
commit ec58f867e4

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

@ -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

@ -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();
}

@ -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<JournalRespDTO> list=pageList.stream().map(a->getArticleRespDTO(a,user)).collect(Collectors.toList());
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.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) {

@ -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;
/**
*
*
@ -59,7 +65,7 @@ public class SongController {
return Result.unauthorized(null);
}
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);
}
@ -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<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<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);
}
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;
}

@ -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;
@ApiModelProperty(value = "歌曲号")
private Integer songNo;
@ApiModelProperty(value = "已收藏")
private boolean haveCollect;
}

@ -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;

@ -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<UserCollect, Integer>, JpaSpecificationExecutor<UserCollect> {
import mongodb.UserCollect;
public interface UserCollectDao extends MongoRepository<UserCollect, String> {
public UserCollect findByUserIdAndObjectIdAndCollectType(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 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);
}
}

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

Loading…
Cancel
Save