Skip to content

Commit

Permalink
feat: 인기 있는 책 조회 API 구현 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
fnzl54 committed Oct 19, 2023
1 parent ca2d13f commit 7c9ff3e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.techeer.checkIt.domain.book.controller;

import com.techeer.checkIt.domain.book.dto.Response.BookRes;
import com.techeer.checkIt.domain.book.dto.Response.BookSearchLikeRes;
import com.techeer.checkIt.domain.book.dto.Response.BookSearchRes;
import com.techeer.checkIt.domain.book.service.BookService;
import com.techeer.checkIt.domain.user.entity.User;
Expand Down Expand Up @@ -67,4 +68,10 @@ public ResponseEntity<ResultResponse> updateLikeById(
return ResponseEntity.ok(ResultResponse.of(UPDATE_BOOK_LIKE_SUCCESS ,bookResponse));
}

@ApiOperation(value = "인기 있는 책 조회 API")
@GetMapping("/like")
public ResponseEntity<ResultResponse> getLikeBooksList() {
Page<BookSearchLikeRes> books = bookService.sortedBooksByLike();
return ResponseEntity.ok(ResultResponse.of(GET_NEW_BOOK_SUCCESS, books));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.techeer.checkIt.domain.book.dao.RedisDao;
import com.techeer.checkIt.domain.book.dto.Response.BookRes;
import com.techeer.checkIt.domain.book.dto.Response.BookSearchLikeRes;
import com.techeer.checkIt.domain.book.dto.Response.BookSearchRes;
import com.techeer.checkIt.domain.book.entity.Book;
import com.techeer.checkIt.domain.book.entity.BookDocument;
import com.techeer.checkIt.domain.book.exception.BookNotFoundException;
import com.techeer.checkIt.domain.book.mapper.BookMapper;
import com.techeer.checkIt.domain.book.repository.BookJpaRepository;
import com.techeer.checkIt.domain.book.repository.BookSearchRepository;
import javax.transaction.Transactional;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -39,6 +41,12 @@ public Page<BookSearchRes> sortedBooksByTime() {
return bookMapper.toPageDtoList(newBooks);
}

public Page<BookSearchLikeRes> sortedBooksByLike() {
PageRequest pageRequest = PageRequest.of(0, 10, Sort.by(Sort.Order.desc("likeCount")));
Page<Book> newBooks = bookJpaRepository.findAll(pageRequest);
return bookMapper.toPageDtoList2(newBooks);
}

// id별 조회할 때
public BookRes findBookById(Long userId, Long bookId) {
Book book = bookJpaRepository.findByBookId(bookId).orElseThrow(BookNotFoundException::new);
Expand All @@ -53,22 +61,31 @@ public BookRes findBookById(Long userId, Long bookId) {
public Book findById(Long id) {
return bookJpaRepository.findById(id).orElseThrow(BookNotFoundException::new);
}
@Transactional
public void updateLike(Long userId, Long bookId) {
Book book = bookJpaRepository.findByBookId(bookId).orElseThrow(BookNotFoundException::new);
String redisKey = "B" + bookId.toString(); // 게시글 key
String redisUserKey = "U" + userId.toString(); // 유저 key
// redis에 없는 게시글 id가 들어올 경우 : 새롭게 데이터를 만들어주고 좋아요수를 0으로 초기화, 있는 경우 : 현제 좋아요수 반환
String values = (redisDao.getValues(redisKey) == null) ? redisDao.setValues(redisKey,"0") : redisDao.getValues(redisKey);
int likes = Integer.parseInt(values);
int likes = Integer.parseInt(values), bookLikes = 0;


// 유저를 key로 조회한 게시글 ID List안에 해당 게시글 ID가 포함되어있지 않는다면,
if (!redisDao.getValuesList(redisUserKey).contains(redisKey.substring(1))) {
redisDao.setValuesList(redisUserKey, redisKey.substring(1)); // 유저 key로 해당 글 ID를 List 형태로 저장
likes = Integer.parseInt(values) + 1; // 좋아요 증가
redisDao.setValues(redisKey, String.valueOf(likes)); // 글ID key로 좋아요 저장
bookLikes = book.getLikeCount();
book.updateLike(bookLikes+1);
} else {
redisDao.deleteValueList(redisUserKey, redisKey.substring(1)); // 유저 key로 해당 글 ID를 List 형태에서 제거
likes = Integer.parseInt(values) - 1; // 좋아요 감소
redisDao.setValues(redisKey, String.valueOf(likes)); // 글ID key로 좋아요 저장
bookLikes = book.getLikeCount();
book.updateLike(bookLikes-1);
}

bookJpaRepository.save(book);
}
}

0 comments on commit 7c9ff3e

Please sign in to comment.