From 4a8ca4f2e654d287f2c61200602a657c936adb13 Mon Sep 17 00:00:00 2001 From: wangqing <408564126@qq.com> Date: Thu, 25 Jan 2024 13:14:25 +0800 Subject: [PATCH] move friend service to luoo_user --- .../user/controller/FriendController.java | 89 +++++++++++++++++++ .../luoo/user/controller/MyController.java | 44 ++++++--- .../controller/UserCollectInfoController.java | 2 +- .../java/com/luoo/user/dao/FriendDao.java | 19 ++++ .../java/com/luoo/user/dao/NoFriendDao.java | 11 +++ .../luoo/user/dto/response/UserRespDTO.java | 8 ++ .../main/java/com/luoo/user/pojo/Friend.java | 45 ++++++++++ .../java/com/luoo/user/pojo/NoFriend.java | 38 ++++++++ .../com/luoo/user/service/FriendService.java | 72 +++++++++++++++ .../user/service/UserCollectInfoService.java | 4 +- 10 files changed, 315 insertions(+), 17 deletions(-) create mode 100644 luoo_user/src/main/java/com/luoo/user/controller/FriendController.java create mode 100644 luoo_user/src/main/java/com/luoo/user/dao/FriendDao.java create mode 100644 luoo_user/src/main/java/com/luoo/user/dao/NoFriendDao.java create mode 100644 luoo_user/src/main/java/com/luoo/user/pojo/Friend.java create mode 100644 luoo_user/src/main/java/com/luoo/user/pojo/NoFriend.java create mode 100644 luoo_user/src/main/java/com/luoo/user/service/FriendService.java diff --git a/luoo_user/src/main/java/com/luoo/user/controller/FriendController.java b/luoo_user/src/main/java/com/luoo/user/controller/FriendController.java new file mode 100644 index 0000000..84a7399 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/controller/FriendController.java @@ -0,0 +1,89 @@ +package com.luoo.user.controller; + + +import api.Result; +import api.StatusCode; +import com.luoo.user.service.FriendService; +import com.luoo.user.service.UserInfoService; +import controller.BaseController; +import dto.UserLoginDto; +import io.jsonwebtoken.Claims; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; + +@RestController +@RequestMapping("/friend") +public class FriendController extends BaseController { + + @Autowired + private HttpServletRequest request; + + @Autowired + private FriendService friendService; + + @Autowired + private UserInfoService userInfoService; + + + /** + * 添加好友或者添加非好友(关注还是拉黑) 1是关注,2是拉黑 + * + * @return + */ + @PutMapping("/like/{friendid}/{type}") + public Result addFriend(@PathVariable String friendid, @PathVariable String type) { + + //验证是否登录,并且拿到ID + String token = (String) request.getAttribute("claims_user"); + UserLoginDto userLoginDto = getUserLoginDto(token); + if (null == userLoginDto) { + return Result.unauthorized(null); + } + String userid = userLoginDto.getUserId(); + //判断是添加好友还是非好友(判断是关注操作还是拉黑操作) + if (type != null) { + if (type.equals("1")) { + //添加好友 + int flag = friendService.addFriend(userid,friendid); + if (flag == 0) { + return Result.failed(StatusCode.FRIEND_CAN_NOT_REPEAT_ADD_FRIEND); // 不能重复添加好友 + } + if (flag == 1) { + userInfoService.updatefanscountandfollowcount(1,userid,friendid); + return Result.success(); + } + + } else if (type.equals("2")) { + //添加非好友 + int flag = friendService.addNoFriend(userid,friendid); + if (flag == 0) { + return Result.failed(StatusCode.FRIEND_CAN_NOT_REPEAT_ADD_NON_FRIEND); + } + if (flag == 1) { + return Result.success(); + } + } + return Result.failed(StatusCode.VALIDATE_FAILED); + } else { + return Result.failed(StatusCode.VALIDATE_FAILED); + } + } + + + @DeleteMapping("/{friendid}") + public Result deleteFriend(@PathVariable String friendid){ + + //验证是否登录,并且拿到ID + String token = (String) request.getAttribute("claims_user"); + UserLoginDto userLoginDto = getUserLoginDto(token); + if (null == userLoginDto) { + return Result.unauthorized(null); + } + String userid = userLoginDto.getUserId(); + friendService.deleteFriend(userid,friendid); + userInfoService.updatefanscountandfollowcount(-1,userid,friendid); + return Result.success(); + } +} 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 0eae87f..15924c7 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 @@ -7,7 +7,10 @@ import java.io.InputStream; import java.nio.file.Paths; import java.util.Date; import java.util.EnumMap; +import java.util.List; +import com.luoo.user.pojo.UserCollectInfo; +import com.luoo.user.service.UserCollectInfoService; import org.apache.commons.io.IOUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -27,7 +30,6 @@ import controller.BaseController; import com.luoo.user.dto.response.UserRespDTO; import com.luoo.user.pojo.UserInfo; import com.luoo.user.service.S3Service; -import com.luoo.user.service.UserCollectService; import com.luoo.user.service.UserInfoService; import annotation.GlobalInterceptor; @@ -56,7 +58,9 @@ public class MyController extends BaseController{ private UserInfoService userInfoService; @Autowired - private UserCollectService userCollectService; + private UserCollectInfoService userCollectInfoService; +// private UserCollectService userCollectService; + public static String UPLOAD_DIRECTORY = "user/avatar/"; @@ -70,20 +74,32 @@ public class MyController extends BaseController{ } UserRespDTO userRespDTO = new UserRespDTO(); UserInfo user = userInfoService.findById(userLoginDto.getUserId()); + UserCollectInfo userCollectInfo = userCollectInfoService.findByUserId(userLoginDto.getUserId()); + List songList =userCollectInfo.getSongList(); + List journalList = userCollectInfo.getJournalList(); BeanUtils.copyProperties(user, userRespDTO); //EnumMap map=userCollectService.getUserCollectTypeMap(user.getId()); - int fansCount=userCollectService.getFansCount(user.getId()); - int followCount=userCollectService.getCount(user.getId(),CollectTypeEnum.FOLLOW.getType()); - int thumbUpCount=userCollectService.getCount(user.getId(),CollectTypeEnum.THUMB_UP.getType()); - int songCount=userCollectService.getCount(user.getId(),CollectTypeEnum.SONG.getType()); - int journalCount=userCollectService.getCount(user.getId(),CollectTypeEnum.JOURNAL.getType()); - - userRespDTO.setFollowCount(followCount); - userRespDTO.setFansCount(fansCount); - userRespDTO.setThumbUpCount(thumbUpCount); - userRespDTO.setCommentReplyCount(0); - userRespDTO.setSongCount(songCount); - userRespDTO.setJournalCount(journalCount); +// int fansCount=userCollectService.getFansCount(user.getId()); +// int followCount=userCollectService.getCount(user.getId(),CollectTypeEnum.FOLLOW.getType()); +// int thumbUpCount=userCollectService.getCount(user.getId(),CollectTypeEnum.THUMB_UP.getType()); +// int songCount=userCollectService.getCount(user.getId(),CollectTypeEnum.SONG.getType()); +// int journalCount=userCollectService.getCount(user.getId(),CollectTypeEnum.JOURNAL.getType()); +// +// userRespDTO.setFollowCount(followCount); +// userRespDTO.setFansCount(fansCount); +// userRespDTO.setThumbUpCount(thumbUpCount); +// userRespDTO.setCommentReplyCount(0); +// userRespDTO.setSongCount(songCount); +// userRespDTO.setJournalCount(journalCount); + userRespDTO.setJournalList(journalList); + userRespDTO.setSongList(songList); + if(null != songList){ + userRespDTO.setSongCount(songList.size()); + } + + if(null != journalList) { + userRespDTO.setJournalCount(journalList.size()); + } if(null!=user.getBirthday()) { userRespDTO.setBirthDay(DateUtil.format(user.getBirthday(), DateTimePatternEnum.YYYY_DOT_MM_DOT_DD.getPattern())); } 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 65f1207..753009e 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 @@ -24,7 +24,7 @@ public class UserCollectInfoController { @GetMapping("/show") public Result show(){ - UserCollectInfo userCollectInfo =userCollectInfoService.findByUserId(); + UserCollectInfo userCollectInfo =userCollectInfoService.findByUserId("111222"); return Result.success(userCollectInfo); } diff --git a/luoo_user/src/main/java/com/luoo/user/dao/FriendDao.java b/luoo_user/src/main/java/com/luoo/user/dao/FriendDao.java new file mode 100644 index 0000000..a20ffcd --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/dao/FriendDao.java @@ -0,0 +1,19 @@ +package com.luoo.user.dao; + +import com.luoo.user.pojo.Friend; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; + +public interface FriendDao extends JpaRepository { + + public Friend findByUseridAndFriendid(String userid,String friendid); + + @Modifying + @Query(value = "update tb_friend set islike = ? where userid = ? and friendid = ?",nativeQuery = true) + public void updateIslike(String islike,String userid,String friendid); + + @Modifying + @Query(value = "delete from tb_friend where userid = ? and friendid = ?",nativeQuery = true) + void deletefriend(String userid, String friendid); +} diff --git a/luoo_user/src/main/java/com/luoo/user/dao/NoFriendDao.java b/luoo_user/src/main/java/com/luoo/user/dao/NoFriendDao.java new file mode 100644 index 0000000..f3097ba --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/dao/NoFriendDao.java @@ -0,0 +1,11 @@ +package com.luoo.user.dao; + +import com.luoo.user.pojo.NoFriend; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface NoFriendDao extends JpaRepository { + + public NoFriend findByUseridAndFriendid(String userid,String friendid); + + +} 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 2f92b11..ccf5117 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 @@ -1,8 +1,12 @@ package com.luoo.user.dto.response; +import com.luoo.user.dto.UserCollectJournalDto; +import com.luoo.user.dto.UserCollectSongDto; import io.swagger.annotations.ApiModelProperty; import lombok.Data; +import java.util.List; + @Data public class UserRespDTO { @ApiModelProperty(value = "ID") @@ -25,8 +29,12 @@ public class UserRespDTO { private int thumbUpCount; @ApiModelProperty(value = "喜欢歌曲数") private int songCount; + + private List songList; @ApiModelProperty(value = "收藏期刊数") private int journalCount; + + private List journalList; @ApiModelProperty(value = "获得评论数") private int commentReplyCount; @ApiModelProperty(value = "生日,格式为: yyyy.MM.dd") diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/Friend.java b/luoo_user/src/main/java/com/luoo/user/pojo/Friend.java new file mode 100644 index 0000000..f4bd0a5 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/pojo/Friend.java @@ -0,0 +1,45 @@ +package com.luoo.user.pojo; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.IdClass; +import javax.persistence.Table; +import java.io.Serializable; + +@Entity +@Table(name = "tb_friend") +@IdClass(Friend.class) +public class Friend implements Serializable { + + @Id + private String userid; + + @Id + private String friendid; + + private String islike; + + public String getUserid() { + return userid; + } + + public void setUserid(String userid) { + this.userid = userid; + } + + public String getFriendid() { + return friendid; + } + + public void setFriendid(String friendid) { + this.friendid = friendid; + } + + public String getIslike() { + return islike; + } + + public void setIslike(String islike) { + this.islike = islike; + } +} diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/NoFriend.java b/luoo_user/src/main/java/com/luoo/user/pojo/NoFriend.java new file mode 100644 index 0000000..75a618c --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/pojo/NoFriend.java @@ -0,0 +1,38 @@ +package com.luoo.user.pojo; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.IdClass; +import javax.persistence.Table; +import java.io.Serializable; + +@Entity +@Table(name = "tb_nofriend") +@IdClass(NoFriend.class) +public class NoFriend implements Serializable { + + @Id + private String userid; + + @Id + private String friendid; + + + public String getUserid() { + return userid; + } + + public void setUserid(String userid) { + this.userid = userid; + } + + public String getFriendid() { + return friendid; + } + + public void setFriendid(String friendid) { + this.friendid = friendid; + } + + +} diff --git a/luoo_user/src/main/java/com/luoo/user/service/FriendService.java b/luoo_user/src/main/java/com/luoo/user/service/FriendService.java new file mode 100644 index 0000000..945cf13 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/service/FriendService.java @@ -0,0 +1,72 @@ +package com.luoo.user.service; + + +import com.luoo.user.dao.FriendDao; +import com.luoo.user.dao.NoFriendDao; +import com.luoo.user.pojo.Friend; +import com.luoo.user.pojo.NoFriend; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional +public class FriendService { + + @Autowired + private FriendDao friendDao; + + @Autowired + private NoFriendDao noFriendDao; + public int addFriend(String userid, String friendid) { + // 先判断 userid到friendid是否有数据,有就是重复添加好友 + + Friend friend = friendDao.findByUseridAndFriendid(userid, friendid); + if (friend!=null) { + return 0; + } + // 直接添加好友,让好友表中userid到friend方向的type为0 + + friend = new Friend(); + friend.setUserid(userid); + friend.setFriendid(friendid); + friend.setIslike("0"); + friendDao.save(friend); + //判断从friendid到userid是否有数据,如果有,把双方的状态都改为1 + + if (friendDao.findByUseridAndFriendid(friendid,userid)!=null) { + // 把双方的islike改成1 + friendDao.updateIslike("1",userid,friendid); + friendDao.updateIslike("1",friendid,userid); + + } + return 1; + } + + public int addNoFriend(String userid, String friendid) { + //先判断是否已经是非好友 + NoFriend nofriend = noFriendDao.findByUseridAndFriendid(userid, friendid); + if (nofriend !=null) { + return 0; + } + nofriend = new NoFriend(); + nofriend.setUserid(userid); + nofriend.setFriendid(friendid); + noFriendDao.save(nofriend); + return 1; + + } + + public void deleteFriend(String userid, String friendid) { + // 删除好友表中userid到friendid这条数据 + + friendDao.deletefriend(userid,friendid); + // 更新friendid到userid的islike为0 + friendDao.updateIslike("0",friendid,userid); + // 非好友表中添加数据 + NoFriend nofriend = new NoFriend(); + nofriend.setFriendid(friendid); + nofriend.setUserid(userid); + noFriendDao.save(nofriend); + } +} 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 16cde92..28e31b8 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 @@ -45,9 +45,9 @@ public class UserCollectInfoService { } - public UserCollectInfo findByUserId(){ + public UserCollectInfo findByUserId(String userId){ - return userCollectInfoDao.findUserCollectInfoByUserId("111222"); + return userCollectInfoDao.findUserCollectInfoByUserId(userId); } public void unCollect(){