Skip to content

Commit

Permalink
refactor: PR 피드백 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
fnzl54 committed Sep 20, 2023
1 parent d7896fb commit abefc4e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.List;

import static com.techeer.checkIt.global.result.ResultCode.GET_NEW_BOOK_SUCCESS;
import static com.techeer.checkIt.global.result.ResultCode.UPDATE_BOOK_LIKE_SUCCESS;
import static com.techeer.checkIt.global.result.ResultCode.GET_ONE_BOOK_SUCCESS;

@Api(tags = "책 API")
@RequestMapping("/api/v1/books")
Expand All @@ -37,10 +39,13 @@ public ResponseEntity<List<BookSearchRes>> searchTitle(@RequestParam(defaultValu

@ApiOperation(value = "책 한 권 조회 API")
@GetMapping("{bookId}")
public ResponseEntity<BookRes> getBookById(@AuthenticationPrincipal UserDetail userDetail, @PathVariable Long bookId){
public ResponseEntity<ResultResponse> getBookById(
@AuthenticationPrincipal UserDetail userDetail,
@PathVariable Long bookId
){
User user = userService.findUserByUsername(userDetail.getUsername());
BookRes bookResponse = bookService.findBookById(user.getId(), bookId);
return ResponseEntity.ok(bookResponse);
return ResponseEntity.ok(ResultResponse.of(GET_ONE_BOOK_SUCCESS, bookResponse));
}

@ApiOperation(value = "신규 도서 조회 API")
Expand All @@ -52,11 +57,14 @@ public ResponseEntity<ResultResponse> getNewBooksList() {

@ApiOperation(value = "책 좋아요 API")
@GetMapping("/like/{bookId}")
public ResponseEntity<BookRes> updateLikeById(@AuthenticationPrincipal UserDetail userDetail, @PathVariable Long bookId){
public ResponseEntity<ResultResponse> updateLikeById(
@AuthenticationPrincipal UserDetail userDetail,
@PathVariable Long bookId
){
User user = userService.findUserByUsername(userDetail.getUsername());
bookService.updateLike(user.getId(), bookId);
BookRes bookResponse = bookService.findBookById(user.getId(), bookId);
return ResponseEntity.ok(bookResponse);
return ResponseEntity.ok(ResultResponse.of(UPDATE_BOOK_LIKE_SUCCESS ,bookResponse));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ public class BookRes {
private int width;
private int pages;
private int likes;
private boolean like_status;
private boolean likeStatus;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@Component
public class BookMapper {
public BookRes toDto(Book book, int likes, boolean like_status) {
public BookRes toDto(Book book, int likes, boolean likeStatus) {
return BookRes.builder()
.id(book.getId())
.title(book.getTitle())
Expand All @@ -24,7 +24,7 @@ public BookRes toDto(Book book, int likes, boolean like_status) {
.width(book.getWidth())
.pages(book.getPages())
.likes(likes)
.like_status(like_status)
.likeStatus(likeStatus)
.build();
}
public BookSearchRes toBookSearchDto(BookDocument book) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ public Page<BookSearchRes> sortedBooksByTime() {
}

// id별 조회할 때
public BookRes findBookById(Long userId, Long id) {
Book book = bookJpaRepository.findByBookId(id).orElseThrow(BookNotFoundException::new);
String redisKey = "B" + id.toString();
public BookRes findBookById(Long userId, Long bookId) {
Book book = bookJpaRepository.findByBookId(bookId).orElseThrow(BookNotFoundException::new);
String redisKey = "B" + bookId.toString();
String redisUserKey = "U" + userId.toString();
String values = (redisDao.getValues(redisKey) == null) ? redisDao.setValues(redisKey,"0") : redisDao.getValues(redisKey);
int views = Integer.parseInt(values);
boolean like_status = !redisDao.getValuesList(redisUserKey).contains(redisKey.substring(1)) ? false : true;
return bookMapper.toDto(book, views, like_status);
int likes = Integer.parseInt(values);
boolean likeStatus = !redisDao.getValuesList(redisUserKey).contains(redisKey.substring(1)) ? false : true;
return bookMapper.toDto(book, likes, likeStatus);
}
// 책 판별용
public Book findById(Long id) {
Expand All @@ -58,17 +58,17 @@ public void updateLike(Long userId, Long bookId) {
String redisUserKey = "U" + userId.toString(); // 유저 key
// redis에 없는 게시글 id가 들어올 경우 : 새롭게 데이터를 만들어주고 좋아요수를 0으로 초기화, 있는 경우 : 현제 좋아요수 반환
String values = (redisDao.getValues(redisKey) == null) ? redisDao.setValues(redisKey,"0") : redisDao.getValues(redisKey);
int views = Integer.parseInt(values);
int likes = Integer.parseInt(values);

// 유저를 key로 조회한 게시글 ID List안에 해당 게시글 ID가 포함되어있지 않는다면,
if (!redisDao.getValuesList(redisUserKey).contains(redisKey.substring(1))) {
redisDao.setValuesList(redisUserKey, redisKey.substring(1)); // 유저 key로 해당 글 ID를 List 형태로 저장
views = Integer.parseInt(values) + 1; // 좋아요 증가
redisDao.setValues(redisKey, String.valueOf(views)); // 글ID key로 좋아요 저장
likes = Integer.parseInt(values) + 1; // 좋아요 증가
redisDao.setValues(redisKey, String.valueOf(likes)); // 글ID key로 좋아요 저장
} else {
redisDao.deleteValueList(redisUserKey, redisKey.substring(1)); // 유저 key로 해당 글 ID를 List 형태에서 제거
views = Integer.parseInt(values) - 1; // 좋아요 감소
redisDao.setValues(redisKey, String.valueOf(views)); // 글ID key로 좋아요 저장
likes = Integer.parseInt(values) - 1; // 좋아요 감소
redisDao.setValues(redisKey, String.valueOf(likes)); // 글ID key로 좋아요 저장
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public RedisTemplate<String, Object> redisTemplate() {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setEnableTransactionSupport(true);
return redisTemplate;
}

Expand All @@ -41,6 +42,7 @@ public RedisTemplate<String, String> redisLikeTemplate() {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setEnableTransactionSupport(true);
return redisTemplate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public enum ResultCode {

// Book
GET_NEW_BOOK_SUCCESS("B001","신규 도서 조회 성공"),
UPDATE_BOOK_LIKE_SUCCESS("B002","좋아요 갱신 성공"),
GET_ONE_BOOK_SUCCESS("B003","책 조회 성공"),


// Reading
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/techeer/checkIt/fixture/BookFixtures.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class BookFixtures {
.pages(400)
.publisher("데이원")
.likes(10)
.like_status(true)
.likeStatus(true)
.build();
public static final BookRes TEST_BOOK2 =
BookRes.builder()
Expand All @@ -28,7 +28,7 @@ public class BookFixtures {
.pages(170)
.publisher("어웨이크북스")
.likes(10)
.like_status(true)
.likeStatus(true)
.build();
public static final BookSearchRes TEST_BOOK_SEARCH =
BookSearchRes.builder()
Expand Down

0 comments on commit abefc4e

Please sign in to comment.