Skip to content

Commit

Permalink
Merge pull request #91 from Team-KeepGoing/feature/damage
Browse files Browse the repository at this point in the history
Feat :: select recommend book
  • Loading branch information
priverg authored Oct 15, 2024
2 parents a862ee2 + 7a783fe commit 8abcc9d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
import com.keepgoing.keepserver.domain.book.domain.entity.Book;
import com.keepgoing.keepserver.domain.user.domain.entity.user.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Repository
public interface BookRepository extends JpaRepository<Book, Long> {
Book findBookByNfcCode(String NfcCode);

Optional<Book> findBookByNfcCodeContaining(String NfcCode);

List<Book> findByBorrower(User borrower);

List<Book> findByBorrowerAndRentDateBetween(User borrower, LocalDateTime startDate, LocalDateTime endDate);

@Query(value = "SELECT * FROM keep.book order by RAND() limit 3", nativeQuery = true)
List<Book> selectAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,10 @@ public BaseResponse editBookByNfcCode(@PathVariable(value = "nfcCode") String nf
@RequestBody BookRequestDto bookRequest) throws IOException {
return bookService.editBook(nfcCode, bookRequest);
}

@Operation(summary = "추천 도서 불러오기", description = "추천 도서를 불러옵니다")
@GetMapping("/recommend")
public BaseResponse selectRecommendBook(){
return bookService.selectRecommendBook();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@
import com.keepgoing.keepserver.domain.book.payload.request.BookRequestDto;
import com.keepgoing.keepserver.global.common.BaseResponse;
import org.springframework.security.core.Authentication;

import java.io.IOException;

public interface BookService {
BaseResponse bookRegister(BookDto bookDto);

BaseResponse selectAllBook();

BaseResponse deleteBook(String nfcCode, Authentication auth);

BaseResponse selectMyBook(Authentication auth);

BaseResponse alertMyBook(Authentication auth, String date);

String createNfcCode();

BaseResponse editBook(String nfcCode, BookRequestDto bookRequest) throws IOException;

BaseResponse selectRecommendBook();
}


Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public BaseResponse bookRegister(BookDto bookDto) {
@Override
@Transactional(readOnly = true)
public BaseResponse selectAllBook() {
return new BaseResponse(HttpStatus.OK, "책 불러오기 성공", bookRepository.findAll().stream().map(bookMapper::entityToDto).toList());
return new BaseResponse(HttpStatus.OK,
"책 불러오기 성공",
bookRepository.findAll().stream().map(bookMapper::entityToDto).toList());
}

@Override
Expand Down Expand Up @@ -89,13 +91,23 @@ public String createNfcCode() {
public BaseResponse editBook(String nfcCode, BookRequestDto bookRequest) {
Book book = bookRepository.findBookByNfcCode(nfcCode);

if (bookRequest.state() != null) book.setState(bookRequest.state());
if (bookRequest.imageUrl() != null) book.setImageUrl(bookRequest.imageUrl());
if (bookRequest.name() != null) book.setBookName(bookRequest.name());
if (bookRequest.state() != null)
book.setState(bookRequest.state());
if (bookRequest.imageUrl() != null)
book.setImageUrl(bookRequest.imageUrl());
if (bookRequest.name() != null)
book.setBookName(bookRequest.name());
bookRepository.save(book);
return new BaseResponse(HttpStatus.OK, "책 정보 수정 성공");
}

@Override
@Transactional(readOnly = true)
public BaseResponse selectRecommendBook() {
List<Book> bookLst = bookRepository.selectAll();
return new BaseResponse(HttpStatus.OK, "책 불러오기 성공", bookLst.stream().map(bookMapper::entityToDto).toList());
}

private User getUserByAuthentication(Authentication auth) {
if (auth == null) {
throw BookException.userNotFound();
Expand All @@ -104,4 +116,5 @@ private User getUserByAuthentication(Authentication auth) {
return userRepository.findByEmail(auth.getName())
.orElseThrow(BookException::userNotFound);
}

}

0 comments on commit 8abcc9d

Please sign in to comment.