Skip to content

Commit

Permalink
feat: FCM
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaisqls committed Jul 8, 2023
1 parent f7a0411 commit 2d8d4ed
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,42 @@ package team.aliens.dms.thirdparty.notification
import com.google.auth.oauth2.GoogleCredentials
import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Configuration
import java.io.File
import java.io.IOException
import java.net.URL
import java.nio.file.Files
import java.nio.file.Paths
import javax.annotation.PostConstruct
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Configuration


@Configuration
class FCMConfig(
@Value("\${fcm.path}")
private val path: String
@Value("\${fcm.file-url}")
private val url: String
) {

@PostConstruct
fun initialize() {
try {
if (FirebaseApp.getApps().isEmpty()) {
val options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(File(path).inputStream()))
.build()
FirebaseApp.initializeApp(options)
URL(url).openStream().use { inputStream ->
Files.copy(inputStream, Paths.get(PATH))
val file = File(PATH)
if (FirebaseApp.getApps().isEmpty()) {
val options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(file.inputStream()))
.build()
FirebaseApp.initializeApp(options)
}
file.delete()
}
} catch (e: IOException) {
e.printStackTrace()
}
}

companion object {
private const val PATH = "./credentials.json"
}
}
2 changes: 1 addition & 1 deletion dms-infrastructure/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ spring:
max-request-size: 20MB

fcm:
path: ${FCM_PATH}
file-url: ${FCM_FILE_URL}

secret:
secret-key: ${SECRET_KEY:asdfghgfds}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package team.aliens.dms.persistence.notification

import com.querydsl.core.QueryFactory
import com.querydsl.jpa.impl.JPAQueryFactory
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Component
import team.aliens.dms.domain.notification.model.NotificationOfUser
import team.aliens.dms.domain.notification.spi.NotificationOfUserPort
import team.aliens.dms.persistence.notification.mapper.NotificationOfUserMapper
import team.aliens.dms.persistence.notification.repository.NotificationOfUserJpaRepository
import java.util.UUID
import team.aliens.dms.common.dto.PageData
import team.aliens.dms.persistence.notification.entity.QNotificationOfUserJpaEntity.notificationOfUserJpaEntity

@Component
class NotificationOfPersistenceAdapterOfUser(
private val notificationOfUserMapper: NotificationOfUserMapper,
private val notificationOfUserRepository: NotificationOfUserJpaRepository
private val notificationOfUserRepository: NotificationOfUserJpaRepository,
private val queryFactory: JPAQueryFactory
) : NotificationOfUserPort {

override fun saveNotificationOfUser(notificationOfUser: NotificationOfUser) =
Expand All @@ -29,9 +34,14 @@ class NotificationOfPersistenceAdapterOfUser(
)
}

override fun queryNotificationOfUserByUserId(userId: UUID) =
notificationOfUserRepository.findByUserId(userId)
.map { notificationOfUserMapper.toDomain(it)!! }

override fun queryNotificationOfUserByUserId(userId: UUID, pageData: PageData) =
queryFactory
.selectFrom(notificationOfUserJpaEntity)
.where(notificationOfUserJpaEntity.user.id.eq(userId))
.offset(pageData.offset)
.limit(pageData.size)
.fetch().map { notificationOfUserMapper.toDomain(it)!! }

override fun queryNotificationOfUserById(notificationOfUserId: UUID) = notificationOfUserMapper.toDomain(
notificationOfUserRepository.findByIdOrNull(notificationOfUserId)
Expand Down

0 comments on commit 2d8d4ed

Please sign in to comment.