Skip to content

Commit

Permalink
Merge pull request #54 from Beyond-B/refactor/53-book-recommend
Browse files Browse the repository at this point in the history
♻️ [Refactor]: 책 추천 할때 나이를 나이 분류로 변환하게 변경
  • Loading branch information
jinuklee777 authored Feb 15, 2024
2 parents 0b91fbf + fcfd655 commit 3c6e042
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public BaseResponse<BookResponseDTO.DetailBookDTO> getDetailBook(@AuthUser User
@GetMapping("/recommend")
@Parameter(name = "user", hidden = true)
public BaseResponse<BookResponseDTO.BookContentDTO> recommendBook(
@RequestParam Emotion emotion, @RequestParam Age age, @AuthUser User user) {
Book book = bookService.recommendBook(emotion, age, user);
@RequestParam Emotion emotion, @AuthUser User user) {
Book book = bookService.recommendBook(emotion, user);

return BaseResponse.onSuccess(BookConverter.toCreateBookDTO(book));
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/beyondB/beyondB/service/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface BookService {

BookResponseDTO.DetailBookDTO getDetailBook(User user, Long bookId);

Book recommendBook(Emotion emotion, Age age, User user);
Book recommendBook(Emotion emotion, User user);

Long recentQuiz(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ public BookResponseDTO.DetailBookDTO getDetailBook(User user, Long bookId) {
}

@Override
public Book recommendBook(Emotion emotion, Age age, User user) {
public Book recommendBook(Emotion emotion, User user) {
Feeling feeling = feelingRepository.findByEmotion(emotion);
List<Book> books = bookRepository.findAllByFeeling(feeling);
if (books.isEmpty()) {
throw new BookException(ErrorStatus.BOOK_NOT_FOUND);
}

Age age = stringToAge(user.getAge());
List<Long> readBookIds = user.getUserBookList().stream()
.map(userBook -> userBook.getBook().getId())
.collect(Collectors.toList());
Expand Down Expand Up @@ -191,4 +191,27 @@ private boolean isImageUrlValid(String imageUrl) {
return false;
}
}

private Age stringToAge(String ageString) {
int age;
try {
age = Integer.parseInt(ageString);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid age format");
}

if (age < 7) {
return Age.UNDER_7;
} else if (age <= 10) {
return Age.AGE_7_10;
} else if (age <= 13) {
return Age.AGE_11_13;
} else if (age <= 16) {
return Age.AGE_14_16;
} else if (age <= 19) {
return Age.AGE_17_19;
} else {
return Age.OVER_18;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ public void signUp(UserSignupDTO userSignUpDto) throws Exception {
throw new Exception("이미 존재하는 이메일입니다.");
}

if (userRepository.findByUsername(userSignUpDto.getUsername()).isPresent()) {
throw new Exception("이미 존재하는 닉네임입니다.");
}

User user = User.builder()
.email(userSignUpDto.getEmail())
.password(userSignUpDto.getPassword())
Expand Down

0 comments on commit 3c6e042

Please sign in to comment.