Skip to content

Commit

Permalink
Merge pull request #100 from LeeSM0518/DVK-117-edit-wish-list-view
Browse files Browse the repository at this point in the history
[DVK-117] fix: 찜 목록 조회 수정
  • Loading branch information
LeeSM0518 authored Nov 25, 2024
2 parents 2bcab44 + c4ee231 commit 364cd52
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ import com.devooks.backend.jooq.tables.references.PDF
import com.devooks.backend.jooq.tables.references.RELATED_CATEGORY
import com.devooks.backend.jooq.tables.references.REVIEW
import com.devooks.backend.jooq.tables.references.WISHLIST
import com.devooks.backend.wishlist.v1.dto.GetWishlistCommand
import java.math.BigDecimal
import java.util.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map
import org.jooq.Condition
import org.jooq.DSLContext
import org.jooq.JSON
import org.jooq.Record2
import org.jooq.Record3
import org.jooq.Table
import org.jooq.impl.DSL
import org.jooq.impl.DSL.field
import org.jooq.impl.DSL.key
Expand All @@ -41,35 +47,51 @@ class EbookQueryRepository : JooqR2dbcRepository() {
EBOOK.EBOOK_ID.eq(relatedCategorySubQuery.field("ebook_id", UUID::class.java))
)
).where(
buildConditionsToGetEbooks(command)
buildConditionsToGetEbooks(
title = command.title,
sellerMemberId = command.sellerMemberId,
ebookIdList = command.ebookIdList,
categoryIdList = command.categoryIdList
)
)
}.map { it.into(Long::class.java) }

suspend fun findBy(command: GetEbookCommand): Flow<EbookRow> =
suspend fun findWishlistBy(command: GetWishlistCommand): Flow<EbookRow> =
query {
val reviewSubQuery = getReviewSubQuery()
val mainImageSubQuery = getMainImageSubQuery()
val relatedCategorySubQuery = getRelatedCategorySubQuery()
val (reviewSubQuery, mainImageSubQuery, relatedCategorySubQuery) = getSubQueries()

select(
EBOOK.EBOOK_ID,
EBOOK.TITLE,
EBOOK.PRICE,
MEMBER.MEMBER_ID.`as`("seller_member_id"),
MEMBER.NICKNAME.`as`("seller_nickname"),
MEMBER.PROFILE_IMAGE_PATH.`as`("seller_profile_image_path"),
DSL.coalesce(reviewSubQuery.field("review_rating"), 0.0)
.`as`("review_rating"),
DSL.coalesce(reviewSubQuery.field("review_count"), 0)
.`as`("review_count"),
EBOOK.CREATED_DATE,
EBOOK.MODIFIED_DATE,
DSL.coalesce(
relatedCategorySubQuery.field("related_category_id_list"),
DSL.inline("{}")
).`as`("related_category_id_list"),
WISHLIST.WISHLIST_ID,
mainImageSubQuery.field("main_image_json_data"),
getEbookRowSelectQuery(
reviewSubQuery, relatedCategorySubQuery, mainImageSubQuery
).from(
EBOOK
.join(MEMBER).on(EBOOK.SELLING_MEMBER_ID.eq(MEMBER.MEMBER_ID))
.join(WISHLIST).on(
EBOOK.EBOOK_ID.eq(WISHLIST.EBOOK_ID).and(WISHLIST.MEMBER_ID.eq(command.memberId))
)
.leftJoin(reviewSubQuery).on(
EBOOK.EBOOK_ID.eq(reviewSubQuery.field("ebook_id", UUID::class.java))
)
.leftJoin(mainImageSubQuery).on(
EBOOK.EBOOK_ID.eq(mainImageSubQuery.field("ebook_id", UUID::class.java))
)
.leftJoin(relatedCategorySubQuery).on(
EBOOK.EBOOK_ID.eq(relatedCategorySubQuery.field("ebook_id", UUID::class.java))
)
).where(
buildConditionsToGetEbooks(categoryIdList = command.categoryIdList)
).orderBy(
WISHLIST.CREATED_DATE.desc()
).offset(command.offset).limit(command.limit)
}.map {
it.into(EbookRow::class.java)
}

suspend fun findEbooksBy(command: GetEbookCommand): Flow<EbookRow> =
query {
val (reviewSubQuery, mainImageSubQuery, relatedCategorySubQuery) = getSubQueries()

getEbookRowSelectQuery(
reviewSubQuery, relatedCategorySubQuery, mainImageSubQuery
).from(
EBOOK
.join(MEMBER).on(EBOOK.SELLING_MEMBER_ID.eq(MEMBER.MEMBER_ID))
Expand All @@ -86,7 +108,12 @@ class EbookQueryRepository : JooqR2dbcRepository() {
EBOOK.EBOOK_ID.eq(relatedCategorySubQuery.field("ebook_id", UUID::class.java))
)
).where(
buildConditionsToGetEbooks(command)
buildConditionsToGetEbooks(
title = command.title,
sellerMemberId = command.sellerMemberId,
ebookIdList = command.ebookIdList,
categoryIdList = command.categoryIdList
)
).run {
when (command.orderBy) {
EbookOrder.LATEST -> orderBy(EBOOK.CREATED_DATE.desc())
Expand All @@ -97,7 +124,7 @@ class EbookQueryRepository : JooqR2dbcRepository() {
it.into(EbookRow::class.java)
}

suspend fun findBy(command: GetDetailOfEbookCommand): EbookDetailRow? =
suspend fun findEbooksBy(command: GetDetailOfEbookCommand): EbookDetailRow? =
query {
val reviewSubQuery = getReviewSubQuery()
val mainImageSubQuery = getMainImageSubQuery()
Expand Down Expand Up @@ -157,6 +184,38 @@ class EbookQueryRepository : JooqR2dbcRepository() {
it.into(EbookDetailRow::class.java)
}.firstOrNull()

private fun DSLContext.getEbookRowSelectQuery(
reviewSubQuery: Table<Record3<UUID?, BigDecimal, Int>>,
relatedCategorySubQuery: Table<Record2<UUID?, Array<UUID?>>>,
mainImageSubQuery: Table<Record2<UUID?, JSON>>,
) = select(
EBOOK.EBOOK_ID,
EBOOK.TITLE,
EBOOK.PRICE,
MEMBER.MEMBER_ID.`as`("seller_member_id"),
MEMBER.NICKNAME.`as`("seller_nickname"),
MEMBER.PROFILE_IMAGE_PATH.`as`("seller_profile_image_path"),
DSL.coalesce(reviewSubQuery.field("review_rating"), 0.0)
.`as`("review_rating"),
DSL.coalesce(reviewSubQuery.field("review_count"), 0)
.`as`("review_count"),
EBOOK.CREATED_DATE,
EBOOK.MODIFIED_DATE,
DSL.coalesce(
relatedCategorySubQuery.field("related_category_id_list"),
DSL.inline("{}")
).`as`("related_category_id_list"),
WISHLIST.WISHLIST_ID,
mainImageSubQuery.field("main_image_json_data"),
)

private fun DSLContext.getSubQueries(): Triple<Table<Record3<UUID?, BigDecimal, Int>>, Table<Record2<UUID?, JSON>>, Table<Record2<UUID?, Array<UUID?>>>> {
val reviewSubQuery = getReviewSubQuery()
val mainImageSubQuery = getMainImageSubQuery()
val relatedCategorySubQuery = getRelatedCategorySubQuery()
return Triple(reviewSubQuery, mainImageSubQuery, relatedCategorySubQuery)
}

private fun DSLContext.getDescriptionImageSubQuery() =
select(
EBOOK_IMAGE.EBOOK_ID,
Expand Down Expand Up @@ -210,24 +269,29 @@ class EbookQueryRepository : JooqR2dbcRepository() {
EBOOK_IMAGE.IMAGE_TYPE.eq(EbookImageType.MAIN.name)
).asTable("ebook_main_image")

private fun buildConditionsToGetEbooks(command: GetEbookCommand): List<Condition> {
private fun buildConditionsToGetEbooks(
title: String? = null,
sellerMemberId: UUID? = null,
ebookIdList: List<UUID>? = null,
categoryIdList: List<UUID>? = null,
): List<Condition> {
val conditions = mutableListOf<Condition>()

conditions.add(EBOOK.DELETED_DATE.isNull)

command.title?.also {
title?.also {
conditions.add(EBOOK.TITLE.likeIgnoreCase(it))
}

command.sellerMemberId?.also {
sellerMemberId?.also {
conditions.add(EBOOK.SELLING_MEMBER_ID.eq(it))
}

command.ebookIdList?.also {
ebookIdList?.also {
conditions.add(EBOOK.EBOOK_ID.`in`(it))
}

command.categoryIdList?.also {
categoryIdList?.also {
conditions.add(
field("related_category_id_list", SQLDataType.UUID.arrayDataType)
.contains(it.toTypedArray())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ class EbookService(
}

suspend fun get(command: GetEbookCommand): Page<EbookRow> {
val ebooks = ebookQueryRepository.findBy(command)
val ebooks = ebookQueryRepository.findEbooksBy(command)
val count = ebookQueryRepository.count(command)
return PageImpl(ebooks.toList(), command.pageable, count.first())
}

suspend fun get(command: GetDetailOfEbookCommand): EbookDetailRow =
ebookQueryRepository.findBy(command)
ebookQueryRepository.findEbooksBy(command)
?: throw EbookError.NOT_FOUND_EBOOK.exception

suspend fun modify(command: ModifyEbookCommand): Ebook {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.devooks.backend.auth.v1.service.TokenService
import com.devooks.backend.common.dto.PageResponse
import com.devooks.backend.common.dto.PageResponse.Companion.toResponse
import com.devooks.backend.ebook.v1.domain.Ebook
import com.devooks.backend.ebook.v1.dto.EbookView
import com.devooks.backend.ebook.v1.dto.EbookView.Companion.toEbookView
import com.devooks.backend.ebook.v1.service.EbookService
import com.devooks.backend.wishlist.v1.domain.Wishlist
import com.devooks.backend.wishlist.v1.dto.CreateWishlistCommand
Expand All @@ -13,8 +15,6 @@ import com.devooks.backend.wishlist.v1.dto.CreateWishlistResponse
import com.devooks.backend.wishlist.v1.dto.DeleteWishlistCommand
import com.devooks.backend.wishlist.v1.dto.DeleteWishlistResponse
import com.devooks.backend.wishlist.v1.dto.GetWishlistCommand
import com.devooks.backend.wishlist.v1.dto.WishlistView
import com.devooks.backend.wishlist.v1.dto.WishlistView.Companion.toWishlistView
import com.devooks.backend.wishlist.v1.service.WishlistService
import jakarta.validation.Valid
import java.util.*
Expand Down Expand Up @@ -64,11 +64,11 @@ class WishlistController(
count: Int,
@RequestHeader(AUTHORIZATION)
authorization: String,
): PageResponse<WishlistView> {
): PageResponse<EbookView> {
val memberId = tokenService.getMemberId(Authorization(authorization))
val command = GetWishlistCommand(memberId, categoryIdList, page, count)
val wishlists = wishlistService.get(command)
return wishlists.map { it.toWishlistView() }.toResponse()
val ebooks = wishlistService.get(command)
return ebooks.map { it.toEbookView() }.toResponse()
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package com.devooks.backend.wishlist.v1.controller

import com.devooks.backend.common.dto.PageResponse
import com.devooks.backend.common.exception.ErrorResponse
import com.devooks.backend.ebook.v1.dto.EbookView
import com.devooks.backend.wishlist.v1.dto.CreateWishlistRequest
import com.devooks.backend.wishlist.v1.dto.CreateWishlistResponse
import com.devooks.backend.wishlist.v1.dto.DeleteWishlistResponse
import com.devooks.backend.wishlist.v1.dto.WishlistView
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.media.Content
import io.swagger.v3.oas.annotations.media.Schema
Expand Down Expand Up @@ -98,7 +98,7 @@ interface WishlistControllerDocs {
count: Int,
@Schema(description = "액세스 토큰", example = "Bearer \${accessToken}", required = true)
authorization: String,
): PageResponse<WishlistView>
): PageResponse<EbookView>

@Operation(summary = "찜 취소")
@ApiResponses(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.devooks.backend.common.config.database.JooqR2dbcRepository
import com.devooks.backend.jooq.tables.references.EBOOK
import com.devooks.backend.jooq.tables.references.RELATED_CATEGORY
import com.devooks.backend.jooq.tables.references.WISHLIST
import com.devooks.backend.wishlist.v1.domain.Wishlist
import com.devooks.backend.wishlist.v1.dto.GetWishlistCommand
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
Expand All @@ -14,32 +13,21 @@ import org.springframework.stereotype.Repository
@Repository
class WishlistQueryRepository : JooqR2dbcRepository() {

suspend fun findBy(command: GetWishlistCommand): Flow<Wishlist> =
query {
select(
WISHLIST.WISHLIST_ID.`as`("id"),
WISHLIST.MEMBER_ID,
WISHLIST.EBOOK_ID,
WISHLIST.CREATED_DATE,
).from(
WISHLIST
.join(EBOOK)
.on(EBOOK.EBOOK_ID.eq(WISHLIST.EBOOK_ID))
.join(RELATED_CATEGORY)
.on(RELATED_CATEGORY.RELATED_CATEGORY_ID.eq(RELATED_CATEGORY.RELATED_CATEGORY_ID))
).where(
WISHLIST.MEMBER_ID.eq(command.memberId)
).offset(command.offset).limit(command.limit)
}.map { it.into(Wishlist::class.java) }

suspend fun countBy(command: GetWishlistCommand): Flow<Long> =
query {
select(
DSL.count()
).from(
WISHLIST
.join(EBOOK).on(WISHLIST.EBOOK_ID.eq(EBOOK.EBOOK_ID))
.join(RELATED_CATEGORY).on(EBOOK.EBOOK_ID.eq(RELATED_CATEGORY.EBOOK_ID))
).where(
WISHLIST.MEMBER_ID.eq(command.memberId)
WISHLIST.MEMBER_ID.eq(command.memberId).and(EBOOK.DELETED_DATE.isNull)
.let { where ->
command.categoryIdList?.let { categoryIdList ->
where.and(RELATED_CATEGORY.CATEGORY_ID.`in`(categoryIdList))
} ?: where
}
)
}.map { it.into(Long::class.java) }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.devooks.backend.wishlist.v1.service

import com.devooks.backend.ebook.v1.domain.Ebook
import com.devooks.backend.ebook.v1.repository.EbookQueryRepository
import com.devooks.backend.ebook.v1.repository.row.EbookRow
import com.devooks.backend.wishlist.v1.domain.Wishlist
import com.devooks.backend.wishlist.v1.dto.CreateWishlistCommand
import com.devooks.backend.wishlist.v1.dto.DeleteWishlistCommand
Expand All @@ -19,6 +21,7 @@ import org.springframework.stereotype.Service
class WishlistService(
private val wishlistCrudRepository: WishlistCrudRepository,
private val wishlistQueryRepository: WishlistQueryRepository,
private val ebookQueryRepository: EbookQueryRepository,
) {
suspend fun create(command: CreateWishlistCommand, ebook: Ebook): Wishlist {
wishlistCrudRepository
Expand All @@ -28,8 +31,8 @@ class WishlistService(
return wishlistCrudRepository.save(wishlistEntity).toDomain()
}

suspend fun get(command: GetWishlistCommand): Page<Wishlist> {
val wishlists = wishlistQueryRepository.findBy(command)
suspend fun get(command: GetWishlistCommand): Page<EbookRow> {
val wishlists = ebookQueryRepository.findWishlistBy(command)
val count = wishlistQueryRepository.countBy(command)
return PageImpl(wishlists.toList(), command.pageable, count.first())
}
Expand Down
Loading

0 comments on commit 364cd52

Please sign in to comment.