Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.hansung.leafly.domain.bookreview.exception;

import com.hansung.leafly.global.exception.BaseException;
import com.hansung.leafly.global.response.code.BaseResponseCode;

public class BookReviewAccessDeniedException extends BaseException {
public BookReviewAccessDeniedException() {
super(BookReviewErrorCode.BOOK_REVIEW_ACCESS_DENIED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.hansung.leafly.domain.bookreview.exception;

import com.hansung.leafly.global.response.code.BaseResponseCode;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum BookReviewErrorCode implements BaseResponseCode {
BOOK_REVIEW_ACCESS_DENIED("BOOK_REVIEW_403_1", 403, "본인이 작성한 리뷰만 삭제할 수 있습니다."),
BOOK_REVIEW_NOT_FOUND("BOOK_REVIEW_404_1",404,"해당 독후감을 찾을 수 없습니다." );

private final String code;
private final int httpStatus;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.hansung.leafly.domain.bookreview.exception;

import com.hansung.leafly.global.exception.BaseException;
import com.hansung.leafly.global.response.code.BaseResponseCode;

public class BookReviewNotFoundException extends BaseException {
public BookReviewNotFoundException() {
super(BookReviewErrorCode.BOOK_REVIEW_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@

public interface BookReviewService {
void create(Member member, ReviewReq req);

void delete(Long reviewId, Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.hansung.leafly.domain.bookreview.entity.BookReview;
import com.hansung.leafly.domain.bookreview.entity.BookTag;
import com.hansung.leafly.domain.bookreview.entity.ReviewImage;
import com.hansung.leafly.domain.bookreview.exception.BookReviewAccessDeniedException;
import com.hansung.leafly.domain.bookreview.exception.BookReviewNotFoundException;
import com.hansung.leafly.domain.bookreview.repository.BookReviewRepository;
import com.hansung.leafly.domain.bookreview.repository.BookTagRepository;
import com.hansung.leafly.domain.bookreview.repository.ReviewImageRepository;
Expand Down Expand Up @@ -41,6 +43,19 @@ public void create(Member member, ReviewReq req) {
reviewImageRepository.saveAll(images);
}

@Override
@Transactional
public void delete(Long reviewId, Member member) {
BookReview bookReview = bookReviewRepository.findById(reviewId)
.orElseThrow(()-> new BookReviewNotFoundException());

if(!bookReview.getMember().getId().equals(member.getId())) {
throw new BookReviewAccessDeniedException();
}

bookReviewRepository.delete(bookReview);
}

// 카테고리 태그화
private List<BookTag> processTags(String rawTags, BookReview review) {
if (rawTags == null || rawTags.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hansung.leafly.domain.bookreview.web.controller;

import com.amazonaws.Response;
import com.hansung.leafly.domain.bookreview.service.BookReviewService;
import com.hansung.leafly.domain.bookreview.web.dto.ReviewReq;
import com.hansung.leafly.global.auth.security.CustomMemberDetails;
Expand All @@ -17,6 +18,7 @@
public class BookReviewController {
private final BookReviewService bookReviewService;

//독후감 생성
@PostMapping
public ResponseEntity<SuccessResponse<?>> create(
@AuthenticationPrincipal CustomMemberDetails memberDetails,
Expand All @@ -25,4 +27,14 @@ public ResponseEntity<SuccessResponse<?>> create(
bookReviewService.create(memberDetails.getMember(), req);
return ResponseEntity.status(HttpStatus.CREATED).body(SuccessResponse.created(null));
}

//독후감 삭제
@DeleteMapping("/{reviewId}")
public ResponseEntity<SuccessResponse<?>> delete(
@AuthenticationPrincipal CustomMemberDetails memberDetails,
@PathVariable Long reviewId
){
bookReviewService.delete(reviewId,memberDetails.getMember());
return ResponseEntity.status(HttpStatus.OK).body(SuccessResponse.from(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static <T> SuccessResponse<T> of(T data, BaseResponseCode baseResponseCod
return new SuccessResponse<>(data, baseResponseCode);
}

public static SuccessResponse<?> from(BaseResponseCode baseResponseCode){
public static SuccessResponse<?> fromCode(BaseResponseCode baseResponseCode){
return new SuccessResponse<>(null, baseResponseCode);
}
}