From 142d90f26688f771597552902e3a1a44d58ede61 Mon Sep 17 00:00:00 2001 From: taeyeon Date: Wed, 17 Apr 2024 18:05:13 +0900 Subject: [PATCH 1/4] add :: BookNotFoundException --- .../domain/book/exception/BookNotFoundException.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/java/com/mindway/server/v2/domain/book/exception/BookNotFoundException.java diff --git a/src/main/java/com/mindway/server/v2/domain/book/exception/BookNotFoundException.java b/src/main/java/com/mindway/server/v2/domain/book/exception/BookNotFoundException.java new file mode 100644 index 0000000..f134c9e --- /dev/null +++ b/src/main/java/com/mindway/server/v2/domain/book/exception/BookNotFoundException.java @@ -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); + } +} From 14a99212336729ccc069f8106351c38fc5894615 Mon Sep 17 00:00:00 2001 From: taeyeon Date: Wed, 17 Apr 2024 18:05:28 +0900 Subject: [PATCH 2/4] add :: NotSameAuthorException --- .../domain/book/exception/NotSameAuthorException.java | 10 ++++++++++ .../mindway/server/v2/global/exception/ErrorCode.java | 6 +++++- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/mindway/server/v2/domain/book/exception/NotSameAuthorException.java diff --git a/src/main/java/com/mindway/server/v2/domain/book/exception/NotSameAuthorException.java b/src/main/java/com/mindway/server/v2/domain/book/exception/NotSameAuthorException.java new file mode 100644 index 0000000..42ef937 --- /dev/null +++ b/src/main/java/com/mindway/server/v2/domain/book/exception/NotSameAuthorException.java @@ -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); + } +} diff --git a/src/main/java/com/mindway/server/v2/global/exception/ErrorCode.java b/src/main/java/com/mindway/server/v2/global/exception/ErrorCode.java index c2eee97..42d9e2f 100644 --- a/src/main/java/com/mindway/server/v2/global/exception/ErrorCode.java +++ b/src/main/java/com/mindway/server/v2/global/exception/ErrorCode.java @@ -28,7 +28,11 @@ public enum ErrorCode { NOT_EXIST_GOAL(404, "유저가 설정한 목표가 존재하지 않습니다"), /* notice */ - NOT_FOUND_NOTICE(404, "등록된 공지가 존재하지 않습니다."); + NOT_FOUND_NOTICE(404, "등록된 공지가 존재하지 않습니다."), + + /* book */ + NOT_FOUND_BOOK(404, "등록된 독후감을 찾을 수 없습니다."), + NOT_SAME_AUTHOR(403, "작성자가 아닙니다."); private final int status; private final String message; From 12cc7f91d179808d4d11e8b335df7a42a28a8afc Mon Sep 17 00:00:00 2001 From: taeyeon Date: Wed, 17 Apr 2024 18:05:44 +0900 Subject: [PATCH 3/4] =?UTF-8?q?create=20::=20=EB=8F=85=ED=9B=84=EA=B0=90?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../book/service/BookDeleteService.java | 5 +++ .../service/impl/BookDeleteServiceImpl.java | 32 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/main/java/com/mindway/server/v2/domain/book/service/BookDeleteService.java create mode 100644 src/main/java/com/mindway/server/v2/domain/book/service/impl/BookDeleteServiceImpl.java diff --git a/src/main/java/com/mindway/server/v2/domain/book/service/BookDeleteService.java b/src/main/java/com/mindway/server/v2/domain/book/service/BookDeleteService.java new file mode 100644 index 0000000..a22ee1e --- /dev/null +++ b/src/main/java/com/mindway/server/v2/domain/book/service/BookDeleteService.java @@ -0,0 +1,5 @@ +package com.mindway.server.v2.domain.book.service; + +public interface BookDeleteService { + void execute(Long id); +} diff --git a/src/main/java/com/mindway/server/v2/domain/book/service/impl/BookDeleteServiceImpl.java b/src/main/java/com/mindway/server/v2/domain/book/service/impl/BookDeleteServiceImpl.java new file mode 100644 index 0000000..c0823a9 --- /dev/null +++ b/src/main/java/com/mindway/server/v2/domain/book/service/impl/BookDeleteServiceImpl.java @@ -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); + } +} From c3f85ed926327262ac831b8cfe509625487938d1 Mon Sep 17 00:00:00 2001 From: taeyeon Date: Wed, 17 Apr 2024 18:06:31 +0900 Subject: [PATCH 4/4] =?UTF-8?q?add=20::=20endpoint=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../v2/domain/book/presentation/BookController.java | 13 +++++++++---- .../v2/global/security/config/SecurityConfig.java | 1 + 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/mindway/server/v2/domain/book/presentation/BookController.java b/src/main/java/com/mindway/server/v2/domain/book/presentation/BookController.java index 222347e..13d7c3b 100644 --- a/src/main/java/com/mindway/server/v2/domain/book/presentation/BookController.java +++ b/src/main/java/com/mindway/server/v2/domain/book/presentation/BookController.java @@ -1,15 +1,13 @@ 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 @@ -17,10 +15,17 @@ public class BookController { private final BookWriteService bookWriteService; + private final BookDeleteService bookDeleteService; @PostMapping public ResponseEntity writeBookReport (@RequestBody @Valid BookWriteRequest bookWriteRequest) { bookWriteService.execute(bookWriteRequest); return ResponseEntity.status(HttpStatus.CREATED).build(); } + + @DeleteMapping("/{book_id}") + public ResponseEntity deleteBookReport (@PathVariable(value = "book_id") Long id) { + bookDeleteService.execute(id); + return ResponseEntity.noContent().build(); + } } diff --git a/src/main/java/com/mindway/server/v2/global/security/config/SecurityConfig.java b/src/main/java/com/mindway/server/v2/global/security/config/SecurityConfig.java index 0a1d174..2568cf5 100644 --- a/src/main/java/com/mindway/server/v2/global/security/config/SecurityConfig.java +++ b/src/main/java/com/mindway/server/v2/global/security/config/SecurityConfig.java @@ -66,6 +66,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { // book .requestMatchers(HttpMethod.POST, "/api/v2/book").authenticated() + .requestMatchers(HttpMethod.DELETE, "api/v2/book/{book_id}").authenticated() // notice .requestMatchers(HttpMethod.POST, "/api/v2/notice").hasAnyAuthority(Authority.ROLE_TEACHER.name(), Authority.ROLE_HELPER.name())