-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f47b99b
commit cef8c58
Showing
22 changed files
with
307 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/com/hero/alignlab/config/database/QueryDslConfig.kt
This file contains 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,16 @@ | ||
package com.hero.alignlab.config.database | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory | ||
import jakarta.persistence.EntityManager | ||
import jakarta.persistence.PersistenceContext | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
|
||
@Configuration | ||
class QueryDslConfig { | ||
@PersistenceContext(name = "heroEntityManager") | ||
private lateinit var entityManager: EntityManager | ||
|
||
@Bean | ||
fun jpaQueryFactory(): JPAQueryFactory = JPAQueryFactory(entityManager) | ||
} |
This file contains 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 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
56 changes: 56 additions & 0 deletions
56
src/main/kotlin/com/hero/alignlab/domain/group/application/GroupTagService.kt
This file contains 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,56 @@ | ||
package com.hero.alignlab.domain.group.application | ||
|
||
import com.hero.alignlab.domain.group.domain.GroupTag | ||
import com.hero.alignlab.domain.group.domain.GroupTagMap | ||
import com.hero.alignlab.domain.group.infrastructure.GroupTagMapRepository | ||
import com.hero.alignlab.domain.group.infrastructure.GroupTagRepository | ||
import com.hero.alignlab.domain.group.model.request.CreateGroupTagRequest | ||
import com.hero.alignlab.exception.ErrorCode | ||
import com.hero.alignlab.exception.InvalidRequestException | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Service | ||
class GroupTagService( | ||
private val groupTagRepository: GroupTagRepository, | ||
private val groupTagMapRepository: GroupTagMapRepository, | ||
) { | ||
@Transactional | ||
fun saveSync(request: CreateGroupTagRequest): List<GroupTag> { | ||
return request.tagNames.map { tagName -> | ||
findOrCreateTag(tagName).also { tag -> | ||
groupTagMapRepository.save(GroupTagMap(groupId = request.groupId, tagId = tag.id)) | ||
} | ||
} | ||
} | ||
|
||
@Transactional | ||
fun deleteSyncGroupId(groupId: Long) { | ||
groupTagMapRepository.deleteByGroupId(groupId) | ||
} | ||
|
||
suspend fun findByGroupId(groupId: Long): List<GroupTag> { | ||
return withContext(Dispatchers.IO) { | ||
val tagIds = groupTagMapRepository.findByGroupId(groupId) | ||
.map { it.tagId } | ||
groupTagRepository.findByIdIn(tagIds) | ||
} | ||
} | ||
|
||
private fun findOrCreateTag(tagName: String): GroupTag { | ||
return groupTagRepository.findByName(tagName) | ||
.firstOrNull() ?: groupTagRepository.save(GroupTag(name = tagName)) | ||
} | ||
|
||
fun validateGroupTag(tagNames: List<String>) { | ||
if (tagNames.size > 3) { | ||
throw InvalidRequestException(ErrorCode.OVER_COUNT_GROUP_TAG_ERROR) | ||
} | ||
|
||
if (tagNames.size != tagNames.distinct().size) { | ||
throw InvalidRequestException(ErrorCode.DUPLICATE_GROUP_TAG_NAME_ERROR) | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/com/hero/alignlab/domain/group/domain/GroupTag.kt
This file contains 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 com.hero.alignlab.domain.group.domain | ||
|
||
import com.hero.alignlab.domain.common.domain.BaseEntity | ||
import jakarta.persistence.* | ||
|
||
@Entity | ||
@Table(name = "`group_tag`") | ||
data class GroupTag( | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
val id: Long = -1, | ||
|
||
@Column(name = "name") | ||
var name: String, | ||
) : BaseEntity() |
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/com/hero/alignlab/domain/group/domain/GroupTagMap.kt
This file contains 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 com.hero.alignlab.domain.group.domain | ||
|
||
import com.hero.alignlab.domain.common.domain.BaseEntity | ||
import jakarta.persistence.* | ||
|
||
@Entity | ||
@Table(name = "`group_tag_map`") | ||
data class GroupTagMap ( | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
val id: Long = -1, | ||
|
||
@Column(name = "group_id") | ||
val groupId: Long, | ||
|
||
@Column(name = "tag_id") | ||
val tagId: Long | ||
) : BaseEntity() |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/hero/alignlab/domain/group/infrastructure/GroupQRepository.kt
This file contains 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,9 @@ | ||
package com.hero.alignlab.domain.group.infrastructure | ||
|
||
import com.hero.alignlab.domain.group.domain.Group | ||
import org.springframework.data.domain.Page | ||
import org.springframework.data.domain.Pageable | ||
|
||
interface GroupQRepository { | ||
fun findByTagNameAndPage(tagName: String?, pageable: Pageable): Page<Group> | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/kotlin/com/hero/alignlab/domain/group/infrastructure/GroupQRepositoryImpl.kt
This file contains 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,55 @@ | ||
package com.hero.alignlab.domain.group.infrastructure | ||
|
||
import com.hero.alignlab.domain.group.domain.Group | ||
import com.hero.alignlab.domain.group.domain.QGroup.group | ||
import com.hero.alignlab.domain.group.domain.QGroupTag.groupTag | ||
import com.hero.alignlab.domain.group.domain.QGroupTagMap.groupTagMap | ||
import com.querydsl.core.types.OrderSpecifier | ||
import com.querydsl.core.types.dsl.ComparableExpressionBase | ||
import com.querydsl.jpa.impl.JPAQueryFactory | ||
import org.springframework.data.domain.Page | ||
import org.springframework.data.domain.PageImpl | ||
import org.springframework.data.domain.Pageable | ||
|
||
class GroupQRepositoryImpl( | ||
private val queryFactory: JPAQueryFactory, | ||
) : GroupQRepository { | ||
override fun findByTagNameAndPage(tagName: String?, pageable: Pageable): Page<Group> { | ||
val groups = queryFactory | ||
.selectDistinct(group) | ||
.from(group) | ||
.leftJoin(groupTagMap).on(group.id.eq(groupTagMap.groupId)) | ||
.leftJoin(groupTag).on(groupTagMap.tagId.eq(groupTag.id)) | ||
.where( | ||
tagName?.takeIf { it.isNotEmpty() }?.let { groupTag.name.contains(it) } | ||
) | ||
.offset(pageable.offset) | ||
.limit(pageable.pageSize.toLong()) | ||
.orderBy(getGroupOrderSpecifier(pageable)) | ||
.fetch() | ||
|
||
val count = queryFactory | ||
.select(group.countDistinct()) | ||
.from(group) | ||
.leftJoin(groupTagMap).on(group.id.eq(groupTagMap.groupId)) | ||
.leftJoin(groupTag).on(groupTagMap.tagId.eq(groupTag.id)) | ||
.where( | ||
tagName?.takeIf { it.isNotEmpty() }?.let { groupTag.name.contains(it) } | ||
) | ||
.orderBy(getGroupOrderSpecifier(pageable)) | ||
.fetchOne() ?: 0L | ||
|
||
return PageImpl(groups, pageable, count) | ||
} | ||
|
||
private fun getGroupOrderSpecifier(pageable: Pageable): OrderSpecifier<out Comparable<*>?>? { | ||
val order = pageable.sort.firstOrNull() ?: return null | ||
val orderSpecifier: ComparableExpressionBase<out Comparable<*>> = when (order.property) { | ||
"createdAt" -> group.createdAt | ||
"name" -> group.name | ||
"ownerUid" -> group.ownerUid | ||
else -> group.createdAt | ||
} | ||
return if (order.isDescending) orderSpecifier.desc() else orderSpecifier.asc() | ||
} | ||
} |
This file contains 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
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/com/hero/alignlab/domain/group/infrastructure/GroupTagMapRepository.kt
This file contains 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,11 @@ | ||
package com.hero.alignlab.domain.group.infrastructure | ||
|
||
import com.hero.alignlab.domain.group.domain.GroupTagMap | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
interface GroupTagMapRepository : JpaRepository<GroupTagMap, Long> { | ||
fun findByGroupId(groupId: Long): MutableList<GroupTagMap> | ||
fun deleteByGroupId(groupId: Long) | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/com/hero/alignlab/domain/group/infrastructure/GroupTagRepository.kt
This file contains 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,12 @@ | ||
package com.hero.alignlab.domain.group.infrastructure | ||
|
||
import com.hero.alignlab.domain.group.domain.GroupTag | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
interface GroupTagRepository : JpaRepository<GroupTag, Long> { | ||
fun findByName(name: String): List<GroupTag> | ||
|
||
fun findByIdIn(ids: List<Long>): List<GroupTag> | ||
} |
This file contains 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
Oops, something went wrong.