diff --git a/src/main/java/com/book/backend/domain/search/service/SearchService.java b/src/main/java/com/book/backend/domain/search/service/SearchService.java index f5b1ae11..d22c732b 100644 --- a/src/main/java/com/book/backend/domain/search/service/SearchService.java +++ b/src/main/java/com/book/backend/domain/search/service/SearchService.java @@ -82,7 +82,7 @@ public void setDibs(LinkedList 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); } } diff --git a/src/main/java/com/book/backend/domain/user/controller/UserController.java b/src/main/java/com/book/backend/domain/user/controller/UserController.java index e0e4d09d..ce38fbbf 100644 --- a/src/main/java/com/book/backend/domain/user/controller/UserController.java +++ b/src/main/java/com/book/backend/domain/user/controller/UserController.java @@ -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); diff --git a/src/main/java/com/book/backend/domain/user/dto/MyPageDto.java b/src/main/java/com/book/backend/domain/user/dto/MyPageDto.java index 3540fa60..3bee1631 100644 --- a/src/main/java/com/book/backend/domain/user/dto/MyPageDto.java +++ b/src/main/java/com/book/backend/domain/user/dto/MyPageDto.java @@ -11,11 +11,12 @@ public class MyPageDto { private UserDto userDto; private List libraries; - private List dibs; // 찜한 책 - // TODO : 읽은 책 list + private List dibsBooks; // 찜한 책 + private List readBooks; // 읽은 책 public static final String description = "userDto : 유저 정보 | " + "libraries : 도서관 목록 | " + - "dibs : 찜한 책 목록"; + "dibsBooks : 찜한 책 목록 |" + + "readBooks : 읽은 책 목록"; } diff --git a/src/main/java/com/book/backend/domain/user/entity/User.java b/src/main/java/com/book/backend/domain/user/entity/User.java index f62c2d6c..3ea332a0 100644 --- a/src/main/java/com/book/backend/domain/user/entity/User.java +++ b/src/main/java/com/book/backend/domain/user/entity/User.java @@ -54,7 +54,12 @@ public class User { private List 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 books; // 책 찜 + private List dibsBooks; // 책 찜 + + @ElementCollection(fetch = FetchType.EAGER) + @CollectionTable(name = "user_read_books", joinColumns = @JoinColumn(name = "user_id")) + @Column(name = "isbn") + private List readBooks; // 읽은 책 } diff --git a/src/main/java/com/book/backend/domain/userBook/controller/UserBookController.java b/src/main/java/com/book/backend/domain/userBook/controller/UserBookController.java index 3d22cf01..d3c1e962 100644 --- a/src/main/java/com/book/backend/domain/userBook/controller/UserBookController.java +++ b/src/main/java/com/book/backend/domain/userBook/controller/UserBookController.java @@ -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; @@ -18,22 +19,45 @@ 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 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 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 response = userBookService.setDibs(isbn, bookname, bookImgUrl); + List response = userBookService.setDibsBooks(isbn, bookname, bookImgUrl); return responseTemplate.success(response, HttpStatus.OK); } @@ -41,10 +65,10 @@ public ResponseEntity setDibs(String isbn, String bookname, String bookImgUrl @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 response = userBookService.removeDibs(isbn); + List response = userBookService.removeDibsBooks(isbn); return responseTemplate.success(response, HttpStatus.OK); } } diff --git a/src/main/java/com/book/backend/domain/userBook/service/UserBookService.java b/src/main/java/com/book/backend/domain/userBook/service/UserBookService.java index 9795b388..000a34a2 100644 --- a/src/main/java/com/book/backend/domain/userBook/service/UserBookService.java +++ b/src/main/java/com/book/backend/domain/userBook/service/UserBookService.java @@ -19,8 +19,48 @@ public class UserBookService { private final UserService userService; private final UserRepository userRepository; + //읽은책 추가 + public List 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 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 getReadBooks() { + log.trace("UserBookService > getReadBooks()"); + User user = userService.loadLoggedinUser(); + if (user == null) { + throw new CustomException(ErrorCode.USER_NOT_FOUND); + } + return user.getReadBooks(); + } + // 찜 설정 - public List setDibs(String isbn, String bookname, String bookImgUrl) { + public List setDibsBooks(String isbn, String bookname, String bookImgUrl) { log.trace("UserBookService > setDibs()"); User user = userService.loadLoggedinUser(); if (user == null) { @@ -31,38 +71,38 @@ public List 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 removeDibs(String isbn) { + public List 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 getDibsList() { + public List 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)); } } diff --git a/src/main/resources/sql/dummy.sql b/src/main/resources/sql/dummy.sql index 25a5a72e..2ef75e6e 100644 --- a/src/main/resources/sql/dummy.sql +++ b/src/main/resources/sql/dummy.sql @@ -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