-
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
44bddd3
commit 4927817
Showing
17 changed files
with
289 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
-- 응원하기 | ||
CREATE TABLE `cheer_up` | ||
( | ||
`id` bigint NOT NULL AUTO_INCREMENT COMMENT 'cheer up id', | ||
`uid` bigint NOT NULL COMMENT 'uid', | ||
`target_uid` bigint NOT NULL COMMENT 'target_uid', | ||
`cheered_at` date NOT NULL COMMENT '응원한 날짜', | ||
`created_at` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '생성일', | ||
`modified_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '수정일', | ||
PRIMARY KEY (`id`) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='응원하기'; | ||
CREATE UNIQUE INDEX uidx__uid__target_uid__cheered_at ON cheer_up (uid, target_uid, cheered_at); | ||
CREATE INDEX idx__target ON cheer_up (target_uid); | ||
CREATE INDEX idx__cheered_at__uid ON cheer_up (cheered_at, uid); |
66 changes: 66 additions & 0 deletions
66
src/main/kotlin/com/hero/alignlab/domain/cheer/application/CheerUpFacade.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,66 @@ | ||
package com.hero.alignlab.domain.cheer.application | ||
|
||
import com.hero.alignlab.domain.auth.model.AuthUser | ||
import com.hero.alignlab.domain.cheer.domain.CheerUp | ||
import com.hero.alignlab.domain.cheer.model.request.CheerUpRequest | ||
import com.hero.alignlab.domain.cheer.model.response.CheerUpResponse | ||
import com.hero.alignlab.domain.group.application.GroupUserService | ||
import com.hero.alignlab.ws.handler.ReactiveGroupUserWebSocketHandler | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.springframework.stereotype.Service | ||
import java.time.LocalDate | ||
|
||
@Service | ||
class CheerUpFacade( | ||
private val groupUserService: GroupUserService, | ||
private val cheerUpService: CheerUpService, | ||
private val reactiveGroupUserWebSocketHandler: ReactiveGroupUserWebSocketHandler, | ||
) { | ||
private val logger = KotlinLogging.logger { } | ||
|
||
suspend fun cheerUp(user: AuthUser, request: CheerUpRequest): CheerUpResponse { | ||
val now = LocalDate.now() | ||
|
||
/** 동일 그룹 유저인지 확인하기 */ | ||
val groupUser = groupUserService.findByUidOrThrow(user.uid) | ||
val otherGroupUsers = groupUserService.findAllByGroupId(groupUser.groupId) | ||
|
||
val createdUids = otherGroupUsers.mapNotNull { otherGroupUser -> | ||
val isSameGroup = request.uids.contains(otherGroupUser.uid) | ||
val isExists = cheerUpService.existsByUidAndTargetUidAndCheeredAt( | ||
uid = user.uid, | ||
targetUid = otherGroupUser.uid, | ||
cheeredAt = now | ||
) | ||
|
||
/** 동일 그룹, 응원하기를 진행하지 않는 경우에 action 진행 */ | ||
when (isSameGroup && !isExists) { | ||
true -> { | ||
runCatching { | ||
cheerUpService.save( | ||
CheerUp( | ||
uid = user.uid, | ||
targetUid = otherGroupUser.uid, | ||
cheeredAt = now | ||
) | ||
) | ||
}.onFailure { e -> | ||
/** 이미 등록된 케이스인 경우가 대다수 */ | ||
logger.error(e) { "fail to create cheerUp" } | ||
}.onSuccess { createdCheerUp -> | ||
reactiveGroupUserWebSocketHandler.launchSendEventByCheerUp( | ||
uid = createdCheerUp.targetUid, | ||
groupId = otherGroupUser.groupId, | ||
senderUid = user.uid, | ||
) | ||
}.getOrNull()?.targetUid | ||
} | ||
|
||
/** 응원하기를 진행하지 못한 경우 */ | ||
false -> null | ||
} | ||
} | ||
|
||
return CheerUpResponse(createdUids.toSet()) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/kotlin/com/hero/alignlab/domain/cheer/application/CheerUpService.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,40 @@ | ||
package com.hero.alignlab.domain.cheer.application | ||
|
||
import com.hero.alignlab.common.extension.executes | ||
import com.hero.alignlab.config.database.TransactionTemplates | ||
import com.hero.alignlab.domain.cheer.domain.CheerUp | ||
import com.hero.alignlab.domain.cheer.infrastructure.CheerUpRepository | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import org.springframework.stereotype.Service | ||
import java.time.LocalDate | ||
|
||
@Service | ||
class CheerUpService( | ||
private val cheerUpRepository: CheerUpRepository, | ||
private val txTemplates: TransactionTemplates, | ||
) { | ||
suspend fun countAllByCheeredAtAndUid(cheeredAt: LocalDate, uid: Long): Long { | ||
return withContext(Dispatchers.IO) { | ||
cheerUpRepository.countAllByCheeredAtAndUid(cheeredAt, uid) | ||
} | ||
} | ||
|
||
suspend fun existsByUidAndTargetUidAndCheeredAt(uid: Long, targetUid: Long, cheeredAt: LocalDate): Boolean { | ||
return withContext(Dispatchers.IO) { | ||
cheerUpRepository.existsByUidAndTargetUidAndCheeredAt(uid, targetUid, cheeredAt) | ||
} | ||
} | ||
|
||
suspend fun save(cheerUp: CheerUp): CheerUp { | ||
return txTemplates.writer.executes { | ||
cheerUpRepository.save(cheerUp) | ||
} | ||
} | ||
|
||
suspend fun findAllByTargetUidInAndCheeredAt(targetUids: Set<Long>, cheeredAt: LocalDate): List<CheerUp> { | ||
return withContext(Dispatchers.IO) { | ||
cheerUpRepository.findAllByTargetUidInAndCheeredAt(targetUids, cheeredAt) | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/com/hero/alignlab/domain/cheer/domain/CheerUp.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,22 @@ | ||
package com.hero.alignlab.domain.cheer.domain | ||
|
||
import com.hero.alignlab.domain.common.domain.BaseEntity | ||
import jakarta.persistence.* | ||
import java.time.LocalDate | ||
|
||
@Entity | ||
@Table(name = "cheer_up") | ||
class CheerUp( | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
val id: Long = 0L, | ||
|
||
@Column(name = "uid") | ||
val uid: Long, | ||
|
||
@Column(name = "target_uid") | ||
val targetUid: Long, | ||
|
||
@Column(name = "cheered_at") | ||
val cheeredAt: LocalDate, | ||
) : BaseEntity() |
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/com/hero/alignlab/domain/cheer/infrastructure/CheerUpRepository.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,17 @@ | ||
package com.hero.alignlab.domain.cheer.infrastructure | ||
|
||
import com.hero.alignlab.domain.cheer.domain.CheerUp | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.stereotype.Repository | ||
import org.springframework.transaction.annotation.Transactional | ||
import java.time.LocalDate | ||
|
||
@Transactional(readOnly = true) | ||
@Repository | ||
interface CheerUpRepository : JpaRepository<CheerUp, Long> { | ||
fun countAllByCheeredAtAndUid(cheeredAt: LocalDate, uid: Long): Long | ||
|
||
fun existsByUidAndTargetUidAndCheeredAt(uid: Long, targetUid: Long, cheeredAt: LocalDate): Boolean | ||
|
||
fun findAllByTargetUidInAndCheeredAt(targetUids: Set<Long>, cheeredAt: LocalDate): List<CheerUp> | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/com/hero/alignlab/domain/cheer/model/request/CheerUpRequest.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,6 @@ | ||
package com.hero.alignlab.domain.cheer.model.request | ||
|
||
data class CheerUpRequest( | ||
/** 응원할 유저의 uids */ | ||
val uids: Set<Long> | ||
) |
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/com/hero/alignlab/domain/cheer/model/response/CheerUpResponse.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,6 @@ | ||
package com.hero.alignlab.domain.cheer.model.response | ||
|
||
data class CheerUpResponse( | ||
/** 응원하기가 성공한 케이스에 대해 응답 */ | ||
val uids: Set<Long>, | ||
) |
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/com/hero/alignlab/domain/cheer/resource/CheerUpResource.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,32 @@ | ||
package com.hero.alignlab.domain.cheer.resource | ||
|
||
import com.hero.alignlab.common.extension.wrapCreated | ||
import com.hero.alignlab.common.model.Response | ||
import com.hero.alignlab.domain.auth.model.AuthUser | ||
import com.hero.alignlab.domain.cheer.application.CheerUpFacade | ||
import com.hero.alignlab.domain.cheer.model.request.CheerUpRequest | ||
import com.hero.alignlab.domain.cheer.model.response.CheerUpResponse | ||
import io.swagger.v3.oas.annotations.Operation | ||
import io.swagger.v3.oas.annotations.tags.Tag | ||
import org.springframework.http.MediaType | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@Tag(name = "응원하기 API") | ||
@RestController | ||
@RequestMapping(produces = [MediaType.APPLICATION_JSON_VALUE]) | ||
class CheerUpResource( | ||
private val cheerUpFacade: CheerUpFacade, | ||
) { | ||
@Operation(summary = "응원하기") | ||
@PostMapping("/cheer-up") | ||
suspend fun cheerUp( | ||
user: AuthUser, | ||
@RequestBody request: CheerUpRequest, | ||
): ResponseEntity<Response<CheerUpResponse>> { | ||
return cheerUpFacade.cheerUp(user, request).wrapCreated() | ||
} | ||
} |
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
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
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
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.