Skip to content

Commit

Permalink
[Feat #40] 컨텐츠 카운트 칼럼추가 (#41)
Browse files Browse the repository at this point in the history
* fix: 오탈자 수정

* feat: 컨텐츠 카운트 칼럼 추가
  • Loading branch information
jimin3263 authored Jul 29, 2024
1 parent b39cc9d commit 3898214
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Repository

@Repository
class ContenAdapter(
class ContentAdapter(
private val contentRepository: ContentRepository
) : ContentPort {
override fun loadByUserIdAndId(userId: Long, id: Long) = contentRepository.findByUserIdAndIdAndDeleted(userId, id)
Expand All @@ -24,4 +24,8 @@ class ContenAdapter(
contentRepository.findByIdOrNull(content.id)
?.delete()
}

override fun fetchContentCountByCategoryId(categoryId: Long): Int =
contentRepository.countByCategoryId(categoryId)

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ interface ContentRepository : JpaRepository<ContentEntity, Long> {
@Param("userId") userId: Long,
@Param("id") id: Long
): ContentEntity?

fun countByCategoryId(id: Long): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import org.springframework.test.context.ContextConfiguration
@DataJpaTest(includeFilters = [ComponentScan.Filter(Repository::class)])
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ContextConfiguration(initializers = [TestContainerSupport::class])
class ContenAdapterTest(
@Autowired private val contentAdapter: ContenAdapter,
class ContentAdapterTest(
@Autowired private val contentAdapter: ContentAdapter,
@Autowired private val contentRepository: ContentRepository,
@Autowired private val userRepository: UserRepository,
@Autowired private val categoryRepository: CategoryRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.pokit.category.port.out.CategoryPort
import com.pokit.common.exception.AlreadyExistsException
import com.pokit.common.exception.InvalidRequestException
import com.pokit.common.exception.NotFoundCustomException
import com.pokit.content.port.out.ContentPort
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Slice
import org.springframework.data.domain.SliceImpl
Expand All @@ -21,7 +22,8 @@ import org.springframework.transaction.annotation.Transactional
@Transactional(readOnly = true)
class CategoryService(
private val categoryPort: CategoryPort,
private val categoryImagePort: CategoryImagePort
private val categoryImagePort: CategoryImagePort,
private val contentPort: ContentPort
) : CategoryUseCase {
companion object {
private const val MAX_CATEGORY_COUNT = 30
Expand Down Expand Up @@ -77,18 +79,22 @@ class CategoryService(
categoryPort.countByUserId(userId)

override fun getCategories(userId: Long, pageable: Pageable, filterUncategorized: Boolean): Slice<Category> {
val categories = categoryPort.loadAllByUserId(userId, pageable)
val categoriesSlice = categoryPort.loadAllByUserId(userId, pageable)

val categories = categoriesSlice.content.map { category ->
val contentCount = contentPort.fetchContentCountByCategoryId(category.categoryId)
category.copy(contentCount = contentCount)
}

val filteredCategories = if (filterUncategorized) {
categories.content.filter { it.categoryName != UNCATEGORIZED.displayName }
categories.filter { it.categoryName != UNCATEGORIZED.displayName }
} else {
categories.content
categories
}

return SliceImpl(filteredCategories, pageable, categories.hasNext())
return SliceImpl(filteredCategories, pageable, categoriesSlice.hasNext())
}


override fun getAllCategoryImages(): List<CategoryImage> =
categoryImagePort.loadAll()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ interface ContentPort {
fun persist(content: Content): Content

fun delete(content: Content)

fun fetchContentCountByCategoryId(categoryId: Long): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ data class Category(
val userId: Long,
var categoryName: String,
var categoryImage: CategoryImage,
var contentCount: Int = 0,
) {
fun update(categoryName: String, categoryImage: CategoryImage) {
this.categoryName = categoryName
Expand Down

0 comments on commit 3898214

Please sign in to comment.