-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 리뷰 답변 수정 엔드포인트 추가 * test: 리뷰 답변 수정
- Loading branch information
Showing
6 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
src/main/java/com/liberty52/product/service/applicationservice/ReplyModifyService.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,7 @@ | ||
package com.liberty52.product.service.applicationservice; | ||
|
||
import com.liberty52.product.service.controller.dto.ReplyModifyRequestDto; | ||
|
||
public interface ReplyModifyService { | ||
void modify(String adminId, String role, ReplyModifyRequestDto dto, String reviewId, String replyId); | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/liberty52/product/service/applicationservice/ReplyModifyServiceImpl.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,31 @@ | ||
package com.liberty52.product.service.applicationservice; | ||
|
||
import com.liberty52.product.global.exception.external.badrequest.BadRequestException; | ||
import com.liberty52.product.global.exception.external.forbidden.InvalidRoleException; | ||
import com.liberty52.product.global.exception.external.notfound.ResourceNotFoundException; | ||
import com.liberty52.product.service.controller.dto.ReplyModifyRequestDto; | ||
import com.liberty52.product.service.entity.Reply; | ||
import com.liberty52.product.service.repository.ReplyRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import static com.liberty52.product.global.contants.RoleConstants.ADMIN; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class ReplyModifyServiceImpl implements ReplyModifyService { | ||
private final ReplyRepository replyRepository; | ||
|
||
@Override | ||
public void modify(String adminId, String role, ReplyModifyRequestDto dto, String reviewId, String replyId) { | ||
if(!ADMIN.equals(role)) | ||
throw new InvalidRoleException(role); | ||
Reply reply = replyRepository.findById(replyId) | ||
.orElseThrow(() -> new ResourceNotFoundException("Reply", "id", replyId)); | ||
if(!reply.getReview().getId().equals(reviewId)) | ||
throw new BadRequestException("Review와 Reply가 매칭되지 않습니다."); | ||
reply.modify(dto.getContent()); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/liberty52/product/service/controller/ReplyModifyController.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.liberty52.product.service.controller; | ||
|
||
|
||
import com.liberty52.product.service.applicationservice.ReplyCreateService; | ||
import com.liberty52.product.service.applicationservice.ReplyModifyService; | ||
import com.liberty52.product.service.controller.dto.ReplyCreateRequestDto; | ||
import com.liberty52.product.service.controller.dto.ReplyModifyRequestDto; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class ReplyModifyController { | ||
|
||
private final ReplyModifyService replyModifyService; | ||
|
||
@PutMapping("/reviews/{reviewId}/replies/{replyId}") | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public void replyModify(@RequestHeader(HttpHeaders.AUTHORIZATION) String adminId, | ||
@RequestHeader("LB-Role") String role, | ||
@Validated @RequestBody ReplyModifyRequestDto dto, | ||
@PathVariable String reviewId, | ||
@PathVariable String replyId) { | ||
replyModifyService.modify(adminId, role, dto, reviewId, replyId); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/liberty52/product/service/controller/dto/ReplyModifyRequestDto.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,17 @@ | ||
package com.liberty52.product.service.controller.dto; | ||
|
||
import jakarta.validation.constraints.NotEmpty; | ||
import jakarta.validation.constraints.Size; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class ReplyModifyRequestDto { | ||
@NotEmpty | ||
@Size(min = 1, max = 1000) | ||
private String content; | ||
public static ReplyModifyRequestDto createForTest(String content) { | ||
ReplyModifyRequestDto dto = new ReplyModifyRequestDto(); | ||
dto.content = content; | ||
return dto; | ||
} | ||
} |
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
80 changes: 80 additions & 0 deletions
80
...est/java/com/liberty52/product/service/applicationservice/ReplyModifyServiceImplTest.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,80 @@ | ||
package com.liberty52.product.service.applicationservice; | ||
|
||
import com.liberty52.product.global.config.DBInitConfig.DBInitService; | ||
import com.liberty52.product.global.exception.external.badrequest.BadRequestException; | ||
import com.liberty52.product.global.exception.external.forbidden.InvalidRoleException; | ||
import com.liberty52.product.global.exception.external.notfound.ResourceNotFoundException; | ||
import com.liberty52.product.service.controller.dto.ReplyModifyRequestDto; | ||
import com.liberty52.product.service.entity.Orders; | ||
import com.liberty52.product.service.entity.Product; | ||
import com.liberty52.product.service.entity.Reply; | ||
import com.liberty52.product.service.entity.Review; | ||
import com.liberty52.product.service.repository.ReplyRepository; | ||
import com.liberty52.product.service.repository.ReviewRepository; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.UUID; | ||
|
||
import static com.liberty52.product.global.contants.RoleConstants.ADMIN; | ||
|
||
@Transactional | ||
@SpringBootTest | ||
class ReplyModifyServiceImplTest { | ||
|
||
@Autowired | ||
ReplyModifyService service; | ||
|
||
final String mockAdminId = "bar"; | ||
final String mockReplyContent = "Hello world"; | ||
String reviewId; | ||
String replyId; | ||
@Autowired | ||
private ReviewRepository reviewRepository; | ||
@Autowired | ||
private ReplyRepository replyRepository; | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
Review review = DBInitService.getReview(); | ||
Reply reply = Reply.create(mockReplyContent, mockAdminId); | ||
reply.associate(review); | ||
reviewRepository.save(review); | ||
reviewId = review.getId(); | ||
replyId = reply.getId(); | ||
} | ||
|
||
@Test | ||
void Modify_Reply_Success() { | ||
String newContent = UUID.randomUUID().toString(); | ||
service.modify(mockAdminId, ADMIN, ReplyModifyRequestDto.createForTest(newContent), reviewId, replyId); | ||
Reply reply = replyRepository.findById(replyId).get(); | ||
Assertions.assertEquals(newContent, reply.getContent()); | ||
} | ||
|
||
@Test | ||
void InvalidRoleException() { | ||
Assertions.assertThrows(InvalidRoleException.class, () -> service.modify(mockAdminId, UUID.randomUUID().toString(), ReplyModifyRequestDto.createForTest(UUID.randomUUID().toString()), reviewId, replyId)); | ||
} | ||
|
||
@Test | ||
void ResourceNotFoundException() { | ||
Assertions.assertThrows(ResourceNotFoundException.class, () -> service.modify(mockAdminId, ADMIN, ReplyModifyRequestDto.createForTest(UUID.randomUUID().toString()), reviewId, UUID.randomUUID().toString())); | ||
} | ||
|
||
@Test | ||
void BadRequest() { | ||
Product product = DBInitService.getProduct(); | ||
Orders order = DBInitService.getOrder(); | ||
Review otherReview = Review.create(3, "content"); | ||
otherReview.associate(order); | ||
otherReview.associate(product); | ||
reviewRepository.save(otherReview); | ||
|
||
Assertions.assertThrows(BadRequestException.class, () -> service.modify(mockAdminId, ADMIN, ReplyModifyRequestDto.createForTest(UUID.randomUUID().toString()), otherReview.getId(), replyId)); | ||
} | ||
} |