Skip to content
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

feat: 상위 태그 내 존재 연도 조회 API 구현(#116) #117

Merged
merged 4 commits into from
May 20, 2024
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
7 changes: 6 additions & 1 deletion Api-Module/src/docs/asciidoc/Tag.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ operation::TagControllerTest/createChildTagTest/[snippets='http-request,path-par
[[GetParentTagTest]]
=== 상위 태그 조회 API

operation::TagControllerTest/getAllParentTagByUserRegisterTest/[snippets='http-request,request-headers,http-response,response-fields']
operation::TagControllerTest/getAllParentTagByUserTest/[snippets='http-request,request-headers,http-response,response-fields']

[[GetTopRankTagTest]]
=== 연도 내 경험 최근 추가 순 태그 조회 API
Expand All @@ -36,6 +36,11 @@ operation::TagControllerTest/getAllChildTagTest/[snippets='http-request,path-par

operation::TagControllerTest/getChildTagsByYear/[snippets='http-request,path-parameters,http-response,response-fields']

[[GetYearsByParentTagId]]
=== 상위 태그 내 존재 연도 조회 API

operation::TagControllerTest/getYearsByParentTagId/[snippets='http-request,path-parameters,http-response,response-fields']

[[duplicatedParentTagTest]]
==== 상위 태그 이름 중복 예외

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ class ExperienceGetService(
}

fun getExperienceByYearAndParentTag(year: Int, parentTagId: UUID): GetExperience.Response {
val experiences = experienceReader.readByYearAndParentTagId(year, parentTagId).map {
val experiences = experienceReader.readByYearAndTagId(year, parentTagId).map {
createExperienceDetailResponse(it)
}

return GetExperience.Response(experiences)
}

fun getExperienceByYearAndChildTag(year: Int, childTagId: UUID): GetExperience.Response {
val experiences = experienceReader.readByYearAndParentTagId(year, childTagId).map {
val experiences = experienceReader.readByYearAndTagId(year, childTagId).map {
createExperienceDetailResponse(it)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ class GetParentTag {
val strongPointCount: Int,
val experienceCount: Int
)

data class Years(
val years: List<Int>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class TagGetService(
@Transactional(readOnly = true)
fun getParentTagsByYearAndLimit(year: Int, limit: Int): GetParentTag.Response {
val currentUserId = getAuthenticationPrincipal()
val topParentTagIds = experienceReader.readByYearDesc(year, currentUserId)
val topParentTagIds = experienceReader.readByUserIDAndYearDesc(year, currentUserId)
.distinctBy { it.parentTagId }
.take(limit)
.map { it.parentTagId }
Expand All @@ -54,7 +54,7 @@ class TagGetService(
@Transactional(readOnly = true)
fun getAllParentTagsByYear(year: Int): GetParentTag.TotalTagInfo {
val currentUserId = getAuthenticationPrincipal()
val experiences = experienceReader.readByYearDesc(year, currentUserId)
val experiences = experienceReader.readByUserIDAndYearDesc(year, currentUserId)

val experienceGroup = experiences.groupBy { it.parentTagId }

Expand Down Expand Up @@ -85,7 +85,7 @@ class TagGetService(
@Transactional(readOnly = true)
fun getAllChildTagsByYearAndParentTagId(year: Int, parentTagId: UUID): GetChildTag.TotalTagInfo {
val currentUserId = getAuthenticationPrincipal()
val experiences = experienceReader.readByYearDesc(year, currentUserId)
val experiences = experienceReader.readByUserIDAndYearDesc(year, currentUserId)
val experienceGroup = experiences.groupBy { it.childTagId }

val tagSummaries = experienceGroup.map {
Expand All @@ -103,4 +103,17 @@ class TagGetService(
tagSummaries
)
}

fun getAllYearsByParentTagId(parentTagId: UUID): GetParentTag.Years {
val experiences = getAuthenticationPrincipal().let {
experienceReader.readByUserIdAndParentTagId(it, parentTagId)
}

val years = experiences
.distinctBy { it.createdAt.year }
.map { it.createdAt.year }
.sorted().reversed()

return GetParentTag.Years(years)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ object TagApi {
const val TOP_RANK_TAG_URL = "$BASE_URL/top-rank"
const val TAG_PATH_VARIABLE_URL = "$BASE_URL/{tagId}"
const val MY_CHILD_TAG_URL = "$BASE_URL/{tagId}/my"
const val ALL_YEARS = "$BASE_URL/{parentTagId}/all-years"
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class TagController(
private val tagDeleteService: TagDeleteService,
private val tagGetService: TagGetService
) {
@GetMapping(TagApi.ALL_YEARS)
fun getAllYearsByParentTag(@PathVariable("parentTagId") parentTagId: UUID): GetParentTag.Years{
return tagGetService.getAllYearsByParentTagId(parentTagId)
}

@GetMapping(TagApi.TOP_RANK_TAG_URL)
fun getTopRankTagsByLimit(
@RequestParam("year") year: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,40 @@ class TagControllerTest : BaseRestDocsTest() {
)
}

@Test
@DisplayName("상위 태그 내 존재하는 경험들의 연도를 반환한다.")
fun getYearsByParentTagId() {
//given

val yearResponse = GetParentTag.Years(
arrayListOf(2024, 2023, 2022)
)

val parentTagId = UUID.randomUUID()
given(tagController.getAllYearsByParentTag(parentTagId)).willReturn(yearResponse)

val request = RestDocumentationRequestBuilders.get(TagApi.ALL_YEARS, parentTagId)
.header("Authorization", "Bearer Access Token")

//when
val result = mockMvc.perform(request)

//then
result.andExpect(status().isOk)
.andDo(resultHandler.document(
requestHeaders(
headerWithName("Authorization").description("엑세스 토큰")
),
pathParameters(
parameterWithName("parentTagId").description("상위 태그 id")
),
responseFields(
fieldWithPath("years").description("상위 태그 리스트"),
)
)
)
}

@Test
@DisplayName("최근에 추가된 경험이 있는 순으로 상위 태그 정보를 반환한다.")
fun getTopRankParentTagTest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ interface ExperienceRepository {
fun findByUserIdAndYearDesc(year: Int, userId: UUID): List<Experience>
fun findByYearAndParentTagId(year: Int, parentTagId: UUID): List<Experience>
fun findByYearAndChildTagId(year: Int, childTagId: UUID): List<Experience>
fun findByUserIdAndParentTagId(userId: UUID, parentTagId: UUID): List<Experience>
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ class ExperienceReader(
return experienceRepository.findAllByUserId(userId)
}

fun readByYearDesc(year: Int, userId: UUID): List<Experience> {
fun readByUserIDAndYearDesc(year: Int, userId: UUID): List<Experience> {
return experienceRepository.findByUserIdAndYearDesc(year, userId)
}

fun readByYearAndParentTagId(year: Int, parentTagId: UUID) : List<Experience> {
fun readByYearAndTagId(year: Int, parentTagId: UUID) : List<Experience> {
return experienceRepository.findByYearAndParentTagId(year, parentTagId)
}

fun readByYearAndChildTagId(year: Int, childTagId: UUID): List<Experience> {
return experienceRepository.findByYearAndChildTagId(year, childTagId)
fun readByUserIdAndParentTagId(userId: UUID, parentTagId: UUID): List<Experience> {
return experienceRepository.findByUserIdAndParentTagId(userId, parentTagId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,12 @@ public List<Experience> findByYearAndChildTagId(int year, UUID childTagId) {

return experienceJpaEntities.stream().map(experienceMapper::toExperienceDomainEntity).toList();
}

@Override
public List<Experience> findByUserIdAndParentTagId(UUID userId, UUID parentTagId) {
List<ExperienceJpaEntity> experienceJpaEntities = experienceJpaRepository.findByUserIdAndParentTagId(userId,
parentTagId);

return experienceJpaEntities.stream().map(experienceMapper::toExperienceDomainEntity).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public interface ExperienceJpaRepository extends JpaRepository<ExperienceJpaEnti
List<ExperienceJpaEntity> findByUserIdAndCreatedAtBetweenOrderByCreatedAtDesc(UUID userId, LocalDateTime startYear, LocalDateTime endYear);
List<ExperienceJpaEntity> findByParentTagIdAndCreatedAtBetweenOrderByCreatedAtDesc(UUID parentTagId, LocalDateTime startYear, LocalDateTime endYear);
List<ExperienceJpaEntity> findByChildTagIdAndCreatedAtBetweenOrderByCreatedAtDesc(UUID childTagId, LocalDateTime startYear, LocalDateTime endYear);
List<ExperienceJpaEntity> findByUserIdAndParentTagId(UUID userId, UUID parentTagId);
}
Loading