-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from MARU-EGG/refactor/question-list-response
[feat] answer 답변 내용 수정 api
- Loading branch information
Showing
16 changed files
with
318 additions
and
20 deletions.
There are no files selected for viewing
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
38 changes: 38 additions & 0 deletions
38
src/main/java/mju/iphak/maru_egg/answer/api/AdminAnswerController.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,38 @@ | ||
package mju.iphak.maru_egg.answer.api; | ||
|
||
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 io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import mju.iphak.maru_egg.answer.application.AnswerService; | ||
import mju.iphak.maru_egg.answer.dto.request.UpdateAnswerContentRequest; | ||
import mju.iphak.maru_egg.common.meta.CustomApiResponse; | ||
import mju.iphak.maru_egg.common.meta.CustomApiResponses; | ||
|
||
@Tag(name = "Answer API", description = "답변 관련 API 입니다.") | ||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/api/admin/answers") | ||
public class AdminAnswerController { | ||
|
||
private final AnswerService answerService; | ||
|
||
@Operation(summary = "답변 수정", description = "답변을 수정하는 API", responses = { | ||
@ApiResponse(responseCode = "200", description = "답변을 수정 성공") | ||
}) | ||
@CustomApiResponses({ | ||
@CustomApiResponse(error = "HttpMessageNotReadableException", status = 400, message = "Invalid input format: JSON parse error: Cannot deserialize value of type `java.lang.Long` from String \"ㅇㅇ\": not a valid `java.lang.Long` value", description = "잘못된 요청 값을 보낸 경우"), | ||
@CustomApiResponse(error = "EntityNotFoundException", status = 404, message = "답변 id가 123131인 답변을 찾을 수 없습니다.", description = "답변을 찾지 못한 경우"), | ||
@CustomApiResponse(error = "InternalServerError", status = 500, message = "내부 서버 오류가 발생했습니다.", description = "내부 서버 오류") | ||
}) | ||
@PostMapping() | ||
public void updateAnswerContent(@Valid @RequestBody UpdateAnswerContentRequest request) { | ||
answerService.updateAnswerContent(request.id(), request.content()); | ||
} | ||
} |
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
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
21 changes: 21 additions & 0 deletions
21
src/main/java/mju/iphak/maru_egg/answer/dto/request/UpdateAnswerContentRequest.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,21 @@ | ||
package mju.iphak.maru_egg.answer.dto.request; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotBlank; | ||
|
||
@Schema(description = "답변 수정 요청 DTO", example = """ | ||
{ | ||
"id": 601440912047263653, | ||
"content": "변경된 답변입니다." | ||
} | ||
""") | ||
public record UpdateAnswerContentRequest( | ||
|
||
@Schema(description = "답변 ID") | ||
Long id, | ||
|
||
@Schema(description = "변경할 답변 내용") | ||
@NotBlank(message = "변경할 답변은 비어있을 수 없습니다.") | ||
String content | ||
) { | ||
} |
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
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
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
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
48 changes: 48 additions & 0 deletions
48
src/main/java/mju/iphak/maru_egg/question/dto/response/QuestionListItemResponse.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,48 @@ | ||
package mju.iphak.maru_egg.question.dto.response; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.Builder; | ||
import mju.iphak.maru_egg.answer.dto.response.AnswerResponse; | ||
import mju.iphak.maru_egg.question.domain.Question; | ||
|
||
@Builder | ||
@Schema(description = "질문 응답 DTO", example = """ | ||
{ | ||
"id": 600697396846981500, | ||
"content": "수시 일정 알려주세요." | ||
"dateInformation": "생성일자: 2024-07-15T23:36:59.834804, 마지막 DB 갱신일자: 2024-07-15T23:36:59.834804", | ||
"answer": { | ||
"id": 600697396935061600, | ||
"content": "2024년 수시 일정은 다음과 같습니다:\\n\\n- 전체 전형 2024.12.19.(목)~12.26.(목) 18:00: 최초합격자 발표 및 시충원 관련 내용 공지 예정\\n- 문서등록 및 등록금 납부 기간: 2025. 2. 10.(월) 10:00 ~ 2. 12.(수) 15:00\\n- 등록금 납부 기간: 2024.12.16.(월) 10:00 ~ 12. 18.(수) 15:00\\n\\n추가로, 복수지원 관련하여 수시모집의 모든 전형에 중복 지원이 가능하며, 최대 6개 이내의 전형에만 지원할 수 있습니다. 반드시 지정 기간 내에 문서등록과 최종 등록(등록금 납부)을 해야 합니다. 또한, 합격자는 합격한 대학에 등록하지 않을 경우 합격 포기로 간주되니 유의하시기 바랍니다.", | ||
"renewalYear": 2024, | ||
"dateInformation": "생성일자: 2024-07-15T23:36:59.847690, 마지막 DB 갱신일자: 2024-07-15T23:36:59.847690" | ||
} | ||
} | ||
""") | ||
public record QuestionListItemResponse( | ||
@Schema(description = "질문 id") | ||
Long id, | ||
|
||
@Schema(description = "질문") | ||
String content, | ||
|
||
@Schema(description = "질문 조회수") | ||
int viewCount, | ||
|
||
@Schema(description = "답변 DB 생성 및 업데이트 날짜") | ||
String dateInformation, | ||
|
||
@Schema(description = "답변 DTO") | ||
AnswerResponse answer | ||
) { | ||
|
||
public static QuestionListItemResponse of(Question question, AnswerResponse answer) { | ||
return QuestionListItemResponse.builder() | ||
.id(question.getId()) | ||
.content(question.getContent()) | ||
.viewCount(question.getViewCount()) | ||
.dateInformation(question.getDateInformation()) | ||
.answer(answer) | ||
.build(); | ||
} | ||
} |
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
102 changes: 102 additions & 0 deletions
102
src/test/java/mju/iphak/maru_egg/answer/api/AdminAnswerControllerTest.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,102 @@ | ||
package mju.iphak.maru_egg.answer.api; | ||
|
||
import static org.mockito.ArgumentMatchers.*; | ||
import static org.mockito.Mockito.*; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
|
||
import jakarta.persistence.EntityNotFoundException; | ||
import mju.iphak.maru_egg.answer.application.AnswerService; | ||
import mju.iphak.maru_egg.answer.dto.request.UpdateAnswerContentRequest; | ||
import mju.iphak.maru_egg.common.IntegrationTest; | ||
|
||
class AdminAnswerControllerTest extends IntegrationTest { | ||
|
||
@MockBean | ||
private AnswerService answerService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@Test | ||
@WithMockUser(roles = "ADMIN") | ||
void 답변_수정_API_정상적인_요청() throws Exception { | ||
// given | ||
UpdateAnswerContentRequest request = new UpdateAnswerContentRequest(1L, "새로운 답변 내용"); | ||
|
||
// when | ||
ResultActions resultActions = mvc.perform(post("/api/admin/answers") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(request))); | ||
|
||
// then | ||
resultActions | ||
.andExpect(status().isOk()) | ||
.andDo(print()); | ||
} | ||
|
||
@Test | ||
@WithMockUser(roles = "ADMIN") | ||
void 답변_수정_API_잘못된_JSON_형식() throws Exception { | ||
// given | ||
String invalidJson = "{\"id\": \"ㅇㅇ\", \"content\": \"새로운 답변 내용\"}"; | ||
|
||
// when | ||
ResultActions resultActions = mvc.perform(post("/api/admin/answers") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(invalidJson)); | ||
|
||
// then | ||
resultActions | ||
.andExpect(status().isBadRequest()) | ||
.andDo(print()); | ||
} | ||
|
||
@Test | ||
@WithMockUser(roles = "ADMIN") | ||
void 답변_수정_API_존재하지_않는_답변_ID() throws Exception { | ||
// given | ||
UpdateAnswerContentRequest request = new UpdateAnswerContentRequest(999L, "새로운 답변 내용"); | ||
doThrow(new EntityNotFoundException("답변을 찾을 수 없습니다.")).when(answerService) | ||
.updateAnswerContent(anyLong(), anyString()); | ||
|
||
// when | ||
ResultActions resultActions = mvc.perform(post("/api/admin/answers") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(request))); | ||
|
||
// then | ||
resultActions | ||
.andExpect(status().isNotFound()) | ||
.andDo(print()); | ||
} | ||
|
||
@Test | ||
@WithMockUser(roles = "ADMIN") | ||
void 답변_수정_API_서버_내부_오류() throws Exception { | ||
// given | ||
UpdateAnswerContentRequest request = new UpdateAnswerContentRequest(1L, "새로운 답변 내용"); | ||
doThrow(new RuntimeException("내부 서버 오류")).when(answerService).updateAnswerContent(anyLong(), anyString()); | ||
|
||
// when | ||
ResultActions resultActions = mvc.perform(post("/api/admin/answers") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(request))); | ||
|
||
// then | ||
resultActions | ||
.andExpect(status().isInternalServerError()) | ||
.andDo(print()); | ||
} | ||
} |
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
Oops, something went wrong.