Skip to content

Commit

Permalink
LIB-192 리뷰 답변 수정 (#107)
Browse files Browse the repository at this point in the history
* feat: 리뷰 답변 수정 엔드포인트 추가

* test: 리뷰 답변 수정
  • Loading branch information
jinlee0 authored May 7, 2023
1 parent 5bba112 commit 5e8ebea
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 0 deletions.
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);
}
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());
}
}
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);
}

}
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;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/liberty52/product/service/entity/Reply.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ public void removeReview() {
review.removeReply(this);
review = null;
}

public void modify(String content) {
this.content = content;
validContent();
}
}
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));
}
}

0 comments on commit 5e8ebea

Please sign in to comment.