Skip to content

Commit

Permalink
Merge pull request #105 from BOOK-TALK/#104-read-book
Browse files Browse the repository at this point in the history
#104 read book
  • Loading branch information
chanwoo7 authored Aug 24, 2024
2 parents 753e925 + 3a41074 commit f13dac8
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void setDibs(LinkedList<SearchResponseDto> bookList) {
throw new CustomException(ErrorCode.USER_NOT_FOUND);
}
for(SearchResponseDto dto : bookList){
if(user.getBooks().stream().anyMatch(userBookDto -> userBookDto.getIsbn().equals(dto.getIsbn13()))){
if(user.getDibsBooks().stream().anyMatch(userBookDto -> userBookDto.getIsbn().equals(dto.getIsbn13()))){
dto.setDibs(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public ResponseEntity<?> getUserInfo() {
MyPageDto responseDto = MyPageDto.builder()
.userDto(userDto)
.libraries(userService.getLibraries(user))
.dibs(userBookService.getDibsList())
.dibsBooks(userBookService.getDibsBooks())
.readBooks(userBookService.getReadBooks())
.build();

return responseTemplate.success(responseDto, HttpStatus.OK);
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/book/backend/domain/user/dto/MyPageDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
public class MyPageDto {
private UserDto userDto;
private List<LibraryDto> libraries;
private List<UserBookDto> dibs; // 찜한 책
// TODO : 읽은 책 list
private List<UserBookDto> dibsBooks; // 찜한 책
private List<UserBookDto> readBooks; // 읽은 책

public static final String description =
"userDto : 유저 정보 | " +
"libraries : 도서관 목록 | " +
"dibs : 찜한 책 목록";
"dibsBooks : 찜한 책 목록 |" +
"readBooks : 읽은 책 목록";
}
9 changes: 7 additions & 2 deletions src/main/java/com/book/backend/domain/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ public class User {
private List<LibraryDto> libraries;

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "user_books", joinColumns = @JoinColumn(name = "user_id"))
@CollectionTable(name = "user dibs_books", joinColumns = @JoinColumn(name = "user_id"))
@Column(name = "isbn")
private List<UserBookDto> books; // 책 찜
private List<UserBookDto> dibsBooks; // 책 찜

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "user_read_books", joinColumns = @JoinColumn(name = "user_id"))
@Column(name = "isbn")
private List<UserBookDto> readBooks; // 읽은 책
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.book.backend.domain.openapi.service.RequestValidate;
import com.book.backend.domain.userBook.dto.UserBookDto;
import com.book.backend.domain.userBook.service.UserBookService;
import com.book.backend.global.log.RequestLogger;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
Expand All @@ -18,33 +19,56 @@
import com.book.backend.global.ResponseTemplate;

@RestController
@RequestMapping("/api/book/dibs")
@RequestMapping("/api/book")
@RequiredArgsConstructor
@Slf4j
@Tag(name="찜", description = "찜 해제 / 찜 추가")
@Tag(name="읽은 책 & 책 찜", description = "읽은책 삭제 / 찜 해제 / 읽은 책 추가 / 찜 추가")
public class UserBookController {
private final RequestValidate requestValidate;
private final UserBookService userBookService;
private final ResponseTemplate responseTemplate;

@Operation(summary="읽은 책 추가", description="읽은 책 추가하고 user의 최종 읽은책 리스트를 반환합니다.",
responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = UserBookDto.class)),
description = UserBookDto.description)})
@PostMapping("/read")
public ResponseEntity<?> setReadBooks(String isbn, String bookname, String bookImgUrl) {
RequestLogger.param(new String[]{"isbn, bookname, bookImgUrl"}, isbn, bookname, bookImgUrl);
requestValidate.isValidIsbn(isbn);
List<UserBookDto> response = userBookService.setReadBooks(isbn, bookname, bookImgUrl);
return responseTemplate.success(response, HttpStatus.OK);
}

// 해제 API
@Operation(summary="읽은 책 삭제", description="읽은 책 삭제하고 user의 최종 읽은책 리스트를 반환합니다.",
responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = UserBookDto.class)),
description = UserBookDto.description)})
@DeleteMapping("/read")
public ResponseEntity<?> removeReadBooks(String isbn) {
RequestLogger.param(new String[]{"isbn"}, isbn);
requestValidate.isValidIsbn(isbn);
List<UserBookDto> response = userBookService.removeReadBooks(isbn);
return responseTemplate.success(response, HttpStatus.OK);
}

@Operation(summary="책 찜 추가", description="새로운 책을 찜하고 user의 최종 찜 리스트를 반환합니다.",
responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = UserBookDto.class)),
description = UserBookDto.description)})
@PostMapping("/")
public ResponseEntity<?> setDibs(String isbn, String bookname, String bookImgUrl) throws Exception {
@PostMapping("/dibs")
public ResponseEntity<?> setDibsBooks(String isbn, String bookname, String bookImgUrl) throws Exception {
requestValidate.isValidIsbn(isbn);
List<UserBookDto> response = userBookService.setDibs(isbn, bookname, bookImgUrl);
List<UserBookDto> response = userBookService.setDibsBooks(isbn, bookname, bookImgUrl);
return responseTemplate.success(response, HttpStatus.OK);
}

// 해제 API
@Operation(summary="책 찜 해제", description="찜 해제하고 user의 최종 찜 리스트를 반환합니다.",
responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = UserBookDto.class)),
description = UserBookDto.description)})
@DeleteMapping("/")
public ResponseEntity<?> removeDibs(String isbn) {
@DeleteMapping("/dibs")
public ResponseEntity<?> removeDibsBooks(String isbn) {
requestValidate.isValidIsbn(isbn);
List<UserBookDto> response = userBookService.removeDibs(isbn);
List<UserBookDto> response = userBookService.removeDibsBooks(isbn);
return responseTemplate.success(response, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,48 @@ public class UserBookService {
private final UserService userService;
private final UserRepository userRepository;

//읽은책 추가
public List<UserBookDto> setReadBooks(String isbn, String bookname, String bookImgUrl) {
log.trace("UserBookService > setReadBooks()");
User user = userService.loadLoggedinUser();
if (user == null) {
throw new CustomException(ErrorCode.USER_NOT_FOUND);
}
UserBookDto userBookDto = UserBookDto.builder()
.isbn(isbn)
.bookname(bookname)
.bookImageURL(bookImgUrl)
.build();
user.getReadBooks().add(userBookDto);
userRepository.save(user);

return user.getReadBooks();
}

//읽은책 삭제
public List<UserBookDto> removeReadBooks(String isbn) {
log.trace("UserBookService > removeReadBooks()");
User user = userService.loadLoggedinUser();
if (user == null) {
throw new CustomException(ErrorCode.USER_NOT_FOUND);
}
user.getReadBooks().removeIf(userBookDto -> userBookDto.getIsbn().equals(isbn));
userRepository.save(user);
return user.getReadBooks();
}

//읽은책 조회
public List<UserBookDto> getReadBooks() {
log.trace("UserBookService > getReadBooks()");
User user = userService.loadLoggedinUser();
if (user == null) {
throw new CustomException(ErrorCode.USER_NOT_FOUND);
}
return user.getReadBooks();
}

// 찜 설정
public List<UserBookDto> setDibs(String isbn, String bookname, String bookImgUrl) {
public List<UserBookDto> setDibsBooks(String isbn, String bookname, String bookImgUrl) {
log.trace("UserBookService > setDibs()");
User user = userService.loadLoggedinUser();
if (user == null) {
Expand All @@ -31,38 +71,38 @@ public List<UserBookDto> setDibs(String isbn, String bookname, String bookImgUrl
.bookname(bookname)
.bookImageURL(bookImgUrl)
.build();
user.getBooks().add(userBookDto);
user.getDibsBooks().add(userBookDto);
userRepository.save(user);

return user.getBooks();
return user.getDibsBooks();
}

// 찜 해제
public List<UserBookDto> removeDibs(String isbn) {
public List<UserBookDto> removeDibsBooks(String isbn) {
log.trace("UserBookService > removeDibs()");
User user = userService.loadLoggedinUser();
if (user == null) {
throw new CustomException(ErrorCode.USER_NOT_FOUND);
}
user.getBooks().removeIf(userBookDto -> userBookDto.getIsbn().equals(isbn));
user.getDibsBooks().removeIf(userBookDto -> userBookDto.getIsbn().equals(isbn));
userRepository.save(user);
return user.getBooks();
return user.getDibsBooks();
}

// 찜 리스트 조회
public List<UserBookDto> getDibsList() {
public List<UserBookDto> getDibsBooks() {
log.trace("UserBookService > getDibsList()");
User user = userService.loadLoggedinUser();
if (user == null) {
throw new CustomException(ErrorCode.USER_NOT_FOUND);
}
return user.getBooks();
return user.getDibsBooks();
}

// 찜 여부 확인
public boolean isDibs(String isbn) {
log.trace("UserBookService > isDibs()");
User user = userService.loadLoggedinUser();
return user.getBooks().stream().anyMatch(userBookDto -> userBookDto.getIsbn().equals(isbn));
return user.getDibsBooks().stream().anyMatch(userBookDto -> userBookDto.getIsbn().equals(isbn));
}
}
7 changes: 6 additions & 1 deletion src/main/resources/sql/dummy.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ CREATE TABLE IF NOT EXISTS user
nickname varchar(255) null,
password varchar(255) null,
reg_date datetime(6) null,
email varchar(255) null,
phone varchar(255) null,
apple_id varchar(255) null,
constraint UK792t1v1e9f43yusiryq37re13
unique (kakao_id),
constraint UKmnjg735lsf6wola6yyxxfj08l
unique (apple_id),
constraint UKns0jdvuknugj5tmxq82un8q1x
unique (login_id),
check (`gender` between 0 and 2)
)engine=InnoDB DEFAULT CHARSET=utf8;
);
CREATE TABLE IF NOT EXISTS user_opentalk
(
user_opentalk_id bigint auto_increment
Expand Down

0 comments on commit f13dac8

Please sign in to comment.