-
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.
Merge pull request #84 from Team-MindWay/82-book-report-delete
독후감 삭제 api
- Loading branch information
Showing
7 changed files
with
72 additions
and
5 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
src/main/java/com/mindway/server/v2/domain/book/exception/BookNotFoundException.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,10 @@ | ||
package com.mindway.server.v2.domain.book.exception; | ||
|
||
import com.mindway.server.v2.global.exception.ErrorCode; | ||
import com.mindway.server.v2.global.exception.MindWayException; | ||
|
||
public class BookNotFoundException extends MindWayException { | ||
public BookNotFoundException() { | ||
super(ErrorCode.NOT_FOUND_BOOK); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/mindway/server/v2/domain/book/exception/NotSameAuthorException.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,10 @@ | ||
package com.mindway.server.v2.domain.book.exception; | ||
|
||
import com.mindway.server.v2.global.exception.ErrorCode; | ||
import com.mindway.server.v2.global.exception.MindWayException; | ||
|
||
public class NotSameAuthorException extends MindWayException { | ||
public NotSameAuthorException() { | ||
super(ErrorCode.NOT_SAME_AUTHOR); | ||
} | ||
} |
13 changes: 9 additions & 4 deletions
13
src/main/java/com/mindway/server/v2/domain/book/presentation/BookController.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 |
---|---|---|
@@ -1,26 +1,31 @@ | ||
package com.mindway.server.v2.domain.book.presentation; | ||
|
||
import com.mindway.server.v2.domain.book.presentation.dto.request.BookWriteRequest; | ||
import com.mindway.server.v2.domain.book.service.BookDeleteService; | ||
import com.mindway.server.v2.domain.book.service.BookWriteService; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v2/book") | ||
public class BookController { | ||
|
||
private final BookWriteService bookWriteService; | ||
private final BookDeleteService bookDeleteService; | ||
|
||
@PostMapping | ||
public ResponseEntity<Void> writeBookReport (@RequestBody @Valid BookWriteRequest bookWriteRequest) { | ||
bookWriteService.execute(bookWriteRequest); | ||
return ResponseEntity.status(HttpStatus.CREATED).build(); | ||
} | ||
|
||
@DeleteMapping("/{book_id}") | ||
public ResponseEntity<Void> deleteBookReport (@PathVariable(value = "book_id") Long id) { | ||
bookDeleteService.execute(id); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/mindway/server/v2/domain/book/service/BookDeleteService.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,5 @@ | ||
package com.mindway.server.v2.domain.book.service; | ||
|
||
public interface BookDeleteService { | ||
void execute(Long id); | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/mindway/server/v2/domain/book/service/impl/BookDeleteServiceImpl.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,32 @@ | ||
package com.mindway.server.v2.domain.book.service.impl; | ||
|
||
import com.mindway.server.v2.domain.book.entity.Book; | ||
import com.mindway.server.v2.domain.book.exception.BookNotFoundException; | ||
import com.mindway.server.v2.domain.book.exception.NotSameAuthorException; | ||
import com.mindway.server.v2.domain.book.presentation.dto.request.BookWriteRequest; | ||
import com.mindway.server.v2.domain.book.repository.BookRepository; | ||
import com.mindway.server.v2.domain.book.service.BookDeleteService; | ||
import com.mindway.server.v2.domain.user.entity.User; | ||
import com.mindway.server.v2.domain.user.util.UserUtil; | ||
import com.mindway.server.v2.global.annotation.ServiceWithTransaction; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@ServiceWithTransaction | ||
@RequiredArgsConstructor | ||
public class BookDeleteServiceImpl implements BookDeleteService { | ||
|
||
private final BookRepository bookRepository; | ||
private final UserUtil userUtil; | ||
|
||
public void execute(Long id) { | ||
User user = userUtil.getCurrentUser(); | ||
Book book = bookRepository.findById(id) | ||
.orElseThrow(BookNotFoundException::new); | ||
|
||
if (user != book.getUser()) { | ||
throw new NotSameAuthorException(); | ||
} | ||
|
||
bookRepository.delete(book); | ||
} | ||
} |
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
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