Skip to content

Commit

Permalink
feat: 포킷 복제 api (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimin3263 authored Aug 19, 2024
1 parent 132626e commit f3d4b9e
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pokit.category

import com.pokit.auth.model.PrincipalUser
import com.pokit.category.dto.request.DuplicateCategoryRequest
import com.pokit.category.dto.response.SharedContentsResponse
import com.pokit.category.port.`in`.CategoryUseCase
import com.pokit.common.wrapper.ResponseWrapper.wrapOk
Expand Down Expand Up @@ -48,4 +49,13 @@ class CategoryShareController(
.wrapOk()
}

@Operation(summary = "포킷 복제 API")
@PostMapping
fun duplicateCategory(
@AuthenticationPrincipal user: PrincipalUser,
@RequestBody request: DuplicateCategoryRequest,
): ResponseEntity<Unit> =
categoryUseCase.duplicateCategory(request.originCategoryId, request.categoryName, user.id)
.wrapOk()

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.pokit.category.dto.request

import jakarta.validation.constraints.Size

data class DuplicateCategoryRequest (
val originCategoryId: Long,
@field:Size(min = 1, max = 10, message = "최대 10자까지 입력 가능합니다.")
val categoryName: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,38 @@ class ContentAdapter(
return SliceImpl(contentResults, pageable, hasNext)
}

override fun duplicateContent(originCategoryId: Long, targetCategoryId: Long) {
val contents = loadByCategoryIdAndOpenType(originCategoryId, OpenType.PUBLIC)

val targetContentEntities = contents.map {
ContentEntity.from(it, targetCategoryId)
}

contentRepository.bulkInsert(targetContentEntities)
}

override fun loadByContentIds(contentIds: List<Long>): List<Content> =
contentRepository.findByIdIn(contentIds)
.map { it.toDomain() }

private fun loadByCategoryIdAndOpenType(categoryId: Long, opentype: OpenType): List<Content> {
val contentEntities = queryFactory.select(contentEntity)
.from(contentEntity)
.join(categoryEntity).on(categoryEntity.id.eq(contentEntity.categoryId))
.where(
categoryEntity.id.eq(categoryId),
categoryEntity.openType.eq(opentype),
contentEntity.deleted.isFalse,
)
.fetch()

val contents = contentEntities.map {
it.toDomain()
}

return contents
}

private fun <T> getHasNext(
items: MutableList<T>,
pageable: Pageable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ class ContentEntity(
domain = content.domain,
thumbNail = content.thumbNail
)

fun from(content: Content, categoryId: Long) = ContentEntity(
id = content.id,
categoryId = categoryId,
type = content.type,
data = content.data,
title = content.title,
memo = content.memo,
alertYn = "NO",
domain = content.domain,
thumbNail = content.thumbNail
)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.pokit.out.persistence.content.persist

interface ContentJdbcRepository {
fun bulkInsert(contentEntity: List<ContentEntity>)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.pokit.out.persistence.content.persist

import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.stereotype.Repository

@Repository
class ContentJdbcRepositoryImpl(
private val jdbcTemplate: JdbcTemplate
) : ContentJdbcRepository {
override fun bulkInsert(contentEntities: List<ContentEntity>) {
val sql = """
INSERT INTO content (
category_id, type, data, title, memo, alert_yn, domain, is_deleted, thumb_nail, created_at, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
""".trimIndent()

val batchArgs = contentEntities.map { content ->
arrayOf(
content.categoryId,
content.type.name,
content.data,
content.title,
content.memo,
content.alertYn,
content.domain,
content.deleted,
content.thumbNail
)
}

jdbcTemplate.batchUpdate(sql, batchArgs)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param

interface ContentRepository : JpaRepository<ContentEntity, Long> {
interface ContentRepository : JpaRepository<ContentEntity, Long>, ContentJdbcRepository {
@Query(
"""
select co from ContentEntity co
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ interface CategoryUseCase {
fun getCategory(userId: Long, categoryId: Long): Category
fun getSharedCategory(categoryId: Long, userId: Long): Category
fun completeShare(categoryId: Long, userId: Long)
fun duplicateCategory(originCategoryId: Long, categoryName: String, userId: Long)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.pokit.category.model.Category
import com.pokit.category.model.CategoryImage
import com.pokit.category.model.CategoryStatus.UNCATEGORIZED
import com.pokit.category.model.OpenType
import com.pokit.category.model.duplicate
import com.pokit.category.port.`in`.CategoryUseCase
import com.pokit.category.port.out.CategoryImagePort
import com.pokit.category.port.out.CategoryPort
Expand Down Expand Up @@ -126,6 +127,27 @@ class CategoryService(
categoryPort.persist(category)
}

@Transactional
override fun duplicateCategory(originCategoryId: Long, categoryName: String, userId: Long) {
val originCategory = categoryPort.loadByIdAndOpenType(originCategoryId, OpenType.PUBLIC)
?: throw NotFoundCustomException(CategoryErrorCode.NOT_FOUND_CATEGORY)

if (originCategory.userId == userId) {
throw InvalidRequestException(CategoryErrorCode.SHARE_ALREADY_EXISTS_CATEGORY)
}

if (categoryPort.countByUserId(userId) >= MAX_CATEGORY_COUNT) {
throw InvalidRequestException(CategoryErrorCode.SHARE_MAX_CATEGORY_LIMIT_EXCEEDED)
}

if (categoryPort.existsByNameAndUserId(originCategory.categoryName, userId)) {
throw AlreadyExistsException(CategoryErrorCode.SHARE_ALREADY_EXISTS_CATEGORY_NAME)
}

val newCategory = categoryPort.persist(originCategory.duplicate(categoryName, userId))
contentPort.duplicateContent(originCategoryId, newCategory.categoryId)
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ interface ContentPort {
opentype: OpenType,
pageable: Pageable
): Slice<SharedContentResult>

fun duplicateContent(originCategoryId: Long, targetCategoryId: Long)
}
8 changes: 8 additions & 0 deletions domain/src/main/kotlin/com/pokit/category/model/Category.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ fun Category.toRemindCategory() = RemindCategory(
categoryId = this.categoryId,
categoryName = this.categoryName,
)

fun Category.duplicate(newCategoryName: String, userId: Long): Category {
return this.copy(
categoryId = 0L,
userId = userId,
categoryName = newCategoryName
)
}

0 comments on commit f3d4b9e

Please sign in to comment.