-
Notifications
You must be signed in to change notification settings - Fork 1
feat: 사용자별 감정 태그 통계 조회 기능 구현 #70
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ff940c0
[BOOK-204] feat: apis - 씨앗 통계 조회 API 구현
move-hoon dd0eb2a
[BOOK-204] chore: apis - BookUseCase 클래스에서 불필요한 주석 제거 및 코드 정리
move-hoon 9b16c0a
[BOOK-204] chore: apis - ReadingRecordUseCase에서 사용하지 않는 import문 제거 및 …
move-hoon 8f3b55d
[BOOK-204] feat: apis - SeedUseCase 클래스 추가 및 씨앗 통계 조회 기능 구현
move-hoon 0ab3e8f
[BOOK-204] feat: domain, apis - SeedService 및 ReadingRecordTagDomainS…
move-hoon c1f4c1d
[BOOK-204] feat: domain, infra - ReadingRecordTagRepository에 태그 통계 기능…
move-hoon fffcddd
[BOOK-204] feat: global-utils - 일반 감정 태그 카테고리 열거형 추가
move-hoon a68002e
[BOOK-204] feat: apis - 씨앗 통계 응답 DTO 추가
move-hoon 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
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
23 changes: 23 additions & 0 deletions
23
apis/src/main/kotlin/org/yapp/apis/seed/controller/SeedController.kt
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,23 @@ | ||
| package org.yapp.apis.seed.controller | ||
|
|
||
| import org.springframework.http.ResponseEntity | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal | ||
| import org.springframework.web.bind.annotation.RequestMapping | ||
| import org.springframework.web.bind.annotation.RestController | ||
| import org.yapp.apis.seed.dto.response.SeedStatsResponse | ||
| import org.yapp.apis.seed.usecase.SeedUseCase | ||
| import java.util.* | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/v1/seeds") | ||
| class SeedController( | ||
| private val seedUseCase: SeedUseCase | ||
| ) : SeedControllerApi { | ||
|
|
||
| override fun getSeedStats( | ||
| @AuthenticationPrincipal userId: UUID | ||
| ): ResponseEntity<SeedStatsResponse> { | ||
| val stats = seedUseCase.getSeedStats(userId) | ||
| return ResponseEntity.ok(stats) | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
apis/src/main/kotlin/org/yapp/apis/seed/controller/SeedControllerApi.kt
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,41 @@ | ||
| package org.yapp.apis.seed.controller | ||
|
|
||
| import io.swagger.v3.oas.annotations.Operation | ||
| import io.swagger.v3.oas.annotations.media.Content | ||
| import io.swagger.v3.oas.annotations.media.Schema | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponse | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponses | ||
| import io.swagger.v3.oas.annotations.tags.Tag | ||
| import org.springframework.http.ResponseEntity | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal | ||
| import org.springframework.web.bind.annotation.GetMapping | ||
| import org.yapp.apis.seed.dto.response.SeedStatsResponse | ||
| import org.yapp.globalutils.exception.ErrorResponse | ||
| import java.util.* | ||
|
|
||
| @Tag(name = "Seed", description = "씨앗(감정 태그) 관련 API") | ||
| interface SeedControllerApi { | ||
|
|
||
| @Operation( | ||
| summary = "씨앗 통계 조회", | ||
| description = "사용자가 모은 감정 태그별 씨앗 개수를 조회합니다." | ||
| ) | ||
| @ApiResponses( | ||
| value = [ | ||
| ApiResponse( | ||
| responseCode = "200", | ||
| description = "씨앗 통계 조회 성공", | ||
| content = [Content(schema = Schema(implementation = SeedStatsResponse::class))] | ||
| ), | ||
| ApiResponse( | ||
| responseCode = "404", | ||
| description = "사용자를 찾을 수 없음", | ||
| content = [Content(schema = Schema(implementation = ErrorResponse::class))] | ||
| ) | ||
| ] | ||
| ) | ||
| @GetMapping("/stats") | ||
| fun getSeedStats( | ||
| @AuthenticationPrincipal userId: UUID | ||
| ): ResponseEntity<SeedStatsResponse> | ||
| } |
31 changes: 31 additions & 0 deletions
31
apis/src/main/kotlin/org/yapp/apis/seed/dto/response/SeedStatsResponse.kt
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,31 @@ | ||
| package org.yapp.apis.seed.dto.response | ||
|
|
||
| import org.yapp.domain.readingrecordtag.vo.TagStatsVO | ||
| import org.yapp.globalutils.tag.GeneralEmotionTagCategory | ||
|
|
||
| data class SeedStatsResponse private constructor( | ||
| val categories: List<SeedCategoryStats> | ||
| ) { | ||
| data class SeedCategoryStats private constructor( | ||
| val name: String, | ||
| val count: Int | ||
| ) { | ||
| companion object { | ||
| fun of(name: String, count: Int): SeedCategoryStats { | ||
| return SeedCategoryStats(name, count) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| fun from(tagStatsVO: TagStatsVO): SeedStatsResponse { | ||
| val categories = GeneralEmotionTagCategory.entries.map { category -> | ||
| SeedCategoryStats.of( | ||
| name = category.displayName, | ||
| count = tagStatsVO.categoryStats.getOrDefault(category, 0) | ||
| ) | ||
| } | ||
| return SeedStatsResponse(categories) | ||
| } | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
apis/src/main/kotlin/org/yapp/apis/seed/service/SeedService.kt
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,21 @@ | ||
| package org.yapp.apis.seed.service | ||
|
|
||
| import org.springframework.stereotype.Service | ||
| import org.yapp.apis.seed.dto.response.SeedStatsResponse | ||
| import org.yapp.domain.readingrecordtag.ReadingRecordTagDomainService | ||
| import org.yapp.globalutils.tag.GeneralEmotionTagCategory | ||
| import java.util.* | ||
|
|
||
| @Service | ||
| class SeedService( | ||
| private val readingRecordTagDomainService: ReadingRecordTagDomainService | ||
| ) { | ||
| fun getSeedStatsByUserId(userId: UUID): SeedStatsResponse { | ||
| val tagStatsVO = readingRecordTagDomainService.countTagsByUserIdAndCategories( | ||
| userId = userId, | ||
| categories = GeneralEmotionTagCategory.entries.map { it.displayName } | ||
| ) | ||
|
|
||
| return SeedStatsResponse.from(tagStatsVO) | ||
| } | ||
move-hoon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
20 changes: 20 additions & 0 deletions
20
apis/src/main/kotlin/org/yapp/apis/seed/usecase/SeedUseCase.kt
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,20 @@ | ||
| package org.yapp.apis.seed.usecase | ||
|
|
||
| import org.springframework.transaction.annotation.Transactional | ||
| import org.yapp.apis.auth.service.UserAuthService | ||
| import org.yapp.apis.seed.dto.response.SeedStatsResponse | ||
| import org.yapp.apis.seed.service.SeedService | ||
| import org.yapp.globalutils.annotation.UseCase | ||
| import java.util.* | ||
|
|
||
| @UseCase | ||
| @Transactional(readOnly = true) | ||
| class SeedUseCase( | ||
| private val userAuthService: UserAuthService, | ||
| private val seedService: SeedService | ||
| ) { | ||
| fun getSeedStats(userId: UUID): SeedStatsResponse { | ||
| userAuthService.validateUserExists(userId) | ||
| return seedService.getSeedStatsByUserId(userId) | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
domain/src/main/kotlin/org/yapp/domain/readingrecordtag/ReadingRecordTagDomainService.kt
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,15 @@ | ||
| package org.yapp.domain.readingrecordtag | ||
|
|
||
| import org.yapp.domain.readingrecordtag.vo.TagStatsVO | ||
| import org.yapp.globalutils.annotation.DomainService | ||
| import java.util.* | ||
|
|
||
| @DomainService | ||
| class ReadingRecordTagDomainService( | ||
| private val readingRecordTagRepository: ReadingRecordTagRepository | ||
| ) { | ||
| fun countTagsByUserIdAndCategories(userId: UUID, categories: List<String>): TagStatsVO { | ||
| val categoryStats = readingRecordTagRepository.countTagsByUserIdAndCategories(userId, categories) | ||
| return TagStatsVO.newInstance(categoryStats) | ||
| } | ||
| } |
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
28 changes: 28 additions & 0 deletions
28
domain/src/main/kotlin/org/yapp/domain/readingrecordtag/vo/TagStatsVO.kt
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 org.yapp.domain.readingrecordtag.vo | ||
|
|
||
| import org.yapp.globalutils.tag.GeneralEmotionTagCategory | ||
| import java.util.EnumMap | ||
|
|
||
| data class TagStatsVO private constructor( | ||
| val categoryStats: EnumMap<GeneralEmotionTagCategory, Int> | ||
| ) { | ||
| init { | ||
| categoryStats.values.forEach { count -> | ||
| require(count >= 0) { "태그 개수는 0 이상이어야 합니다." } | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| fun newInstance(categoryStats: Map<String, Int>): TagStatsVO { | ||
| val enumStats = EnumMap<GeneralEmotionTagCategory, Int>(GeneralEmotionTagCategory::class.java) | ||
|
|
||
| categoryStats.forEach { (displayName, count) -> | ||
| GeneralEmotionTagCategory.fromDisplayName(displayName)?.let { enumCategory -> | ||
| enumStats[enumCategory] = count | ||
| } | ||
| } | ||
move-hoon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return TagStatsVO(enumStats) | ||
| } | ||
| } | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
global-utils/src/main/kotlin/org/yapp/globalutils/tag/GeneralEmotionTagCategory.kt
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,18 @@ | ||
| package org.yapp.globalutils.tag | ||
|
|
||
| enum class GeneralEmotionTagCategory( | ||
| val displayName: String | ||
| ) { | ||
| WARMTH("따뜻함"), | ||
| JOY("즐거움"), | ||
| TENSION("긴장감"), | ||
| SADNESS("슬픔"); | ||
|
|
||
| companion object { | ||
| private val BY_DISPLAY_NAME = entries.associateBy { it.displayName } | ||
|
|
||
| fun fromDisplayName(displayName: String): GeneralEmotionTagCategory? { | ||
| return BY_DISPLAY_NAME[displayName] | ||
| } | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
...otlin/org/yapp/infra/readingrecordtag/repository/JpaReadingRecordTagQuerydslRepository.kt
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,7 @@ | ||
| package org.yapp.infra.readingrecordtag.repository | ||
|
|
||
| import java.util.* | ||
|
|
||
| interface JpaReadingRecordTagQuerydslRepository { | ||
| fun countTagsByUserIdAndCategories(userId: UUID, categories: List<String>): Map<String, Int> | ||
| } | ||
move-hoon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
68 changes: 68 additions & 0 deletions
68
.../yapp/infra/readingrecordtag/repository/impl/JpaReadingRecordTagQuerydslRepositoryImpl.kt
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,68 @@ | ||
| package org.yapp.infra.readingrecordtag.repository.impl | ||
|
|
||
| import com.querydsl.core.types.dsl.BooleanExpression | ||
| import com.querydsl.jpa.impl.JPAQueryFactory | ||
| import org.springframework.stereotype.Repository | ||
| import org.yapp.infra.readingrecord.entity.QReadingRecordEntity | ||
| import org.yapp.infra.readingrecordtag.entity.QReadingRecordTagEntity | ||
| import org.yapp.infra.readingrecordtag.repository.JpaReadingRecordTagQuerydslRepository | ||
| import org.yapp.infra.tag.entity.QTagEntity | ||
| import org.yapp.infra.userbook.entity.QUserBookEntity | ||
| import java.util.* | ||
|
|
||
| @Repository | ||
| class JpaReadingRecordTagQuerydslRepositoryImpl( | ||
| private val queryFactory: JPAQueryFactory | ||
| ) : JpaReadingRecordTagQuerydslRepository { | ||
|
|
||
| private val readingRecordTag = QReadingRecordTagEntity.readingRecordTagEntity | ||
| private val readingRecord = QReadingRecordEntity.readingRecordEntity | ||
| private val userBook = QUserBookEntity.userBookEntity | ||
| private val tag = QTagEntity.tagEntity | ||
|
|
||
| override fun countTagsByUserIdAndCategories( | ||
| userId: UUID, | ||
| categories: List<String> | ||
| ): Map<String, Int> { | ||
| if (categories.isEmpty()) { | ||
| return emptyMap() | ||
| } | ||
|
|
||
| val results = queryFactory | ||
| .select(tag.name, readingRecordTag.count()) | ||
| .from(readingRecordTag) | ||
| .join(readingRecord).on(readingRecordTag.readingRecordId.eq(readingRecord.id)) | ||
| .join(userBook).on(readingRecord.userBookId.eq(userBook.id)) | ||
| .join(tag).on(readingRecordTag.tagId.eq(tag.id)) | ||
| .where( | ||
| userBook.userIdEq(userId), | ||
| readingRecord.isNotDeleted(), | ||
| readingRecordTag.isNotDeleted(), | ||
| tag.nameIn(categories) | ||
| ) | ||
| .groupBy(tag.name) | ||
| .fetch() | ||
|
|
||
| return results.associate { tuple -> | ||
| val tagName = tuple[tag.name] ?: "" | ||
| val count = tuple[readingRecordTag.count()]?.toInt() ?: 0 | ||
| tagName to count | ||
| } | ||
| } | ||
|
|
||
| private fun QUserBookEntity.userIdEq(userId: UUID): BooleanExpression { | ||
| return this.userId.eq(userId) | ||
| } | ||
|
|
||
| private fun QReadingRecordEntity.isNotDeleted(): BooleanExpression { | ||
| return this.deletedAt.isNull | ||
| } | ||
|
|
||
| private fun QReadingRecordTagEntity.isNotDeleted(): BooleanExpression { | ||
| return this.deletedAt.isNull | ||
| } | ||
|
|
||
| private fun QTagEntity.nameIn(categories: List<String>): BooleanExpression { | ||
| return this.name.`in`(categories) | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.