diff --git a/luoo_user/src/main/java/com/luoo/user/controller/MyController.java b/luoo_user/src/main/java/com/luoo/user/controller/MyController.java index 1a9609e..a4c18af 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/MyController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/MyController.java @@ -8,6 +8,7 @@ import java.nio.file.Paths; import java.util.Date; import java.util.EnumMap; import java.util.List; +import java.util.Set; import com.luoo.user.pojo.UserCollectInfo; import com.luoo.user.service.UserCollectInfoService; @@ -98,16 +99,16 @@ public class MyController extends BaseController{ } UserCollectInfo userCollectInfo = userCollectInfoService.findByUserId(userLoginDto.getUserId()); if(null!=userCollectInfo) { - List songList =userCollectInfo.getSongList(); - List journalList = userCollectInfo.getJournalList(); - userRespDTO.setJournalList(journalList); - userRespDTO.setSongList(songList); - if(null != songList){ - userRespDTO.setSongCount(songList.size()); + Set songSet =userCollectInfo.getSongSet(); + Set journalSet = userCollectInfo.getJournalSet(); + userRespDTO.setJournalSet(journalSet); + userRespDTO.setSongSet(songSet); + if(null != songSet){ + userRespDTO.setSongCount(songSet.size()); } - if(null != journalList) { - userRespDTO.setJournalCount(journalList.size()); + if(null != journalSet) { + userRespDTO.setJournalCount(journalSet.size()); } } diff --git a/luoo_user/src/main/java/com/luoo/user/controller/UserCollectInfoController.java b/luoo_user/src/main/java/com/luoo/user/controller/UserCollectInfoController.java index 753009e..493cd25 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/UserCollectInfoController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/UserCollectInfoController.java @@ -4,14 +4,19 @@ package com.luoo.user.controller; import api.Result; import com.luoo.user.pojo.UserCollectInfo; import com.luoo.user.service.UserCollectInfoService; +import controller.BaseController; +import dto.UserLoginDto; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -@RestController -@RequestMapping("/UserCollectInfo") -public class UserCollectInfoController { +import javax.servlet.http.HttpServletRequest; +@RestController +@RequestMapping("/userCollectInfo") +public class UserCollectInfoController extends BaseController { + @Autowired + private HttpServletRequest request; @Autowired private UserCollectInfoService userCollectInfoService; @@ -27,11 +32,60 @@ public class UserCollectInfoController { UserCollectInfo userCollectInfo =userCollectInfoService.findByUserId("111222"); return Result.success(userCollectInfo); } +// +// @PutMapping("/unCollect") +// public Result unCollect() { +// userCollectInfoService.unCollect(); +// return Result.success(); +// } + + + + + /** + * 收藏 type为journal 为期刊,type为song为单曲 + */ - @PutMapping("/unCollect") - public Result unCollect() { - userCollectInfoService.unCollect(); + @PostMapping("/collect/{type}/{id}") + public Result collect(@PathVariable String type,@PathVariable String id) { + //验证是否登录,并且拿到ID + String token = (String) request.getAttribute("claims_user"); + UserLoginDto userLoginDto = getUserLoginDto(token); + if (null == userLoginDto) { + return Result.unauthorized(null); + } + String userid = userLoginDto.getUserId(); + + if("journal".equals(type)) { + userCollectInfoService.collectJournal(userid,id); + } + if("song".equals(type)) { + userCollectInfoService.collectSong(userid,id); + } + + return Result.success(); + } + + /** + * 收藏 type为journal 为期刊,type为song为单曲 + */ + @PostMapping("/unCollect/{type}/{id}") + public Result unCollect(@PathVariable String type,@PathVariable String id) { + //验证是否登录,并且拿到ID + String token = (String) request.getAttribute("claims_user"); + UserLoginDto userLoginDto = getUserLoginDto(token); + if (null == userLoginDto) { + return Result.unauthorized(null); + } + String userid = userLoginDto.getUserId(); + if("journal".equals(type)) { + userCollectInfoService.unCollectJournal(userid,id); + } + if("song".equals(type)) { + userCollectInfoService.unCollectSong(userid,id); + } return Result.success(); } + } diff --git a/luoo_user/src/main/java/com/luoo/user/dto/response/UserRespDTO.java b/luoo_user/src/main/java/com/luoo/user/dto/response/UserRespDTO.java index ccf5117..70a0358 100644 --- a/luoo_user/src/main/java/com/luoo/user/dto/response/UserRespDTO.java +++ b/luoo_user/src/main/java/com/luoo/user/dto/response/UserRespDTO.java @@ -6,6 +6,7 @@ import io.swagger.annotations.ApiModelProperty; import lombok.Data; import java.util.List; +import java.util.Set; @Data public class UserRespDTO { @@ -30,11 +31,11 @@ public class UserRespDTO { @ApiModelProperty(value = "喜欢歌曲数") private int songCount; - private List songList; + private Set songSet; @ApiModelProperty(value = "收藏期刊数") private int journalCount; - private List journalList; + private Set journalSet; @ApiModelProperty(value = "获得评论数") private int commentReplyCount; @ApiModelProperty(value = "生日,格式为: yyyy.MM.dd") diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/UserCollectInfo.java b/luoo_user/src/main/java/com/luoo/user/pojo/UserCollectInfo.java index 2313084..8034b6d 100644 --- a/luoo_user/src/main/java/com/luoo/user/pojo/UserCollectInfo.java +++ b/luoo_user/src/main/java/com/luoo/user/pojo/UserCollectInfo.java @@ -8,6 +8,7 @@ import org.springframework.format.annotation.DateTimeFormat; import java.io.Serializable; import java.util.Date; import java.util.List; +import java.util.Set; @Data @@ -29,12 +30,12 @@ public class UserCollectInfo implements Serializable { /** * 收藏的单曲 */ - private List songList; + private Set songSet; /** * 收藏的期刊 */ - private List journalList; + private Set journalSet; } diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserCollectInfoService.java b/luoo_user/src/main/java/com/luoo/user/service/UserCollectInfoService.java index 28e31b8..154c97f 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserCollectInfoService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserCollectInfoService.java @@ -9,10 +9,7 @@ import org.springframework.data.annotation.CreatedDate; import org.springframework.stereotype.Service; import util.IdWorker; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; @Service public class UserCollectInfoService { @@ -38,7 +35,7 @@ public class UserCollectInfoService { List list = new ArrayList(); list.add(userCollectSongDto); list.add(userCollectSongDto1); - userCollectInfo.setSongList(list); +// userCollectInfo.setSongList(list); userCollectInfo.setCollectId(idWorker.nextId()+""); userCollectInfo.setUserId("111222"); userCollectInfoDao.save(userCollectInfo); @@ -50,12 +47,76 @@ public class UserCollectInfoService { return userCollectInfoDao.findUserCollectInfoByUserId(userId); } - public void unCollect(){ - UserCollectInfo userCollectInfo = userCollectInfoDao.findUserCollectInfoByUserId("111222"); - UserCollectSongDto userCollectSongDto = new UserCollectSongDto(); - userCollectSongDto.setName("smell like teensprit"); - userCollectSongDto.setArtist("Nirvana"); - userCollectInfo.getSongList().remove(userCollectSongDto); - userCollectInfoDao.save(userCollectInfo); - } + + public void collectSong(String userid, String id) { + + UserCollectInfo userCollectInfo = userCollectInfoDao.findUserCollectInfoByUserId(userid); + if (null == userCollectInfo) { + userCollectInfo = new UserCollectInfo(); + userCollectInfo.setUserId(userid); + Set songSet = new HashSet(); + songSet.add(id); + userCollectInfo.setSongSet(songSet); + userCollectInfoDao.save(userCollectInfo); + } else { + Set songSet = userCollectInfo.getSongSet(); + if(null == songSet) { + songSet = new HashSet<>(); + } + songSet.add(id); + + userCollectInfo.setSongSet(songSet); + userCollectInfoDao.save(userCollectInfo); + } + + + } + + public void collectJournal(String userid, String id) { + + + UserCollectInfo userCollectInfo = userCollectInfoDao.findUserCollectInfoByUserId(userid); + if (null == userCollectInfo) { + userCollectInfo = new UserCollectInfo(); + userCollectInfo.setUserId(userid); + Set journalSet = new HashSet(); + journalSet.add(id); + userCollectInfo.setJournalSet(journalSet); + userCollectInfoDao.save(userCollectInfo); + } else { + Set journalSet =userCollectInfo.getJournalSet(); + if(null== journalSet) { + journalSet = new HashSet(); + } + journalSet.add(id); + userCollectInfo.setJournalSet(journalSet); + userCollectInfoDao.save(userCollectInfo); + } + + } + + /** + * 方法过期 + */ + public void unCollect(){ + UserCollectInfo userCollectInfo = userCollectInfoDao.findUserCollectInfoByUserId("111222"); + UserCollectSongDto userCollectSongDto = new UserCollectSongDto(); + userCollectSongDto.setName("smell like teensprit"); + userCollectSongDto.setArtist("Nirvana"); +// userCollectInfo.getSongList().remove(userCollectSongDto); + userCollectInfoDao.save(userCollectInfo); + } + + public void unCollectJournal(String userid, String id) { + UserCollectInfo userCollectInfo = userCollectInfoDao.findUserCollectInfoByUserId(userid); + userCollectInfo.getJournalSet().remove(id); + userCollectInfoDao.save(userCollectInfo); + } + + public void unCollectSong(String userid, String id) { + UserCollectInfo userCollectInfo = userCollectInfoDao.findUserCollectInfoByUserId(userid); + userCollectInfo.getSongSet().remove(id); + userCollectInfoDao.save(userCollectInfo); + + } }