Skip to content

Commit

Permalink
refactor: 그룹 태그 기능 추가 코드 리뷰 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
leeheefull committed Nov 17, 2024
1 parent 973d69a commit 5ba390e
Show file tree
Hide file tree
Showing 28 changed files with 80 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fun NumberPath<Long>.isEquals(parameter: Long?): BooleanExpression? {
}

fun StringPath.isContains(parameter: String?): BooleanExpression? {
return parameter?.let { param -> this.contains(param) }
return parameter?.takeIf { param -> param.isNotEmpty() }?.let { param -> this.contains(param) }
}

fun NumberPath<Long>.isIn(parameters: Set<Long>?): BooleanExpression? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import jakarta.persistence.*
class Discussion(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = -1L,
val id: Long = 0L,

@Column(name = "uid")
val uid: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import jakarta.persistence.*
class DiscussionComment(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = -1L,
val id: Long = 0L,

@Column(name = "uid")
val uid: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class GroupFacade(

val updatedGroupTag = if (!request.tagNames.isNullOrEmpty()) {
groupTagService.validateGroupTag(request.tagNames)
groupTagService.deleteSyncGroupId(groupId)
groupTagService.deleteGroupTagMapSyncByGroupId(groupId)
groupTagService.saveSync(CreateGroupTagRequest(groupId, request.tagNames))
} else emptyList()

Expand Down Expand Up @@ -118,7 +118,7 @@ class GroupFacade(

txTemplates.writer.executes {
groupUserScoreService.deleteAllByUid(uid)
groupTagService.deleteSyncGroupId(groupId)
groupTagService.deleteGroupTagMapSyncByGroupId(groupId)
}
}

Expand Down Expand Up @@ -214,12 +214,12 @@ class GroupFacade(
.filterNot { groupUserScore -> groupUserScore.score == null }
.sortedBy { groupUserScore -> groupUserScore.score }
.take(5)
}
) { group, groupUserScore ->
},
{ groupTagService.findByGroupId(groupId) },
) { group, groupUserScore, tags ->
val ownerGroupUser = userInfoService.getUserByIdOrThrow(group.ownerUid)
val tags = groupTagService.findByGroupId(groupId)

GetGroupResponse.from(group, ownerGroupUser.nickname, tags).run {
GetGroupResponse.of(group, tags, ownerGroupUser.nickname).run {
when (group.ownerUid == user.uid) {
true -> this
false -> this.copy(joinCode = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,33 @@ class GroupTagService(
) {
@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))
}
}
val existsTags = groupTagRepository.findAllByNameIn(request.tagNames)
val existsTagNames = existsTags.map { tag -> tag.name }

val needToCreateTags = request.tagNames
.filterNot { tagName -> existsTagNames.contains(tagName) }
.map { tagName -> GroupTag(name = tagName) }

val createdTags = groupTagRepository.saveAll(needToCreateTags)

(createdTags + existsTags)
.map { tag -> GroupTagMap(groupId = request.groupId, tagId = tag.id) }
.run { groupTagMapRepository.saveAll(this) }

return (createdTags + existsTags)
}

@Transactional
fun deleteSyncGroupId(groupId: Long) {
groupTagMapRepository.deleteByGroupId(groupId)
fun deleteGroupTagMapSyncByGroupId(groupId: Long) {
groupTagMapRepository.deleteAllByGroupId(groupId)
}

suspend fun findByGroupId(groupId: Long): List<GroupTag> {
return withContext(Dispatchers.IO) {
val tagIds = groupTagMapRepository.findByGroupId(groupId)
.map { it.tagId }
groupTagRepository.findByIdIn(tagIds)
groupTagRepository.findByGroupId(groupId)
}
}

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import jakarta.persistence.*
data class Group(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = -1,
val id: Long = 0L,

@Column(name = "name")
var name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import jakarta.persistence.*
data class GroupTag(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = -1,
val id: Long = 0L,

@Column(name = "name")
var name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import jakarta.persistence.*
data class GroupTagMap (
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = -1,
val id: Long = 0L,

@Column(name = "group_id")
val groupId: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import jakarta.persistence.*
data class GroupUser(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = -1,
val id: Long = 0L,

@Column(name = "group_id")
val groupId: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import jakarta.persistence.*
class GroupUserScore(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = -1,
val id: Long = 0L,

@Column(name = "group_id")
val groupId: Long,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hero.alignlab.domain.group.infrastructure

import com.hero.alignlab.common.extension.isContains
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
Expand All @@ -20,9 +21,7 @@ class GroupQRepositoryImpl(
.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) }
)
.where(groupTag.name.isContains(tagName))
.offset(pageable.offset)
.limit(pageable.pageSize.toLong())
.orderBy(getGroupOrderSpecifier(pageable))
Expand All @@ -33,9 +32,7 @@ class GroupQRepositoryImpl(
.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) }
)
.where(groupTag.name.isContains(tagName))
.orderBy(getGroupOrderSpecifier(pageable))
.fetchOne() ?: 0L

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ 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
import org.springframework.transaction.annotation.Transactional

@Transactional(readOnly = true)
@Repository
interface GroupTagMapRepository : JpaRepository<GroupTagMap, Long> {
fun findByGroupId(groupId: Long): MutableList<GroupTagMap>
fun deleteByGroupId(groupId: Long)
fun deleteAllByGroupId(groupId: Long)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.hero.alignlab.domain.group.infrastructure

import com.hero.alignlab.domain.group.domain.GroupTag

interface GroupTagQRepository {
fun findByGroupId(groupId: Long): List<GroupTag>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.hero.alignlab.domain.group.infrastructure

import com.hero.alignlab.domain.group.domain.GroupTag
import com.hero.alignlab.domain.group.domain.QGroupTag.groupTag
import com.hero.alignlab.domain.group.domain.QGroupTagMap.groupTagMap
import com.querydsl.jpa.impl.JPAQueryFactory

class GroupTagQRepositoryImpl(
private val queryFactory: JPAQueryFactory,
) : GroupTagQRepository {
override fun findByGroupId(groupId: Long): List<GroupTag> {
return queryFactory.selectFrom(groupTag)
.join(groupTagMap).on(groupTag.id.eq(groupTagMap.tagId))
.where(groupTagMap.groupId.eq(groupId))
.fetch()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ 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
import org.springframework.transaction.annotation.Transactional

@Transactional(readOnly = true)
@Repository
interface GroupTagRepository : JpaRepository<GroupTag, Long> {
fun findByName(name: String): List<GroupTag>

fun findByIdIn(ids: List<Long>): List<GroupTag>
interface GroupTagRepository : JpaRepository<GroupTag, Long>, GroupTagQRepository {
fun findAllByNameIn(names: List<String>): Set<GroupTag>
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ data class GetGroupResponse(
val tags: List<GroupTagResponse>?,
) {
companion object {
fun from(group: Group, ownerName: String, tags: List<GroupTag>): GetGroupResponse {
fun of(group: Group, tags: List<GroupTag>?, ownerName: String): GetGroupResponse {
return GetGroupResponse(
id = group.id,
name = group.name,
Expand All @@ -33,7 +33,7 @@ data class GetGroupResponse(
joinCode = group.joinCode,
userCount = group.userCount,
userCapacity = group.userCapacity,
tags = tags.map { GroupTagResponse(it.id, it.name) },
tags = tags?.map { GroupTagResponse(it.id, it.name) },
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.hero.alignlab.common.model.PageResponse
import com.hero.alignlab.common.model.Response
import com.hero.alignlab.domain.auth.model.AuthUser
import com.hero.alignlab.domain.group.application.GroupFacade
import com.hero.alignlab.domain.group.application.GroupService
import com.hero.alignlab.domain.group.model.request.CheckGroupRegisterRequest
import com.hero.alignlab.domain.group.model.request.CreateGroupRequest
import com.hero.alignlab.domain.group.model.request.UpdateGroupRequest
Expand All @@ -25,7 +24,6 @@ import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping(produces = [MediaType.APPLICATION_JSON_VALUE])
class GroupResource(
private val groupService: GroupService,
private val groupFacade: GroupFacade,
) {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import jakarta.persistence.*
class ImageMetadata(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = -1,
val id: Long = 0L,

@Column(name = "uid")
val uid: Long = -1,
val uid: Long = 0L,

@Column(name = "filename")
val filename: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import jakarta.persistence.*
class SystemActionLog(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = -1L,
val id: Long = 0L,

@Column(name = "uid")
val uid: Long? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import jakarta.persistence.*
class PoseNotification(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = -1,
val id: Long = 0L,

@Column(name = "uid")
val uid: Long = -1,
val uid: Long = 0L,

@Column(name = "is_active")
var isActive: Boolean = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class PoseCount(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
val id: Long = -1,
val id: Long = 0L,

@Column(name = "uid")
val uid: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class PoseKeyPointSnapshot(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
val id: Long = -1L,
val id: Long = 0L,

@Column(name = "pose_snapshot_id")
val poseSnapshotId: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PoseLayout(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
val id: Long = -1,
val id: Long = 0L,

@Column(name = "uid")
val uid: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class PoseLayoutPoint(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
val id: Long = -1,
val id: Long = 0L,

@Column(name = "pose_layout_id")
val poseLayoutId: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data class PoseSnapshot(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
val id: Long = -1,
val id: Long = 0L,

@Column(name = "uid")
val uid: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import jakarta.persistence.*
class CredentialUserInfo(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = -1,
val id: Long = 0L,

@Column(name = "uid")
val uid: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import jakarta.persistence.*
class OAuthUserInfo(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = -1,
val id: Long = 0L,

@Column(name = "uid")
val uid: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import jakarta.persistence.*
class UserInfo(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = -1,
val id: Long = 0L,

@Column(name = "nickname")
var nickname: String,
Expand Down

0 comments on commit 5ba390e

Please sign in to comment.