From 7282884f6d21e7427ce48e0c3772fc66c99a79a1 Mon Sep 17 00:00:00 2001 From: Johyunik Date: Fri, 1 Dec 2023 10:00:42 +0900 Subject: [PATCH] mylikeAdd --- .../board/controller/LikeController.java | 10 +++++++ .../com/weatherfit/board/dto/MyLikeDTO.java | 19 +++++++++++++ .../board/repository/BoardRepository.java | 3 --- .../board/repository/LikeRepository.java | 2 ++ .../weatherfit/board/service/LikeService.java | 27 +++++++++++++++++++ 5 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/weatherfit/board/dto/MyLikeDTO.java diff --git a/src/main/java/com/weatherfit/board/controller/LikeController.java b/src/main/java/com/weatherfit/board/controller/LikeController.java index 3f3e552..e12ee60 100644 --- a/src/main/java/com/weatherfit/board/controller/LikeController.java +++ b/src/main/java/com/weatherfit/board/controller/LikeController.java @@ -1,9 +1,11 @@ package com.weatherfit.board.controller; +import com.weatherfit.board.dto.MyLikeDTO; import com.weatherfit.board.service.LikeService; import org.springframework.web.bind.annotation.*; import java.io.UnsupportedEncodingException; import java.util.Base64; +import java.util.List; @RestController @@ -21,4 +23,12 @@ public boolean like (@RequestHeader("decodedToken") String nickName, @PathVariab likeService.like(boardId, decodedNickname); return true; } + + @GetMapping("/like/mylike") + public List myLike(@RequestHeader("decodedToken") String nickName) throws UnsupportedEncodingException { + String decodedNickname = new String(Base64.getDecoder().decode(nickName), "UTF-8"); + List myLikeList = likeService.myLike(decodedNickname); + + return myLikeList; + } } \ No newline at end of file diff --git a/src/main/java/com/weatherfit/board/dto/MyLikeDTO.java b/src/main/java/com/weatherfit/board/dto/MyLikeDTO.java new file mode 100644 index 0000000..6c6c573 --- /dev/null +++ b/src/main/java/com/weatherfit/board/dto/MyLikeDTO.java @@ -0,0 +1,19 @@ +package com.weatherfit.board.dto; + +import com.weatherfit.board.domain.BoardEntity; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class MyLikeDTO { + private int boardId; + private String images; + + public MyLikeDTO(BoardEntity boardEntity) { + this.boardId = boardEntity.getBoardId(); + if(!boardEntity.getImages().isEmpty()) { + this.images = String.valueOf(boardEntity.getImages().get(0).getImage_url()); + } + } +} diff --git a/src/main/java/com/weatherfit/board/repository/BoardRepository.java b/src/main/java/com/weatherfit/board/repository/BoardRepository.java index 1b48a6e..955f6fb 100644 --- a/src/main/java/com/weatherfit/board/repository/BoardRepository.java +++ b/src/main/java/com/weatherfit/board/repository/BoardRepository.java @@ -20,7 +20,4 @@ public interface BoardRepository extends JpaRepository, Bo List findByNickName(String nickName); - List findByCategoryInAndHashTagIn(List categories, List hashTags); - List findByHashTagIn(List hashTags); - List findByCategoryIn(List categories); } diff --git a/src/main/java/com/weatherfit/board/repository/LikeRepository.java b/src/main/java/com/weatherfit/board/repository/LikeRepository.java index 3a87855..34a85e8 100644 --- a/src/main/java/com/weatherfit/board/repository/LikeRepository.java +++ b/src/main/java/com/weatherfit/board/repository/LikeRepository.java @@ -4,9 +4,11 @@ import com.weatherfit.board.domain.LikeEntity; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; import java.util.Optional; public interface LikeRepository extends JpaRepository { int countByBoardId_BoardId(int boardId); Optional findByBoardIdAndNickName(BoardEntity boardId, String nickName); + List findByNickName(String nickName); } diff --git a/src/main/java/com/weatherfit/board/service/LikeService.java b/src/main/java/com/weatherfit/board/service/LikeService.java index d622df5..f81dae2 100644 --- a/src/main/java/com/weatherfit/board/service/LikeService.java +++ b/src/main/java/com/weatherfit/board/service/LikeService.java @@ -2,12 +2,15 @@ import com.weatherfit.board.domain.BoardEntity; import com.weatherfit.board.domain.LikeEntity; +import com.weatherfit.board.dto.MyLikeDTO; import com.weatherfit.board.repository.BoardRepository; import com.weatherfit.board.repository.LikeRepository; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.List; import java.util.Optional; @Service @@ -31,8 +34,32 @@ public void like(int boardId, String decodedNickname) { likeRepository.save(likeEntity); } } + public int countLikes(int boardId) { return likeRepository.countByBoardId_BoardId(boardId); } + + public List myLike(String nickName) { + List likeEntities = likeRepository.findByNickName(nickName); + + List boardIds = new ArrayList<>(); + for (LikeEntity likeEntity : likeEntities) { + boardIds.add(likeEntity.getBoardId().getBoardId()); + } + + List likedBoards = new ArrayList<>(); + for (Integer id : boardIds) { + likedBoards.add(boardRepository.findById(id).orElse(null)); + } + + List likeDTOS = new ArrayList<>(); + for (BoardEntity boardEntity : likedBoards) { + if (boardEntity != null) { + likeDTOS.add(new MyLikeDTO(boardEntity)); + } + } + + return likeDTOS; + } }