forked from kookmin-sw/cap-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Be feature/#39 book
- Loading branch information
Showing
7 changed files
with
94 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
backend/src/main/java/com/project/capstone/book/service/BookService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,74 @@ | ||
package com.project.capstone.book.service; | ||
|
||
import com.project.capstone.book.controller.dto.AddBookRequest; | ||
import com.project.capstone.book.controller.dto.BookResponse; | ||
import com.project.capstone.book.domain.Book; | ||
import com.project.capstone.book.domain.BookRepository; | ||
import com.project.capstone.book.exception.BookException; | ||
import com.project.capstone.content.controller.dto.ContentResponse; | ||
import com.project.capstone.content.domain.Content; | ||
import com.project.capstone.content.domain.ContentRepository; | ||
import com.project.capstone.content.domain.ContentType; | ||
import com.project.capstone.content.exception.ContentException; | ||
import com.project.capstone.quiz.controller.dto.QuizResponse; | ||
import com.project.capstone.quiz.domain.Quiz; | ||
import com.project.capstone.quiz.domain.QuizRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static com.project.capstone.book.exception.BookExceptionType.ALREADY_EXIST_BOOK; | ||
import static com.project.capstone.book.exception.BookExceptionType.BOOK_NOT_FOUND; | ||
import static com.project.capstone.content.exception.ContentExceptionType.TYPE_NOT_FOUND; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class BookService { | ||
private final BookRepository bookRepository; | ||
private final QuizRepository quizRepository; | ||
private final ContentRepository contentRepository; | ||
public void addBook(AddBookRequest request) { | ||
if (bookRepository.findBookByIsbn(request.isbn()).isPresent()) { | ||
throw new BookException(ALREADY_EXIST_BOOK); | ||
} | ||
bookRepository.save(new Book(request)); | ||
} | ||
|
||
public BookResponse getBookByIsbn(String isbn) { | ||
Book book = getBook(isbn); | ||
return new BookResponse(book); | ||
} | ||
|
||
public List<QuizResponse> getQuizByBook(String isbn) { | ||
Book book = getBook(isbn); | ||
List<Quiz> quizList= quizRepository.findQuizzesByBook(book); | ||
List<QuizResponse> quizResponseList = new ArrayList<>(); | ||
for (Quiz quiz : quizList) { | ||
quizResponseList.add(new QuizResponse(quiz)); | ||
} | ||
return quizResponseList; | ||
} | ||
|
||
private Book getBook(String isbn) { | ||
return bookRepository.findBookByIsbn(isbn).orElseThrow( | ||
() -> new BookException(BOOK_NOT_FOUND) | ||
); | ||
} | ||
|
||
public List<ContentResponse> getContentsByBook(String isbn, String type) { | ||
Book book = getBook(isbn); | ||
for (ContentType contentType : ContentType.values()) { | ||
if (contentType.equals(ContentType.valueOf(type))) { | ||
List<Content> contentsByTypeAndBook = contentRepository.findContentsByTypeAndBook(ContentType.valueOf(type), book); | ||
return contentsByTypeAndBook.stream() | ||
.map(ContentResponse::new) | ||
.toList(); | ||
} | ||
} | ||
throw new ContentException(TYPE_NOT_FOUND); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
backend/src/main/java/com/project/capstone/quiz/domain/QuizRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
package com.project.capstone.quiz.domain; | ||
|
||
import com.project.capstone.book.domain.Book; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface QuizRepository extends JpaRepository<Quiz, Long> { | ||
Optional<Quiz> findQuizById(Long id); | ||
|
||
List<Quiz> findQuizzesByBook(Book book); | ||
} |