Skip to content

Commit

Permalink
Merge pull request #63 from 9oormthon-univ/hyundong
Browse files Browse the repository at this point in the history
fix :: word 부분 401 에러 해결 - 북마크 조회, 카테고리 별 조회
  • Loading branch information
hyundong-L authored Nov 29, 2024
2 parents 379244d + 23be399 commit e313965
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 43 deletions.
30 changes: 16 additions & 14 deletions src/main/java/com/danpoong/onchung/domain/user/domain/UserInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,23 @@ public class UserInfo {
joinColumns = @JoinColumn(name = "user_info_id")
)
@Column(name = "welfare_card")
private List<WelfareCard> welfareCard = new ArrayList<>();
private List<WelfareCard> welfareCard;

@ManyToMany
@JoinTable(
name = "user_favorite_policy",
joinColumns = @JoinColumn(name = "user_info_id"),
inverseJoinColumns = @JoinColumn(name = "policy_id")
)
private List<Policy> favoritePolicies = new ArrayList<>();

public void addFavoriteWord(Word word) {
if (!favoriteWords.contains(word)) {
favoriteWords.add(word);
}
}

public void removeFavoriteWord(Word word) {
favoriteWords.remove(word);
}

private List<Policy> favoritePolicies;

@ManyToMany
@JoinTable(
name = "user_favorite_word",
joinColumns = @JoinColumn(name = "user_info_id"),
inverseJoinColumns = @JoinColumn(name = "word_id")
)
private List<Word> favoriteWords = new ArrayList<>();
private List<Word> favoriteWords;

@Column(name = "refresh_token")
private String refreshToken;
Expand All @@ -78,8 +67,21 @@ public void removeFavoriteWord(Word word) {
public UserInfo(String email, String nickname) {
this.email = email;
this.nickname = nickname;
favoritePolicies = new ArrayList<>();
favoriteWords = new ArrayList<>();
}

public void addFavoriteWord(Word word) {
if (!favoriteWords.contains(word)) {
favoriteWords.add(word);
}
}

public void removeFavoriteWord(Word word) {
favoriteWords.remove(word);
}


public void updateRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
package com.danpoong.onchung.domain.user.repository;

import com.danpoong.onchung.domain.user.domain.UserInfo;
import com.danpoong.onchung.domain.word.domain.Word;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Optional;

public interface UserInfoRepository extends JpaRepository<UserInfo, Long> {
Optional<UserInfo> findByRefreshToken(String refreshToken);
Optional<UserInfo> findByEmail(String email);

@Query(value = "SELECT w FROM UserInfo u JOIN u.favoriteWords w WHERE u.id = :userId",
countQuery = "SELECT COUNT(w) FROM UserInfo u JOIN u.favoriteWords w WHERE u.id = :userId")
Page<Word> findFavoriteWordsByUserId(@Param("userId") Long userId, Pageable pageable);

// @Query(value = "SELECT w FROM UserInfo u JOIN u.favoriteWords w WHERE u.id = :userId",
// countQuery = "SELECT COUNT(w) FROM UserInfo u JOIN u.favoriteWords w WHERE u.id = :userId")
// List<Word> findFavoriteWordsByUserId(@Param("userId") Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class WordController {
private final WordService wordService;

@Operation(summary = "카테고리 별 단어 조회", description = "한 페이지의 데이터 개수 설정 가능")
@Operation(summary = "카테고리 별 단어 조회", description = "한 페이지의 데이터 개수 설정 가능, 페이지 번호는 0번부터 시작")
@GetMapping("/category/{page_num}")
public ResponseTemplate<Page<WordSummaryResponseDto>> getWords(
@AuthenticationPrincipal Long userId,
Expand All @@ -45,7 +45,7 @@ public ResponseTemplate<?> favoriteWord(
return new ResponseTemplate<>(HttpStatus.OK, "북마크 추가 / 삭제 성공");
}

@Operation(summary = "단어 북마크 조회")
@Operation(summary = "단어 북마크 조회", description = "유저의 북마크를 조회, 페이지 번호는 0번부터 시작")
@GetMapping("/book-mark/{page_num}")
public ResponseTemplate<Page<WordSummaryResponseDto>> getBookmarks(
@AuthenticationPrincipal Long userId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Objects;


@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = "word")
public class Word {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ public interface WordRepository extends JpaRepository<Word, Long> {
Optional<Word> findByTerm(String term);

@Query("SELECT w FROM Word w " +
"LEFT JOIN UserInfo u ON :userId MEMBER OF u.favoriteWords " +
"LEFT JOIN UserInfo u ON u.id = :userId " +
"LEFT JOIN u.favoriteWords fw ON fw = w " +
"WHERE w.category = :category " +
"ORDER BY CASE WHEN w MEMBER OF u.favoriteWords THEN 0 ELSE 1 END, w.id")
"ORDER BY CASE WHEN fw IS NOT NULL THEN 0 ELSE 1 END, w.id")
Page<Word> findWordsByCategoryWithBookmarkFirst(@Param("category") WordCategory category,
@Param("userId") Long userId,
Pageable pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import com.danpoong.onchung.domain.word.repository.WordRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -44,12 +44,7 @@ public Page<WordSummaryResponseDto> getWordsByCategory(String category, Long id,
isBookmarked = userInfo.getFavoriteWords().contains(word);
}

return WordSummaryResponseDto.builder()
.wordId(word.getId())
.term(word.getTerm())
.isBookmark(isBookmarked)
.relatedWelfare(word.getRelatedWelfare())
.build();
return createWordSummaryResponse(word, isBookmarked);
});
}

Expand Down Expand Up @@ -79,24 +74,12 @@ public void favoriteWord(Long id, Long wordId) {
}
}

public Page<WordSummaryResponseDto> getBookmarkedWords(Long id, int page) {
UserInfo userInfo = userInfoRepository.findById(id).orElseThrow(() -> new UserNotFoundException("해당 ID의 사용자가 존재하지 않습니다."));
List<Word> wordList = userInfo.getFavoriteWords();
public Page<WordSummaryResponseDto> getBookmarkedWords(Long userId, int page) {
Pageable pageable = PageRequest.of(page, WORD_PAGE_SIZE);

int totalWords = wordList.size();
int start = Math.min(page * WORD_PAGE_SIZE, totalWords);
int end = Math.min((page + 1) * WORD_PAGE_SIZE, totalWords);
Page<Word> wordsPage = userInfoRepository.findFavoriteWordsByUserId(userId, pageable);

List<Word> pagedWords = wordList.subList(start, end);
Page<Word> pageResult = new PageImpl<>(pagedWords, PageRequest.of(page, WORD_PAGE_SIZE), totalWords);

return pageResult.map(word -> WordSummaryResponseDto.builder()
.wordId(word.getId())
.term(word.getTerm())
.isBookmark(true)
.relatedWelfare(word.getRelatedWelfare())
.build()
);
return wordsPage.map(word -> createWordSummaryResponse(word, true));
}

public WordSummaryResponseDto searchWord(String type, String term, Long id) {
Expand Down

0 comments on commit e313965

Please sign in to comment.