Skip to content

Commit

Permalink
Merge pull request #84 from Team-MindWay/82-book-report-delete
Browse files Browse the repository at this point in the history
독후감 삭제 api
  • Loading branch information
ta2ye0n authored Apr 17, 2024
2 parents 3d9190e + c3f85ed commit 351e2fc
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 5 deletions.
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);
}
}
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);
}
}
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();
}
}
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);
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 351e2fc

Please sign in to comment.