Skip to content

Commit

Permalink
[feat #117] 디바이스 토큰(FCM) 저장 API (#118)
Browse files Browse the repository at this point in the history
* feat : FcmToken 포트 구현

* feat : FcmToken 저장 API

* feat : FcmToken 저장 Transactional 누락 반영
  • Loading branch information
dlswns2480 authored Aug 24, 2024
1 parent a90b018 commit b79a3e0
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 2 deletions.
12 changes: 12 additions & 0 deletions adapters/in-web/src/main/kotlin/com/pokit/user/UserController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.pokit.auth.config.ErrorOperation
import com.pokit.auth.model.PrincipalUser
import com.pokit.auth.model.toDomain
import com.pokit.common.wrapper.ResponseWrapper.wrapOk
import com.pokit.user.dto.request.ApiCreateFcmTokenRequest
import com.pokit.user.dto.request.ApiSignUpRequest
import com.pokit.user.dto.request.ApiUpdateNicknameRequest
import com.pokit.user.dto.request.toDto
Expand All @@ -12,6 +13,7 @@ import com.pokit.user.dto.response.InterestTypeResponse
import com.pokit.user.dto.response.UserResponse
import com.pokit.user.dto.response.toResponse
import com.pokit.user.exception.UserErrorCode
import com.pokit.user.model.FcmToken
import com.pokit.user.model.InterestType
import com.pokit.user.port.`in`.UserUseCase
import io.swagger.v3.oas.annotations.Operation
Expand Down Expand Up @@ -68,4 +70,14 @@ class UserController(
InterestType.values()
.map { InterestTypeResponse(it.name, it.kor) }
.wrapOk()

@PostMapping("/fcm")
@Operation(summary = "fcm 토큰 저장 API")
fun createFcmToken(
@AuthenticationPrincipal user: PrincipalUser,
@RequestBody request: ApiCreateFcmTokenRequest
): ResponseEntity<FcmToken> {
return userUseCase.createFcmToken(user.id, request.toDto())
.wrapOk()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.pokit.user.dto.request

import io.swagger.v3.oas.annotations.media.Schema
import jakarta.validation.constraints.NotNull

data class ApiCreateFcmTokenRequest(
@field:Schema(description = "사용자 기기 토큰(FCM 토큰)")
@field:NotNull
val token: String
)

internal fun ApiCreateFcmTokenRequest.toDto() = CreateFcmTokenRequest(
token = this.token
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.pokit.out.persistence.user.impl

import com.pokit.out.persistence.user.persist.FcmTokenEntity
import com.pokit.out.persistence.user.persist.FcmTokenRepository
import com.pokit.out.persistence.user.persist.toDomain
import com.pokit.user.model.FcmToken
import com.pokit.user.port.out.FcmTokenPort
import org.springframework.stereotype.Repository

@Repository
class FcmTokenAdapter(
private val fcmTokenRepository: FcmTokenRepository
) : FcmTokenPort {
override fun persist(fcmToken: FcmToken): FcmToken {
val fcmTokenEntity = FcmTokenEntity.of(fcmToken)
return fcmTokenRepository.save(fcmTokenEntity).toDomain()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,24 @@ class FcmTokenEntity(
val userId: Long,

@Column(name = "token")
val token: String
val token: String,

@Column(name = "is_deleted")
var deleted: Boolean = false
) {
fun delete() {
this.deleted = true
}

companion object {
fun of(fcmToken: FcmToken) = FcmTokenEntity(
userId = fcmToken.userId,
token = fcmToken.token
)
}
}

fun FcmTokenEntity.toDomain() = FcmToken(
userId = this.userId,
token = this.token
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.pokit.out.persistence.user.persist

import org.springframework.data.jpa.repository.JpaRepository

interface FcmTokenRepository : JpaRepository<FcmTokenEntity, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.pokit.user.port.`in`

import com.pokit.user.dto.request.CreateFcmTokenRequest
import com.pokit.user.dto.request.SignUpRequest
import com.pokit.user.dto.request.UpdateNicknameRequest
import com.pokit.user.model.FcmToken
import com.pokit.user.model.User

interface UserUseCase {
Expand All @@ -12,4 +14,6 @@ interface UserUseCase {
fun updateNickname(user: User, request: UpdateNicknameRequest): User

fun fetchAllUserId(): List<Long>

fun createFcmToken(userId: Long, request: CreateFcmTokenRequest): FcmToken
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.pokit.user.port.out

import com.pokit.user.model.FcmToken

interface FcmTokenPort {
fun persist(fcmToken: FcmToken): FcmToken
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import com.pokit.category.port.out.CategoryImagePort
import com.pokit.category.port.out.CategoryPort
import com.pokit.common.exception.ClientValidationException
import com.pokit.common.exception.NotFoundCustomException
import com.pokit.user.dto.request.CreateFcmTokenRequest
import com.pokit.user.dto.request.SignUpRequest
import com.pokit.user.dto.request.UpdateNicknameRequest
import com.pokit.user.exception.UserErrorCode
import com.pokit.user.model.FcmToken
import com.pokit.user.model.User
import com.pokit.user.port.`in`.UserUseCase
import com.pokit.user.port.out.FcmTokenPort
import com.pokit.user.port.out.UserPort
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -22,7 +25,8 @@ import org.springframework.transaction.annotation.Transactional
class UserService(
private val userPort: UserPort,
private val categoryPort: CategoryPort,
private val categoryImagePort: CategoryImagePort
private val categoryImagePort: CategoryImagePort,
private val fcmTokenPort: FcmTokenPort
) : UserUseCase {
companion object {
private const val UNCATEGORIZED_IMAGE_ID = 1
Expand Down Expand Up @@ -75,4 +79,12 @@ class UserService(
override fun fetchAllUserId() =
userPort.loadAllIds()


@Transactional
override fun createFcmToken(userId: Long, request: CreateFcmTokenRequest): FcmToken {
val user = userPort.loadById(userId)
?: throw NotFoundCustomException(UserErrorCode.NOT_FOUND_USER)
val fcmToken = FcmToken(user.id, request.token)
return fcmTokenPort.persist(fcmToken)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.pokit.user.dto.request

data class CreateFcmTokenRequest(
val token: String
)

0 comments on commit b79a3e0

Please sign in to comment.