Skip to content

Commit

Permalink
mylikeAdd
Browse files Browse the repository at this point in the history
  • Loading branch information
Johyunik committed Dec 1, 2023
1 parent 68da92d commit 7282884
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/weatherfit/board/controller/LikeController.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,4 +23,12 @@ public boolean like (@RequestHeader("decodedToken") String nickName, @PathVariab
likeService.like(boardId, decodedNickname);
return true;
}

@GetMapping("/like/mylike")
public List<MyLikeDTO> myLike(@RequestHeader("decodedToken") String nickName) throws UnsupportedEncodingException {
String decodedNickname = new String(Base64.getDecoder().decode(nickName), "UTF-8");
List<MyLikeDTO> myLikeList = likeService.myLike(decodedNickname);

return myLikeList;
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/weatherfit/board/dto/MyLikeDTO.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,4 @@ public interface BoardRepository extends JpaRepository<BoardEntity, Integer>, Bo

List<BoardEntity> findByNickName(String nickName);

List<BoardEntity> findByCategoryInAndHashTagIn(List<String> categories, List<String> hashTags);
List<BoardEntity> findByHashTagIn(List<String> hashTags);
List<BoardEntity> findByCategoryIn(List<String> categories);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<LikeEntity, Integer> {
int countByBoardId_BoardId(int boardId);
Optional<LikeEntity> findByBoardIdAndNickName(BoardEntity boardId, String nickName);
List<LikeEntity> findByNickName(String nickName);
}
27 changes: 27 additions & 0 deletions src/main/java/com/weatherfit/board/service/LikeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<MyLikeDTO> myLike(String nickName) {
List<LikeEntity> likeEntities = likeRepository.findByNickName(nickName);

List<Integer> boardIds = new ArrayList<>();
for (LikeEntity likeEntity : likeEntities) {
boardIds.add(likeEntity.getBoardId().getBoardId());
}

List<BoardEntity> likedBoards = new ArrayList<>();
for (Integer id : boardIds) {
likedBoards.add(boardRepository.findById(id).orElse(null));
}

List<MyLikeDTO> likeDTOS = new ArrayList<>();
for (BoardEntity boardEntity : likedBoards) {
if (boardEntity != null) {
likeDTOS.add(new MyLikeDTO(boardEntity));
}
}

return likeDTOS;
}
}

0 comments on commit 7282884

Please sign in to comment.