-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
api/src/main/java/com/dnd/sbooky/api/book/DeleteBookController.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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.dnd.sbooky.api.book; | ||
|
||
import com.dnd.sbooky.api.docs.spec.DeleteBookApiSpec; | ||
import com.dnd.sbooky.api.support.response.ApiResponse; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api") | ||
@RequiredArgsConstructor | ||
public class DeleteBookController implements DeleteBookApiSpec { | ||
|
||
private final DeleteBookUseCase deleteBookUseCase; | ||
|
||
@DeleteMapping("/books/{memberBookId}") | ||
public ApiResponse<?> deleteBook( | ||
@PathVariable Long memberBookId, | ||
@Parameter(hidden = true) @AuthenticationPrincipal UserDetails user) { | ||
|
||
deleteBookUseCase.delete(extractMemberId(user), memberBookId); | ||
return ApiResponse.success(); | ||
} | ||
|
||
private Long extractMemberId(UserDetails user) { | ||
return Long.parseLong(user.getUsername()); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
api/src/main/java/com/dnd/sbooky/api/book/DeleteBookUseCase.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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.dnd.sbooky.api.book; | ||
|
||
import com.dnd.sbooky.api.book.exception.BookForbiddenException; | ||
import com.dnd.sbooky.api.book.exception.BookNotFoundException; | ||
import com.dnd.sbooky.api.support.error.ErrorType; | ||
import com.dnd.sbooky.core.book.MemberBookEntity; | ||
import com.dnd.sbooky.core.book.MemberBookRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class DeleteBookUseCase { | ||
|
||
private final MemberBookRepository memberBookRepository; | ||
|
||
public void delete(Long memberId, Long memberBookId) { | ||
|
||
MemberBookEntity memberBook = | ||
memberBookRepository | ||
.findById(memberBookId) | ||
.orElseThrow(() -> new BookNotFoundException(ErrorType.BOOK_NOT_FOUND)); | ||
|
||
if (!memberBook.isSameMember(memberId)) { | ||
throw new BookForbiddenException(ErrorType.BOOK_ACCESS_FORBIDDEN); | ||
} | ||
|
||
memberBookRepository.delete(memberBook); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
api/src/main/java/com/dnd/sbooky/api/docs/spec/DeleteBookApiSpec.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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.dnd.sbooky.api.docs.spec; | ||
|
||
import com.dnd.sbooky.api.support.response.ApiResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
|
||
@Tag(name = "[Book API]", description = "책 삭제에 관련된 API") | ||
@SecurityRequirement(name = "access-token") | ||
public interface DeleteBookApiSpec { | ||
|
||
@Operation(summary = "책 삭제", description = "회원이 등록한 책을 삭제한다.") | ||
ApiResponse<?> deleteBook(Long memberBookId, UserDetails user); | ||
} |