Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ [Refactor]: 책 추천 할때 나이를 나이 분류로 변환하게 변경 #54

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading