Repository navigation
[feature] 약관 조회 API 추가 #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/com/nexters/gotggam/consent/controller/ConsentController.java
This file contains hidden or 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,28 @@ | ||
| package com.nexters.gotggam.consent.controller; | ||
|
|
||
| import com.nexters.gotggam.consent.dto.ConsentDocumentResponse; | ||
| import com.nexters.gotggam.consent.service.ConsentService; | ||
| import com.nexters.gotggam.global.payload.ApiResponse; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import java.util.List; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @Tag(name = "Consent API", description = "약관 API") | ||
| @RestController | ||
| @RequestMapping("/api/v1/consent-documents") | ||
| @RequiredArgsConstructor | ||
| public class ConsentController { | ||
|
|
||
| private final ConsentService consentService; | ||
|
|
||
| @Operation(summary = "최신 약관 조회", description = "약관 유형별 최신 버전의 본문(마크다운)과 버전을 한 건씩 반환한다.") | ||
| @GetMapping | ||
| public ApiResponse<List<ConsentDocumentResponse>> getLatestDocuments() { | ||
| List<ConsentDocumentResponse> responses = consentService.getLatestDocuments(); | ||
| return ApiResponse.success(responses); | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/nexters/gotggam/consent/dto/ConsentDocumentResponse.java
This file contains hidden or 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,26 @@ | ||
| package com.nexters.gotggam.consent.dto; | ||
|
|
||
| import com.nexters.gotggam.consent.entity.ConsentDocument; | ||
| import com.nexters.gotggam.consent.entity.ConsentType; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
|
|
||
| public record ConsentDocumentResponse( | ||
|
|
||
| @Schema(description = "약관 유형", example = "PRIVACY_POLICY", requiredMode = Schema.RequiredMode.REQUIRED) | ||
| ConsentType type, | ||
|
|
||
| @Schema( | ||
| description = "약관 버전. 결과 제출 시 consents[].version에 그대로 전달한다.", | ||
| example = "v1.0.0", | ||
| requiredMode = Schema.RequiredMode.REQUIRED | ||
| ) | ||
| String version, | ||
|
|
||
| @Schema(description = "약관 본문 (마크다운)", example = "# 개인정보 처리방침\n...", requiredMode = Schema.RequiredMode.REQUIRED) | ||
| String content | ||
| ) { | ||
|
|
||
| public static ConsentDocumentResponse from(ConsentDocument document) { | ||
| return new ConsentDocumentResponse(document.getType(), document.getVersion(), document.getContent()); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
4 changes: 4 additions & 0 deletions
4
src/main/java/com/nexters/gotggam/consent/repository/ConsentDocumentRepository.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,9 +1,13 @@ | ||
| package com.nexters.gotggam.consent.repository; | ||
|
|
||
| import com.nexters.gotggam.consent.entity.ConsentDocument; | ||
| import com.nexters.gotggam.consent.entity.ConsentType; | ||
| import java.util.Optional; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| @Repository | ||
| public interface ConsentDocumentRepository extends JpaRepository<ConsentDocument, Long> { | ||
|
|
||
| Optional<ConsentDocument> findFirstByTypeOrderByIdDesc(ConsentType type); | ||
| } |
This file contains hidden or 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
79 changes: 79 additions & 0 deletions
79
src/test/java/com/nexters/gotggam/consent/controller/ConsentControllerTest.java
This file contains hidden or 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,79 @@ | ||
| package com.nexters.gotggam.consent.controller; | ||
|
|
||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
|
||
| import com.nexters.gotggam.TestcontainersConfiguration; | ||
| import com.nexters.gotggam.consent.entity.ConsentDocument; | ||
| import com.nexters.gotggam.consent.entity.ConsentType; | ||
| import jakarta.persistence.EntityManager; | ||
| import jakarta.persistence.PersistenceContext; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc; | ||
| import org.springframework.context.annotation.Import; | ||
| import org.springframework.test.web.servlet.MockMvc; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Import(TestcontainersConfiguration.class) | ||
| @SpringBootTest | ||
| @AutoConfigureMockMvc | ||
| @Transactional | ||
| class ConsentControllerTest { | ||
|
|
||
| @Autowired | ||
| private MockMvc mockMvc; | ||
|
|
||
| @PersistenceContext | ||
| private EntityManager em; | ||
|
|
||
| @Test | ||
| @DisplayName("약관 유형별로 나중에 넣은 버전 한 건씩을 enum 순서대로 반환한다") | ||
| void getLatestDocuments() throws Exception { | ||
| persist(document(ConsentType.TERMS_OF_SERVICE, "v1.0.0", "# 이용약관 v1.0.0")); | ||
| persist(document(ConsentType.PRIVACY_POLICY, "v1.0.0", "# 처리방침 v1.0.0")); | ||
| persist(document(ConsentType.PRIVACY_POLICY, "v1.1.0", "# 처리방침 v1.1.0")); | ||
|
|
||
| em.flush(); | ||
| em.clear(); | ||
|
|
||
| mockMvc.perform(get("/api/v1/consent-documents")) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$.data.length()").value(2)) | ||
| .andExpect(jsonPath("$.data[0].type").value("PRIVACY_POLICY")) | ||
| .andExpect(jsonPath("$.data[0].version").value("v1.1.0")) | ||
| .andExpect(jsonPath("$.data[0].content").value("# 처리방침 v1.1.0")) | ||
| .andExpect(jsonPath("$.data[1].type").value("TERMS_OF_SERVICE")) | ||
| .andExpect(jsonPath("$.data[1].version").value("v1.0.0")) | ||
| .andExpect(jsonPath("$.data[1].content").value("# 이용약관 v1.0.0")); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("약관 유형 중 하나라도 문서가 없으면 404를 반환한다") | ||
| void getLatestDocumentsWhenMissing() throws Exception { | ||
| persist(document(ConsentType.PRIVACY_POLICY, "v1.0.0", "# 처리방침 v1.0.0")); | ||
|
|
||
| em.flush(); | ||
| em.clear(); | ||
|
|
||
| mockMvc.perform(get("/api/v1/consent-documents")) | ||
| .andExpect(status().isNotFound()) | ||
| .andExpect(jsonPath("$.error.code").value("CONSENT_003")); | ||
| } | ||
|
|
||
| private <T> T persist(T entity) { | ||
| em.persist(entity); | ||
| return entity; | ||
| } | ||
|
|
||
| private ConsentDocument document(ConsentType type, String version, String content) { | ||
| return ConsentDocument.builder() | ||
| .type(type) | ||
| .version(version) | ||
| .content(content) | ||
| .build(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ConsentDocument에
isActive(또는active) boolean 컬럼을 추가한 후, 해당 컬럼을 기준으로 판별하는 방향은 어떨까요?단순히
findFirstByTypeOrderByIdDesc로 "최신 = id 최댓값"을 판단하는 방식은 과거 버전으로 롤백하거나, 수동으로 활성 버전을 지정해야 하는 케이스 등에서 대응이 꼬이는 이슈가 발생할 수 있을 것 같아요 ..!활성화 여부 컬럼 추가와 함께
를 적용하여 도입하는 방향성이 좀 더 적절하다고 생각합니다!
기현님 의견도 여쭤봅니당 ~~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
좋은 의견 같습니다!
id 방식은 롤백하려면 새 버전 행을 지워야 해서, 그 버전에 동의한 본문 근거가 사라지는 문제는 놓쳤네요ㅠㅠ
반영해뒀습니다~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
반영 감사합니다 ~!
배포 후에 dev, prod DB에
partial unique index설정해주시면 좋을 것 같아요!지금 active 교체를 DB에서 직접 하다보니, 실수로 active 문서를 2건 이상 등록하는 상황을 막기 위해 제약을 걸어둬야 할 것 같습니당
이런 수동 DDL이 앞으로도 종종 생길 것 같아서, 추후에 마이그레이션 도구 도입도 같이 얘기해보면 좋을 것 같아요 ㅎㅎ
고생하셨습니다 ~~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
좋습니다!!