Skip to content

Commit

Permalink
Merge pull request #88 from jisung-in/docs/86-userlibrary-rest-docs
Browse files Browse the repository at this point in the history
[Docs] UserLibrary REST Docs 작성
  • Loading branch information
jwooo authored Apr 30, 2024
2 parents 1ec5d52 + 8c6f81a commit 12356fb
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 64 deletions.
62 changes: 62 additions & 0 deletions src/docs/asciidoc/api/userlibrary/userlibrary.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
=== 서재 단건 조회

image::https://jisungin-bucket.s3.ap-northeast-2.amazonaws.com/docs/get-userlibrary.png[]

TIP: 상세 조회된 도서에 대한 사용자의 도서 상태를 조회

==== HTTP Request
include::{snippets}/user-library/get/http-request.adoc[]
include::{snippets}/user-library/get/query-parameters.adoc[]

==== HTTP Response
include::{snippets}/user-library/get/http-response.adoc[]
===== Response Body
include::{snippets}/user-library/get/response-body.adoc[]
include::{snippets}/user-library/get/response-fields.adoc[]

=== 서재 등록

TIP: 상세 조회된 도서에 대한 도서 상태를 생성

==== HTTP Request
include::{snippets}/user-library/create/http-request.adoc[]
===== Request Body
include::{snippets}/user-library/create/request-body.adoc[]
include::{snippets}/user-library/create/request-fields.adoc[]

==== HTTP Response
include::{snippets}/user-library/create/http-response.adoc[]
===== Response Body
include::{snippets}/user-library/create/response-body.adoc[]
include::{snippets}/user-library/create/response-fields.adoc[]

=== 서재 수정

TIP: 상세 조회된 도서에 대한 도서 상태를 수정

==== HTTP Request
include::{snippets}/user-library/edit/http-request.adoc[]
include::{snippets}/user-library/edit/path-parameters.adoc[]
===== Request Body
include::{snippets}/user-library/edit/request-body.adoc[]
include::{snippets}/user-library/edit/request-fields.adoc[]

==== HTTP Response
include::{snippets}/user-library/edit/http-response.adoc[]
===== Response Body
include::{snippets}/user-library/edit/response-body.adoc[]
include::{snippets}/user-library/edit/response-fields.adoc[]

=== 서재 삭제

TIP: 상세 조회된 도서에 대한 도서 상태를 삭제

==== HTTP Request
include::{snippets}/user-library/delete/http-request.adoc[]

==== HTTP Response
include::{snippets}/user-library/delete/http-response.adoc[]
include::{snippets}/user-library/delete/path-parameters.adoc[]
===== Response Body
include::{snippets}/user-library/delete/response-body.adoc[]
include::{snippets}/user-library/delete/response-fields.adoc[]
6 changes: 5 additions & 1 deletion src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ include::api/image/image.adoc[]
[[Book-API]]
== Book API

include::api/book/book.adoc[]
include::api/book/book.adoc[]

[[UserLibrary-API]]
== UserLibrary API
include::api/userlibrary/userlibrary.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ public ApiResponse<Void> editUserLibrary(@PathVariable("userLibraryId") Long use

@DeleteMapping("/user-libraries/{userLibraryId}")
public ApiResponse<Void> deleteUserLibrary(@PathVariable("userLibraryId") Long userLibraryId,
@RequestParam String isbn,
@Auth Long userId
) {
userLibraryService.deleteUserLibrary(userLibraryId, userId, isbn);
userLibraryService.deleteUserLibrary(userLibraryId, userId);

return ApiResponse.ok();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,17 @@ public void editUserLibrary(Long userLibraryId, Long userId, UserLibraryEditServ
}

@Transactional
public void deleteUserLibrary(Long userLibraryId, Long userId, String isbn) {
public void deleteUserLibrary(Long userLibraryId, Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));

Book book = bookRepository.findById(isbn)
.orElseThrow(() -> new BusinessException(ErrorCode.BOOK_NOT_FOUND));

UserLibrary userLibrary = userLibraryRepository.findByIdWithBookAndUser(userLibraryId)
.orElseThrow(() -> new BusinessException(ErrorCode.USER_LIBRARY_NOT_FOUND));

if (!userLibrary.isUserLibraryOwner(user.getId())) {
throw new BusinessException(ErrorCode.UNAUTHORIZED_REQUEST);
}

if (!userLibrary.isSameBook(book.getIsbn())) {
throw new BusinessException(ErrorCode.BOOK_INVALID_INFO);
}

userLibraryRepository.deleteById(userLibrary.getId());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,27 +171,12 @@ public void deleteUserLibrary() throws Exception {
Long userLibraryId = 1L;

// when // then
mockMvc.perform(delete("/v1/user-libraries/{userLibraryId}", userLibraryId)
.param("isbn", "0000X"))
mockMvc.perform(delete("/v1/user-libraries/{userLibraryId}", userLibraryId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value("200"))
.andExpect(jsonPath("$.status").value("OK"))
.andExpect(jsonPath("$.message").value("OK"))
.andDo(print());
}

@Test
@DisplayName("서재 정보 삭제 시 책 isbn 입력은 필수이다.")
public void deleteUserLibraryWithoutIsbn() throws Exception {
// given
Long userLibraryId = 1L;

// when // then
mockMvc.perform(delete("/v1/user-libraries/{userLibraryId}", userLibraryId))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("400"))
.andExpect(jsonPath("$.message").value("유효하지 않은 파라미터 입니다."))
.andDo(print());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public void deleteUserLibrary() {
UserLibrary userLibrary = userLibraryRepository.save(create(user, book));

// when
userLibraryService.deleteUserLibrary(userLibrary.getId(), user.getId(), book.getIsbn());
userLibraryService.deleteUserLibrary(userLibrary.getId(), user.getId());

// then
List<UserLibrary> response = userLibraryRepository.findAll();
Expand All @@ -344,35 +344,20 @@ public void deleteUserLibraryWithoutUser() {
Book book = bookRepository.save(createBook());

// when // then
assertThatThrownBy(() -> userLibraryService.deleteUserLibrary(userLibraryId, userId, book.getIsbn()))
assertThatThrownBy(() -> userLibraryService.deleteUserLibrary(userLibraryId, userId))
.isInstanceOf(BusinessException.class)
.hasMessage("사용자를 찾을 수 없습니다.");
}

@Test
@DisplayName("서재 정보 삭제 시 책 정보가 존재해야 한다.")
public void deleteUserLibraryWithoutBook() {
// given
Long userLibraryId = 1L;
String bookIsbn = "0000X";
User user = userRepository.save(createUser());

// when // then
assertThatThrownBy(() -> userLibraryService.deleteUserLibrary(userLibraryId, user.getId(), bookIsbn))
.isInstanceOf(BusinessException.class)
.hasMessage("책을 찾을 수 없습니다.");
}

@Test
@DisplayName("서재 정보 삭제 시 서재 정보가 존재해야 한다.")
public void deleteUserLibraryWithoutUserLibrary() {
// given
Long userLibraryId = 1L;
User user = userRepository.save(createUser());
Book book = bookRepository.save(createBook());

// when // then
assertThatThrownBy(() -> userLibraryService.deleteUserLibrary(userLibraryId, user.getId(), book.getIsbn()))
assertThatThrownBy(() -> userLibraryService.deleteUserLibrary(userLibraryId, user.getId()))
.isInstanceOf(BusinessException.class)
.hasMessage("서재 정보를 찾을 수 없습니다.");
}
Expand All @@ -390,29 +375,11 @@ public void deleteUserLibraryInvalidUser() {

// when // then
assertThatThrownBy(
() -> userLibraryService.deleteUserLibrary(userLibrary.getId(), anotherUser.getId(), book.getIsbn()))
() -> userLibraryService.deleteUserLibrary(userLibrary.getId(), anotherUser.getId()))
.isInstanceOf(BusinessException.class)
.hasMessage("권한이 없는 사용자입니다.");
}

@Test
@DisplayName("서재 정보 삭제 시 서재 정보와 도서 정보는 일치해야 한다.")
public void deleteUserLibraryInvalidBook() {
// given
User user = userRepository.save(createUser());

Book book = bookRepository.save(createBookWithIsbn("00001"));
Book anotherBook = bookRepository.save(createBookWithIsbn("00002"));

UserLibrary userLibrary = userLibraryRepository.save(create(user, book));

// when // then
assertThatThrownBy(
() -> userLibraryService.deleteUserLibrary(userLibrary.getId(), user.getId(), anotherBook.getIsbn()))
.isInstanceOf(BusinessException.class)
.hasMessage("올바르지 않은 책 정보 입니다.");
}

private static User createUser() {
return User.builder()
.name("[email protected]")
Expand Down
Loading

0 comments on commit 12356fb

Please sign in to comment.